在recycler listview中为所选项目设置背景颜色

时间:2016-08-10 12:27:08

标签: android android-layout android-recyclerview recycler-adapter

我想在回收器列表视图中更改视图的背景颜色,并在同一列表视图中单击另一个项目时恢复为默认颜色。

我的回收站视图模板就像这样

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/list_view_click_background"
    xmlns:custom="http://schemas.android.com/apk/res-auto">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2"
        android:padding="10dp">

        <com.kahoindia.dev.customclasses.KaHOTextView
            android:id="@+id/txtHomework"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            style="@style/kaho_content_label_textview_style"
            custom:CustomTextViewFont="@string/kaho_segoeui_regular_font"
            android:layout_weight="1.5"
            />

        <com.kahoindia.dev.customclasses.KaHOTextView
            android:id="@+id/txtDate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:paddingRight="10dp"
            custom:CustomTextViewFont="@string/kaho_segoeui_regular_font"
            style="@style/kaho_content_description_textview_style"
            android:layout_weight="0.5"/>

    </LinearLayout>

</LinearLayout>

和项目点击监听器是

mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), mRecyclerView, new KaHOIRecyclerViewItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                try {

                     for (int i = 0; i < mRecyclerView.getChildCount(); i++) {
                          if (position == i) {
                                mRecyclerView.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.colorSelectedBackground));
                            } else {
                                mRecyclerView.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.colorWhite));
                          }
                     }


                } catch (Exception e) {

                }
            }

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

            }
        }));

RecyclerTouchListener

 public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

        private GestureDetector gestureDetector;
        private KaHOIRecyclerViewItemClickListener clickListener;

        public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final KaHOIRecyclerViewItemClickListener clickListener) {
            this.clickListener = clickListener;
            gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    return true;
                }

                @Override
                public void onLongPress(MotionEvent e) {
                    View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                    if (child != null && clickListener != null) {
                        clickListener.onLongClick(child, recyclerView.getChildLayoutPosition(child));
                    }
                }
            });
        }

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

            View child = rv.findChildViewUnder(e.getX(), e.getY());
            if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
                clickListener.onClick(child, rv.getChildLayoutPosition(child));
            }
            return false;
        }

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

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }
    }

和KaHOIRecyclerViewItemClickListener接口

public interface KaHOIRecyclerViewItemClickListener {
    void onClick(View view, int position);

    void onLongClick(View view, int position);
}

但这突出了多个位置。请帮帮我

0 个答案:

没有答案