RecyclerView is facing minor issues

I use RecyclerViewin my program and encounter some small problems, I believe that some of you have already encountered and solved all of them.

  • Separator color: Using DividerItemDecoration.java to show the separator between elements, but don’t know where to make changes if I want to change the color of the separator line (for example: the default color is white)

  • Clickable Row: click only works when I click on text in a line, but I want to allow the user to click anywhere on the line (ex: listview) by following this link

  • Wrap RecyclerView: I have only two entries in recyclerview, but it consumes the full height (I want to wrap it based on entries in RecyclerView, for example: listview)

  • Ripple Effect: whenever I click on any item in a RecyclerView without getting a RippleEffect, whereas when I click on a list item, getting a ripple effect

  • RecyclerView Item Animation: How can I achieve animation when I click on any item in RecyclerView

fragment_main.xml: -

<android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_rounded"/>

adapter_main.xml: -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textTitle"
        style="@style/AppTheme.ListTextView"
        />

</LinearLayout>

styles.xml: -

<style name="AppTheme.ListTextView" parent="android:Widget.Material.TextView">
        <item name="android:gravity">left</item>
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>            
        <item name="android:padding">10dp</item>
    </style>

btn_rounded.xml: -

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#008f8471"/>
    <stroke android:width="2dp" android:color="#ffffff" />
    <corners android:radius="10dp"/>
</shape>

MainAdatper.java: -

@Override
public void onBindViewHolder(MainHolder holder, final int position) {
    .....
    holder.mRootView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String strTitle = list.get(position).getTitle().toString();

            Intent intent = new Intent(context, DetailActivity.class);
            intent.putExtra("title", strTitle);
            context.startActivity(intent);
        }
    });

MainFragment.xml: -

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_main, container, false);

        /* Initialize recycler view */
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));

        list = new ArrayList<MainPoho>();
        adapter = new MainAdapter(getActivity(), list);
        mRecyclerView.setAdapter(adapter);

        .......

}
+4
source share
2 answers

1.

First, create a flexible XML file that will represent the divider as follows:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="@color/white" /> <!-- or you could use hex i.e. #ffffff -->
    <size android:height="1dp" />
</shape>

Then set this separator to DividerItemDecorationas follows:

new DividerItemDecoration(getResources().getDrawable(R.drawable.recycler_divider))

2.

, ViewHolder, , mRootView, , . , , :

<?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:orientation="vertical"
    android:background="?attr/selectableItemBackground">
    <!-- This will give your recycler item the ink ripple effect and touch state -->

    <TextView
        android:id="@+id/textTitle"
        style="@style/AppTheme.ListTextView"
        />

</LinearLayout>

onBindViewHolder(...) OnClickListener itemView :

@Override
public void onBindViewHolder(MainHolder holder, final int position) {
    .....
    holder.itemView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String strTitle = list.get(position).getTitle().toString();

            /**
             * Here is where I would typically create a java interface to use as a
             * callback to the fragment/activity/whatever to take action rather
             * than keeping this code in the adapter
             */

            Intent intent = new Intent(context, DetailActivity.class);
            intent.putExtra("title", strTitle);
            context.startActivity(intent);
        }
    });
}

+ .

3.

RecyclerView . LinearLayoutManager onMeasure(...), . : fooobar.com/questions/38265/...

4.

. # 2. ( ) , OnClickListener, ?attr/selectableItemBackground ?attr/selectableItemBackgroundBorderless, , . , 5.0+, , . , , .

: http://developer.android.com/training/material/animations.html#Touch

5.

? , RecyclerView? , , ItemAnimator , :

mRecyclerView.setItemAnimator(...)

:

mRecyclerView.setItemAnimator(new DefaultItemAnimator())

, ? , , Activity Transitions, Android 5,0 +

+6

( 4), FlexibleAdapter RecyclerView. , 2 + xml, (Single/Multi), ListView.

, : https://github.com/davideas/FlexibleAdapter

4 , , ( ) , , , notifyXXX(). , , . , : LinearLayoutManager.

(5) : http://toastdroid.com/2014/09/03/unlocking-recyclerview/

0

All Articles