RecyclerView ?android:attr/selectableItemBackground 对项目不起作用 [英] RecyclerView ?android:attr/selectableItemBackground does not work on items

查看:54
本文介绍了RecyclerView ?android:attr/selectableItemBackground 对项目不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 items.xml

I have this items.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:focusable="true"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:layout_height="wrap_content">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:id="@+id/colorPreview" />


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:textAppearance="@android:style/TextAppearance.Large"
    android:textColor="@android:color/white"
    android:id="@+id/colorName" />

</RelativeLayout>

当我单独使用它时,selectableItemBackground 在我单击视图时会出现动画.但是当我将它用于 RecyclerView 中的项目时,点击效果不再发生.我该如何解决这个问题?

When I use it separately, the selectableItemBackground animates when I click the view. But when I use it for the items in a RecyclerView, the effect on click does not happen anymore. How can I fix this?

PS:这是 RecyclerView 上的侦听器,如果相关:

PS: this is the listener on the RecyclerView, if it is relevant:

 public ColorListOnItemTouchListener(Context context, OnItemClickListener clickListener) {
    mClickListener = clickListener;
    mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
                if(childView != null && mClickListener != null) {
                    mClickListener.onItemLongPress(childView, index);
                }
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            if(childView != null && mClickListener != null) {
                mClickListener.onItemClick(childView, index);
            }
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return false;
        }
    });
}

谢谢!

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

    private static final int layout = R.layout.color_item;
    private Cursor mCursor;

    public ColorsCursorAdapter(Cursor c) {
        super();
        this.mCursor = c;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
        TextView name = (TextView) v.findViewById(R.id.colorName);
        ImageView image = (ImageView) v.findViewById(R.id.colorPreview);
        return new ViewHolder(v, name, image);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        mCursor.moveToPosition(position);
        int color = mCursor.getInt(mCursor.getColumnIndex(ColorItem.COLUMN_COLOR));
        holder.colorName.setText(Utils.getColorString(color));
        holder.colorPreview.setImageDrawable(new ColorDrawable(color));
    }

    @Override
    public int getItemCount() {
        if(mCursor != null) {
            return mCursor.getCount();
        }
        return 0;
    }

    public void swapCursor(Cursor c) {
        mCursor = c;
        notifyDataSetChanged();
    }

    public Cursor getCursor() {
        return mCursor;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView colorName;
        public ImageView colorPreview;

        public ViewHolder(View root, TextView colorName, ImageView colorPreview) {
            super(root);
            root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //
                }
            });
            this.colorName = colorName;
            this.colorPreview = colorPreview;
        }
    }
}

适配器是通过以下方式创建的:

And the adapter is created with:

colorList.setLayoutManager(new LinearLayoutManager(this));
    adapter = new ColorsCursorAdapter(null);
    colorList.setAdapter(adapter);

推荐答案

其他答案中未提及的内容:需要设置 android:clickable="true" 才能使动画正常工作当没有 OnClickListener 附加到视图时.

Something that was not mentioned in other answers: setting android:clickable="true" is required in order to make the animations work when there is no OnClickListener attached to the view.

这篇关于RecyclerView ?android:attr/selectableItemBackground 对项目不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆