【问题标题】:RecyclerView onClick not working properly?RecyclerView onClick 不能正常工作?
【发布时间】:2016-12-08 07:16:13
【问题描述】:

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

<?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>

我正在使用适配器在 RecyclerView 中填充数据,代码如下:

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {

    private ArrayList<Movie> movies;
    private Context context;

    public DataAdapter(Context context, ArrayList<Movie> movies) {
        this.movies = movies;
        this.context = context;
    }

    public void addItems(ArrayList<Movie> movies) {

        this.movies.addAll(movies);

    }

    @Override
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_view_grid_item, viewGroup, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {

        viewHolder.title.setText(movies.get(i).getTitle());
        Picasso.with(context).load(movies.get(i).getImage_url_medium()).placeholder(R.drawable.placeholder).into(viewHolder.thumbnail);
    }

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



    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView title;
        private ImageView thumbnail;

        public ViewHolder(View view) {
            super(view);

            title = (TextView) view.findViewById(R.id.title);
            thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
            view.setOnClickListener(this);
        }

        // Handles the row being being clicked
        @Override
        public void onClick(View view) {
            int position = getAdapterPosition(); // gets item position
            if (position != RecyclerView.NO_POSITION) { // Check if an item was deleted, but the user clicked it before the UI removed it
                Movie movie = movies.get(position);
                // start detail activity
                Intent i = new Intent(context, MovieDetail.class);
                i.putExtra(Constants.MOVIES_OBJECT, movie);
                context.startActivity(i);
            }
        }
    }


}

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

【问题讨论】:

  • 尝试在 CardView 上实现 onclicklistner

标签: android android-layout android-recyclerview


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    使用viewHolder.itemView 喜欢:

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

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

    【讨论】:

      【解决方案3】:

      android:clickable="true" 添加到根RelativeLayout

      【讨论】:

        【解决方案4】:
        <?xml version="1.0" encoding="utf-8"?>
        <android.support.v7.widget.CardView
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                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"
                android:clickable="true"
                android:focusableInTouchMode="true"
                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="false"
                        android:focusableInTouchMode="false"
                        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>
        

        【讨论】:

          【解决方案5】:

          公共类 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
          
              }
          }
          

          }

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-06-05
              • 2015-08-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-07-12
              • 2019-02-08
              • 1970-01-01
              相关资源
              最近更新 更多