2016-10-06 73 views
3

我想setOnClickListener我的ImageView这是在ViewPagerItemXML从ViewPager OnclickAction项目布局

在我activityXML我有ViewPager和其他几个按钮。在我的ViewPagerItemXML我可点击ImageView。我想在活动中点击ImageView之后做出一些操作。不幸的是OnClickListener根本没有触发。

LayoutInflater inflater = (LayoutInflater) this 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
itemView = inflater.inflate(R.layout.viewPager_item, null); 

onThemePhotoClick= (ImageView)itemView.findViewById(R.id.themePhoto); 
onThemePhotoClick.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
      Log.i(TAG,"clickOnPhoto!"); 
    }); 

编辑

我的动态XML

LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
    android:orientation="vertical" 
     android:weightSum="10"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:id="@+id/iconTools"> 

     <ImageButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_navigate_before_black_36dp" 
      android:background="@android:color/transparent" 
      android:id="@+id/backButton" 
      /> 
    </LinearLayout> 

    <LinearLayout 

     android:layout_width="match_parent" 
     android:orientation="horizontal" 
     android:layout_height="0dp" 
     android:layout_weight="8" 
     android:id="@+id/viewPagerLayout"> 

     <android.support.v4.view.ViewPager 
     android:id="@+id/visitMuseumPager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:listitem="@layout/art_work_item" /> 
</LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:id="@+id/bottomButtonToolbar"> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/qrScannerButton" 
     android:text="QrCode" /> 
</LinearLayout> 

    </LinearLayout> 

而且viewPagerItem

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:fillViewport="true"> 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingTop="10dp"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="200dp"> 
     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@color/WelcomeScreenDarker" 
      android:id="@+id/imageBackground" 
      /> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       android:id="@+id/themePhoto" 
       android:clickable="true" 
       /> 
     </RelativeLayout> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/artWorkCounter" 
     android:textColor="@color/searchHint" 
     android:textSize="12sp" 
     android:layout_marginRight="80dp" 
     android:layout_gravity="end" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/artWorkDescriptionTitle" 
      android:paddingTop="15dp" 
      android:textSize="20sp" 
      android:textStyle="bold" 
      android:layout_margin="10dp"/> 

     <TextView 

      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/artWorkDescription" 
      android:layout_margin="10dp" 
      /> 
    </LinearLayout> 
</ScrollView> 
    </LinearLayout> 

EDIT 2 我加入OnClickListener到viewpagerAdapterClass解决我的问题。它可能触发另一种观点。如果有人知道如何触发按钮的实际活动课,请给我一个提示

+0

可能是盗取点击的另一个视图。查看您的ViewPagerXml可能会有所帮助 – JDenais

回答

0

你可以尝试这改变了你的活动..

<ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:id="@+id/themePhoto" /> 

,并在类..

onThemePhotoClick=(ImageView)itemView.findViewById(R.id.themePhoto); 

onThemePhotoClick.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Toast.makeText(context, "You click " + position, Toast.LENGTH_LONG).show(); 
     } 
    }); 
+0

您必须在添加之前定义您的点击侦听器。它不会像这样编译。 – JDenais

+0

我不明白,imageView设置在另一个布局。我应该在viewPager部分的activity布局中实现相同的图像View? – Expiredmind

+0

请参阅我编辑的答案..谢谢 –

0

化妆的ImageView与使用最终可以点击查看要单击最终

final ImageView onThemePhotoClick= (ImageView)itemView.findViewById(R.id.themePhoto); 
+0

仍然不工作 – Expiredmind

2

视图在适配器中触发而不是触发是正常的。

在您的活动中获取充值的层次结构仅包含ViewPager,其中包含不同ViewPagerItems并借助适配器。

这个想法是,您需要将该点击事件传递回活动。要做到这一点,请创建一个接口回调,让您的活动实施它,并从您的适配器调用回调。

关于SO的许多问题将帮助您。 Call Activity method from adapter