如何为recyclerView创建向下箭头指示器所选项目?

时间:2018-01-08 08:27:48

标签: android android-recyclerview

我想用向下箭头指示recyclerView所选项目。我附上截图供参考。请任何人给我一些解决方案。

Xml代码:

Recyclerview:

<android.support.v7.widget.RecyclerView
                android:id="@+id/doctor_list_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                android:orientation="horizontal"
                app:layoutManager="android.support.v7.widget.LinearLayoutManager"
                app:layout_constraintLeft_toLeftOf="parent" />

Recyclerview项目:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical">

    <android.support.constraint.ConstraintLayout
        android:layout_width="@dimen/margin_110dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="@dimen/margin_12dp">

        <com.mikhaellopez.circularimageview.CircularImageView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/user_profile_image_view"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/default_user_image"
            app:civ_border_color="@color/color_blue_"
            app:civ_border_width="1dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/text_view_doctor_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_6dp"
            android:ellipsize="end"
            android:fontFamily="@font/roboto_regular"
            android:maxLines="1"
            android:singleLine="true"
            android:textColor="@color/color_circle_stroke"
            android:textSize="@dimen/text_size_14dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/user_profile_image_view" />

        <TextView
            android:id="@+id/text_view_doctor_speciality"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_4dp"
            android:fontFamily="@font/roboto_regular"
            android:textColor="@color/color_circle_stroke"
            android:textSize="@dimen/text_size_14dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/text_view_doctor_name" />

    </android.support.constraint.ConstraintLayout>

</LinearLayout>

enter image description here

应使用向下箭头指示器突出显示所选项目。我不知道如何创建该指标以突出显示所选项目。

2 个答案:

答案 0 :(得分:0)

您可以添加箭头并将其可见性设置为不可见,在您的on绑定视图持有者创建条件以检查布尔值,在该位置设置其可见性,单击按钮获取位置并将布尔值设置为true并且调用notifyItemChanged(position)。如果您需要代码段,请告诉我们!

答案 1 :(得分:0)

您可以使用RecyclerView.ItemDecoration。

yourRecyclerView.addItemDecoration(arrowDecoration);

class ArrowDecoration extends RecyclerView.ItemDecoration {

   @Override
   public void getItemOffsets(Rect outRect, View view, RecyclerView parent, 
                               State state){
       // this function can add padding like effect
       super.getItemOffsets(outRect, view, parent, state);
       outRect.bottom = 20; // maybe arrow height
   }

   @Override
   public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)
   {
       // this function draw something under the content, if your want to draw
       // something over your content, please override onDrawOver
       int childCount = parent.getChildCount();
       for (int i = 0; i < childCount; i++) {
           View child = parent.getChildAt(i);
           int pos = parent.getChildAdapterPosition(child);
           YourData data = YourAdapter.getItem(pos);
           // calculate your arrow visible and position
           c.drawXXX(...);
       }
   }

}

只需一些演示代码,请按照您的要求进行。