RecyclerView onClick无法正常工作?

时间:2016-12-08 07:16:13

标签: android android-layout android-recyclerview

我在片段中使用RecyclerView以网格格式显示图像,Recycler视图grid_item.xml如下所示:

8:19

我使用适配器填充RecyclerView中的数据,其代码为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@id/card_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_margin="@dimen/card_margin_grid"
        card_view:cardCornerRadius="@dimen/card_album_radius">

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

            <ImageView
                android:id="@id/thumbnail"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:background="?selectableItemBackgroundBorderless"
                android:clickable="true"
                android:scaleType="fitCenter" />

            <TextView
                android:id="@id/title"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/thumbnail"
                android:layout_margin="@dimen/album_title_padding"
                android:textColor="@color/album_title"
                android:textSize="@dimen/album_title" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</RelativeLayout>

我的问题是点击监听器只处理TextView而不是图像,虽然我在整个视图上设置了包含图像和文本的点击监听器。我的实现有问题吗?

5 个答案:

答案 0 :(得分:8)

尝试将android:focusableInTouchMode="false"设置为您的imageview。 并删除android:clickable="true"或将其设置为false

答案 1 :(得分:3)

使用viewHolder.itemView之类的:

viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             //your action
        });

这将对整个RecyclerView项启用点击操作。

答案 2 :(得分:0)

0000000004004d6 <main>:
int main(int argc, char *argv[]) {
  4004d6:       55                      push   rbp
  4004d7:       48 89 e5                mov    rbp,rsp
  4004da:       89 7d fc                mov    DWORD PTR [rbp-0x4],edi
  4004dd:       48 89 75 f0             mov    QWORD PTR [rbp-0x10],rsi

        return 0;
  4004e1:       b8 00 00 00 00          mov    eax,0x0
}
  4004e6:       5d                      pop    rbp
  4004e7:       c3                      ret    
  4004e8:       0f 1f 84 00 00 00 00    nop    DWORD PTR [rax+rax*1+0x0]
  4004ef:       00

答案 3 :(得分:0)

公共类OffersRecycleViewAdapter扩展了RecyclerView.Adapter {

private Activity mActivity;
private List<OfferShopModel> offerShopModels = new ArrayList<>();
ArrayList<String> allColors = new ArrayList<String>();


public OffersRecycleViewAdapter(List<OfferShopModel> offerShopModels, Activity activity, ArrayList<String> allColors) {
    this.offerShopModels = offerShopModels;
    this.mActivity = activity;
}

// String[] allColors = mActivity.getResources().getStringArray(R.array.colors);
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_offers, viewGroup, false);

    return new ItemViewHolder(v);
}

@TargetApi(Build.VERSION_CODES.M)
@Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, final int position) {

    itemViewHolder.mOffer.setText(offerShopModels.get(position).getOffer());

}

@Override
public int getItemCount() {
    return offerShopModels.size();
}

class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    ImageView mLinearLayoutBack;
    TextView mOffer;

    ItemViewHolder(View itemView) {
        super(itemView);
        mOffer = (TextView) itemView.findViewById(R.id.offer);        
        mOffer.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

  // call click event

    }
}

}

答案 4 :(得分:0)

android:clickable="true"删除ImageView或将其更改为android:clickable="false"