0

I'd like to be able to set a View.OnclickListener to an ImageView inside a page (fragment) of my ViewPager, I don't want to click on the whole page of my ViewPager, I just want to be able to click on a specifig ImageView. I followed these questions (onClick not triggered on LinearLayout with child, How to set OnClickListener in ViewPager ) but they did not help me. This is my actual code:

MyPageFragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:focusableInTouchMode="true"
        android:id="@+id/container_linear"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop" />
    </LinearLayout>

</LinearLayout>

MyActivity.xml

<android.support.v4.view.ViewPager
                    android:id="@+id/product_view_pager"
                    android:layout_width="match_parent"
                    android:layout_height="240dp"
                    android:layout_marginLeft="4dp"
                    android:layout_marginRight="4dp"
                    android:gravity="center"
                    android:overScrollMode="never" />

MyFragment.class (where I'm binding the views using Butterknife)

@BindView(R.id.container_linear)
LinearLayout mContainer;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View mRootView = inflater.inflate(R.layout.my_layout, container, false);
    ButterKnife.bind(this, mRootView);

    mContainer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("Test", "Clicked!");
        }
    });
}

Thanks for your support.

Agna JirKon Rx
  • 2,321
  • 2
  • 29
  • 44

3 Answers3

0

One thing to consider would be moving your OnClickListener to the parent LinearLayout you're setting as clickable and seeing if that helps.

Another thing is adding android:focusableInTouchMode="true" to the view you're attaching your OnClickListener to.

Other than that, you might want to share the code for registering the OnClickListener so we can investigate other possible errors.

Bjelis
  • 116
  • 7
0

If I understand correctly you want to click on the image view insideMyPageFragment.xml. This is simple but you need to give ID to the ImageView and refer it in your onCreateView method like you do for LinearLayout and then use onClickListener on that image.

So with recent conversation it seems that in linear layout you need to remove line clickable="false"

Basically that has blocked all the clicks on its children, thus that linear layout as well as the imageview clicks are not working for you. See this code I removed that line. Hope this works

Follow this code.

MyPageFragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:duplicateParentState="true"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="240dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:focusableInTouchMode="true"
    android:id="@+id/container_linear"
    android:gravity="center"
    android:orientation="vertical">
  
    <--you need to give ID to the view to be able to 
                         perform events on them specifically.-->
    <ImageView
        android:id="@+id/my_awesome_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop" />
</LinearLayout>

Now in your MyFragment.class

@BindView(R.id.container_linear)
LinearLayout mContainer;
//declare with butterknife
@BindView(R.id.my_awesome_image)
ImageView myAwesomeImage;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                     @Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View mRootView = inflater.inflate(R.layout.my_layout, container, false);
    ButterKnife.bind(this, mRootView);
    
     //you set click listener previously on entire linear layout instead of imageview 
     //that's why it was not reflecting on imageview click.


    myAwesomeImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG , "Awesome Imageview clicked!");
        }
    });
}
Community
  • 1
  • 1
Aalap Patel
  • 2,058
  • 2
  • 17
  • 31
0

I solved this issue using the following chunk of code:

@Override
public void onViewCreated(View view, Bundle savedInstanceState){

    view.setOnClickListener(new OnClickListener(){
        public void onClick(View v){

            /*  Do as you please here. */
        }
    });
}

The reference for this answer can be viewed here: How to detect click on ImageView in ViewPager's PagerAdapter (android)?

Agna JirKon Rx
  • 2,321
  • 2
  • 29
  • 44