Sun
Sun

Reputation: 6888

RecyclerView facing small Issues

I am using RecyclerView in my program, and facing some small issues, I believe some of you already faced and resolved all of them..

  1. Separator color: Using DividerItemDecoration.java to show separator between Items, but don't know where to make change if i want to change separator line color (like : default to white color)

  2. Clickable Row: click only works when i do tap on text in a row, but i want to allow user to click anywhere in a row (like: listview) following this link

  3. Wrap RecyclerView : I have only two records in a recyclerview but its consuming full height (I want to wrap it based on records in a RecyclerView, like : listview)

  4. Ripple Effect: Whenever i do tap on any of the item in a RecyclerView not getting RippleEffect whereas when i do click on list item getting Ripple Effect

  5. RecyclerView Item Animation: How can i achieve animation when i do tap on any of the 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);

        .......

}

Upvotes: 4

Views: 2109

Answers (2)

Davideas
Davideas

Reputation: 3276

As alternative solution to all points (but 4), recently I've improved and created a FlexibleAdapter pattern for all RecyclerView. Very simple to use, just copy 2 classes in your common files + some xml in order to enable the selection (Single/Multi) as it was for ListView.

Please have a look at the description and full working example: https://github.com/davideas/FlexibleAdapter

For the point 4 the reason because you should give up it's because it is very complex and that solution (that works wrapping) could have incredible impact like loops while you combine it with selection and notifyXXX(). Strange to say but when I used it I started to have troubles and slowness while scrolling. So i removed and now I use the default one: LinearLayoutManager.

For last point (5), here there are many fancy animations: http://toastdroid.com/2014/09/03/unlocking-recyclerview/

Upvotes: 0

r0adkll
r0adkll

Reputation: 549

1.

First, create a drawable xml file that will represent the divider like this:

<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 that divider to your DividerItemDecoration like this:

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

2.

Without seeing how you are inflating your ViewHolder I don't know what mRootView really represents, but here is the approach that I typically take. I would create the item view much like you did, but with a couple changes:

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

Then in your onBindViewHolder(...) method you're going to want to set the OnClickListener to the itemView object in the holder like so:

@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);
        }
    });
}

This should let you use the entire cell as a clickable area + the ripple effect.

3.

Making the RecyclerView wrap its content vertically is a little tricky. You have to subclass the LinearLayoutManager and override it's onMeasure(...) method to force it to wrap it's content. Refer to this post here: https://stackoverflow.com/a/27616854/1623828

4.

See my answer to #2. You have to give the background (or foreground) of the view you're setting the OnClickListener to the value ?attr/selectableItemBackground or ?attr/selectableItemBackgroundBorderless if you want the ripple to go beyond the bounds of the view. This value will use the ink ripple selector when on 5.0+ and will default to your standard no ripple selector on any platform that is lower. You can however create your own custom ripple drawable, but that is a little more involved.

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

5.

What kind of animations? Like, RecyclerView item animations? For those you can create your own animations by subclassing ItemAnimator and applying it by using this method:

mRecyclerView.setItemAnimator(...)

or you can use the default implementation:

mRecyclerView.setItemAnimator(new DefaultItemAnimator())

Otherwise do you mean animating between Activities using Activity Transitions? If so you can follow the documentation here to setup Activity Transitions, however they will only work on Android 5.0+

Upvotes: 6

Related Questions