如何检测 ViewPager 的 PagerAdapter (android) 中对 ImageView 的点击?
How to detect click on ImageView in ViewPager's PagerAdapter (android)?
发生了什么:
- 我正在尝试检测寻呼机中视图(图像)的 onclick 事件
适配器。
- 我正在触发的视图是
imgGodInfoId
- 这永远不会触发
- 这似乎在常规适配器中有效......那么为什么它没有被触发
这里
AdptAtomicGodGallery.java
public class AdptAtomicGodGallery extends PagerAdapter {
// Declare Variables
Context context;
Integer[] godImages;
LayoutInflater inflater;
String godName;
public AdptAtomicGodGallery(Context context, Integer[] _godImages, String _godName) {
this.context = context;
this.godImages = _godImages;
this.godName=_godName;
}
@Override
public int getCount() {
return godImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables
ImageView imgGodId;
final ImageView imgGodInfoId;
final RelativeLayout root;
TextView txtShortDescription;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.adpt_atomic_god_gallery, container,false);
// Locate the ImageView in viewpager_item.xml
imgGodId = (ImageView) itemView.findViewById(R.id.imgGodId);
imgGodInfoId = (ImageView) itemView.findViewById(R.id.imgGodInfoId);
root= (RelativeLayout) itemView.findViewById(R.id.root);
txtShortDescription= (TextView) itemView.findViewById(R.id.txtShortDescription);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap preview_bitmap = BitmapFactory.decodeResource(context.getResources(),godImages[position], options);
Drawable drawable = new BitmapDrawable(context.getResources(),preview_bitmap);
root.setBackgroundDrawable(drawable);
root.setTag(godImages[position]);
imgGodInfoId.setTag(godName);
if(godName.equalsIgnoreCase(Constants.GOD_ONE)){
txtShortDescription.setText(Constants.GOD_ONE_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_TWO)){
txtShortDescription.setText(Constants.GOD_TWO_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_THREE)){
txtShortDescription.setText(Constants.GOD_THREE_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_FOUR)){
txtShortDescription.setText(Constants.GOD_FOUR_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_FIVE)){
txtShortDescription.setText(Constants.GOD_FIVE_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_SIX)){
txtShortDescription.setText(Constants.GOD_SIX_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_SEVEN)){
txtShortDescription.setText(Constants.GOD_SEVEN_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_EIGHT)){
txtShortDescription.setText(Constants.GOD_EIGHT_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_NINE)){
txtShortDescription.setText(Constants.GOD_NINE_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_TEN)){
txtShortDescription.setText(Constants.GOD_TEN_DESCRIPTION_SHORT_DESC);
}
imgGodInfoId.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
/* BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap preview_bitmap = BitmapFactory.decodeResource(context.getResources(),(Integer) root.getTag(), options);
*/
Toast.makeText(context, "clicked", Toast.LENGTH_SHORT).show();
/*Intent intent=new Intent(context,ActAtomicGodDetailDesc.class);
intent.putExtra("GOD_NAME", imgGodInfoId.getTag().toString());
intent.putExtra("GOD_IMAGE",(Integer) root.getTag());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);*/
}
});
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((LinearLayout) object);
}
}
Xml 寻呼机适配器
<RelativeLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgGodInfoId"
android:layout_width="75dp"
android:layout_height="75dp"
android:clickable="true"
android:padding="5dp"
android:scaleType="fitXY"
android:src="@drawable/img_info_black" />
<LinearLayout
android:id="@+id/selShortDescContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/sel_selectyourgod"
android:gravity="center" >
<TextView
android:id="@+id/txtShortDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="@string/swipe_notification"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:textStyle="bold"
android:color="#55000000" />
</LinearLayout>
<LinearLayout
android:id="@+id/selFavGodsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/selShortDescContainer"
android:layout_alignParentLeft="true"
android:layout_marginBottom="141dp"
android:background="@drawable/sel_selectyourgod"
android:gravity="center" >
<TextView
android:id="@+id/txtNotification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="@string/swipe_notification"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:textStyle="bold"
android:color="#55000000" />
</LinearLayout>
</RelativeLayout>
我已经能够这样做了:重写 ViewPager
持有的 Fragment
的 onViewCreated()
方法为:
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
view.setOnClickListener(new OnClickListener(){
public void onClick(View v){
/* Do as you please here. */
}
});
}
试试这个。这应该有效。
编辑:
如果您将 ViewPager
和 PagerAdapter
与 Fragment
一起使用,并覆盖 Fragment
的 onViewCreated()
方法,则此方法将起作用。
发生了什么:
- 我正在尝试检测寻呼机中视图(图像)的 onclick 事件 适配器。
- 我正在触发的视图是
imgGodInfoId
- 这永远不会触发
- 这似乎在常规适配器中有效......那么为什么它没有被触发 这里
AdptAtomicGodGallery.java
public class AdptAtomicGodGallery extends PagerAdapter {
// Declare Variables
Context context;
Integer[] godImages;
LayoutInflater inflater;
String godName;
public AdptAtomicGodGallery(Context context, Integer[] _godImages, String _godName) {
this.context = context;
this.godImages = _godImages;
this.godName=_godName;
}
@Override
public int getCount() {
return godImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables
ImageView imgGodId;
final ImageView imgGodInfoId;
final RelativeLayout root;
TextView txtShortDescription;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.adpt_atomic_god_gallery, container,false);
// Locate the ImageView in viewpager_item.xml
imgGodId = (ImageView) itemView.findViewById(R.id.imgGodId);
imgGodInfoId = (ImageView) itemView.findViewById(R.id.imgGodInfoId);
root= (RelativeLayout) itemView.findViewById(R.id.root);
txtShortDescription= (TextView) itemView.findViewById(R.id.txtShortDescription);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap preview_bitmap = BitmapFactory.decodeResource(context.getResources(),godImages[position], options);
Drawable drawable = new BitmapDrawable(context.getResources(),preview_bitmap);
root.setBackgroundDrawable(drawable);
root.setTag(godImages[position]);
imgGodInfoId.setTag(godName);
if(godName.equalsIgnoreCase(Constants.GOD_ONE)){
txtShortDescription.setText(Constants.GOD_ONE_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_TWO)){
txtShortDescription.setText(Constants.GOD_TWO_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_THREE)){
txtShortDescription.setText(Constants.GOD_THREE_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_FOUR)){
txtShortDescription.setText(Constants.GOD_FOUR_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_FIVE)){
txtShortDescription.setText(Constants.GOD_FIVE_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_SIX)){
txtShortDescription.setText(Constants.GOD_SIX_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_SEVEN)){
txtShortDescription.setText(Constants.GOD_SEVEN_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_EIGHT)){
txtShortDescription.setText(Constants.GOD_EIGHT_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_NINE)){
txtShortDescription.setText(Constants.GOD_NINE_DESCRIPTION_SHORT_DESC);
}else if(godName.equalsIgnoreCase(Constants.GOD_TEN)){
txtShortDescription.setText(Constants.GOD_TEN_DESCRIPTION_SHORT_DESC);
}
imgGodInfoId.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
/* BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap preview_bitmap = BitmapFactory.decodeResource(context.getResources(),(Integer) root.getTag(), options);
*/
Toast.makeText(context, "clicked", Toast.LENGTH_SHORT).show();
/*Intent intent=new Intent(context,ActAtomicGodDetailDesc.class);
intent.putExtra("GOD_NAME", imgGodInfoId.getTag().toString());
intent.putExtra("GOD_IMAGE",(Integer) root.getTag());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);*/
}
});
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((LinearLayout) object);
}
}
Xml 寻呼机适配器
<RelativeLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgGodInfoId"
android:layout_width="75dp"
android:layout_height="75dp"
android:clickable="true"
android:padding="5dp"
android:scaleType="fitXY"
android:src="@drawable/img_info_black" />
<LinearLayout
android:id="@+id/selShortDescContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/sel_selectyourgod"
android:gravity="center" >
<TextView
android:id="@+id/txtShortDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="@string/swipe_notification"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:textStyle="bold"
android:color="#55000000" />
</LinearLayout>
<LinearLayout
android:id="@+id/selFavGodsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/selShortDescContainer"
android:layout_alignParentLeft="true"
android:layout_marginBottom="141dp"
android:background="@drawable/sel_selectyourgod"
android:gravity="center" >
<TextView
android:id="@+id/txtNotification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="@string/swipe_notification"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:textStyle="bold"
android:color="#55000000" />
</LinearLayout>
</RelativeLayout>
我已经能够这样做了:重写 ViewPager
持有的 Fragment
的 onViewCreated()
方法为:
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
view.setOnClickListener(new OnClickListener(){
public void onClick(View v){
/* Do as you please here. */
}
});
}
试试这个。这应该有效。
编辑:
如果您将 ViewPager
和 PagerAdapter
与 Fragment
一起使用,并覆盖 Fragment
的 onViewCreated()
方法,则此方法将起作用。