RecyclerView:setOnClickListener无法访问完整的CardView

时间:2016-06-02 08:04:59

标签: android android-recyclerview onitemclicklistener

我有一个包含ImageView的CardView。 现在我已经实现了一个onItemClickListener。 但它只有在我点击图像视图未覆盖的CardView空间时才有效。 如果我点击图像视图,则不会发生任何事情。

即使点击ImageView,如何实现CardView的onItemClick方法?

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.CardView
            android:id="@+id/card_view_maschine"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_margin="@dimen/card_margin"
            android:elevation="6dp"
            card_view:cardCornerRadius="@dimen/card_album_radius">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/thumbnail"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/maschine_cover_height"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:clickable="true"
                    android:scaleType="fitXY" />

                <TextView
                    android:id="@+id/title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/thumbnail"
                    android:paddingLeft="@dimen/maschine_title_padding"
                    android:paddingRight="@dimen/maschine_title_padding"
                    android:paddingTop="@dimen/maschine_title_padding"
                    android:textColor="@color/recyclerViewTitleColor"
                    android:textSize="@dimen/maschine_title" />

                <TextView
                    android:id="@+id/description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/title"
                    android:paddingBottom="@dimen/description_count_padding_bottom"
                    android:paddingLeft="@dimen/maschine_title_padding"
                    android:paddingRight="@dimen/maschine_title_padding"
                    android:textSize="@dimen/description_count" />

                <ImageView
                    android:id="@+id/overflow"
                    android:layout_width="@dimen/ic_maschine_overflow_width"
                    android:layout_height="@dimen/ic_maschine_overflow_height"
                    android:layout_alignParentRight="true"
                    android:layout_below="@id/thumbnail"
                    android:layout_marginTop="@dimen/ic_maschine_overflow_margin_top"
                    android:scaleType="centerCrop"
                    android:src="@drawable/ic_dots" />

            </RelativeLayout>

        </android.support.v7.widget.CardView>

    </LinearLayout>

OnItemClick:

ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                showDetailFragment();
            }
        });

我用过ItemClickSupport class

3 个答案:

答案 0 :(得分:1)

添加以下课程

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener  {

private OnItemClickListener mListener;

public interface OnItemClickListener {
    public void onItemClick(View view, int position);
}

GestureDetector mGestureDetector;

public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
    mListener = listener;
    mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }
    });

}

@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
    View childView = view.findChildViewUnder(e.getX(), e.getY());
    if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
        mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {

}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}

}

您可以将其用于整个应用程序,如下所示

 recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            //your code here
        }
    }) );

希望这项工作能够肯定。

答案 1 :(得分:0)

在适配器中定义接口,例如

private ClickListener mClickListener;


public interface ClickListener{
    void itemClicked(View view, int position);
}

制作公共制定者:

public void setClickListener(ClickListener clickListener){
    this.mClickListener = clickListener;
}

然后在你的观察者中:

itemView.setOnClickListener(this);

 @Override
 public void onClick(View v) {
     if(mClickListener!=null){
         mClickListener.mainMenuItemClicked(v, getAdapterPosition());
     }
 }

然后在你的适配器活动中:

adapter.setClickListener(this);

//and implement this interface

public class MainActivity extends ActionBarActivity implements YourAdapter.ClickListener

@Override
public void itemClicked(View view, int position) {
}

如果您想要点击图片然后将标签设置为此视图,则在活动中您可以从视图中获取标签并查看点击的位置:

imageView.setTag("image");
imageView.setOnClickListener(this);

@Override
public void itemClicked(View view, int position) {
    if (view.getTag() != null && ((String)view.getTag()).equals("image")){
        //....
    }
}

答案 2 :(得分:0)

FrameLayout中包裹您的所有内容,并将onClickListener移至FrameLayout并在clickable上设置true

示例:因此,如果您当前的布局如下所示:

    <LinearLayout>
        <TextView/>
        <ImageView/>
    </LinearLayout>

使用FrameLayout这样包装内容:

    <FrameLayout
        android:foreground="?attr/selectableItemBackground"
        android:clickable="true"
        ... >

        <LinearLayout>
            <TextView/>
            <ImageView/>
        </LinearLayout>

    </FrameLayout>

然后将当前的onClickListener移至FrameLayout

PS:前景android:foreground="?attr/selectableItemBackground"为您的可点击区域添加了一个漂亮的涟漪效果,这是可选的。