1

I have a nested scrollview as parent and 2 recyclerviews as its children. What my issue is the recycler view draws it's children at one shot instead of drawing on scroll. How can I prevent this. I read that if we add

 android:nestedScrollingEnabled="false"

property this issue comes. But I added this property to make the scroll smoother. Given below is my xml file.

 <android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_fafafa"
        android:orientation="vertical">
      <android.support.v7.widget.RecyclerView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                android:nestedScrollingEnabled="false"
                android:paddingLeft="@dimen/dp_5"
                android:paddingRight="@dimen/dp_5"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </LinearLayout>
 </android.support.v4.widget.NestedScrollView>

Can anybody tell me how can I solve this issue?

andro-girl
  • 7,989
  • 22
  • 71
  • 94

1 Answers1

0

If you only have 2 RecyclerViews as children I would suggest removing one and and using RecyclerViews ViewType.

Use an Adapter like this:

public class ExampleAdapter extends RecyclerView.Adapter<BindableViewHolder> {

    private static final int VIEW_TYPE_CLASS_A = 0;
    private static final int VIEW_TYPE_CLASS_B = 1;

    private List<ClassA> class_a_list;
    private List<ClassB> class_b_list;

    public ExampleAdapter(List<ClassA> class_a_list, List<ClassB> class_b_list) {
        this.class_a_list = class_a_list;
        this.class_b_list = class_b_list;
    }

    @Override
    public BindableViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case VIEW_TYPE_CLASS_A:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_class_a, parent, false);
                return new ClassAHolder(view);
            default:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_class_b, parent, false);
                return new ClassBHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(BindableViewHolder holder, int position) {
        if(position < class_a_list.size()) {
            ((ClassAHolder) holder).bind(class_a_list.get(position));
        } else {
            ((ClassBHolder) holder).bind(class_b_list.get(position - class_a_list.size()));
        }
    }

    @Override
    public int getItemCount() {
        return class_a_list.size() + class_b_list.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(position < class_a_list.size()) {
            return VIEW_TYPE_CLASS_A;
        } else {
            return VIEW_TYPE_CLASS_B;
        }
    }
}

With the help of getItemViewType(int position) you determine which kind of View there should be in the specific position.

Then you can use ViewHolders like these:

public abstract class BindableViewHolder<T> extends RecyclerView.ViewHolder {

    public BindableViewHolder(View itemView) {
        super(itemView);
    }

    public abstract void bind(T data);
}

public class ClassAHolder extends BindableViewHolder<ClassA> {

    public ClassAHolder(View itemView) {
        super(itemView);
    }

    @Override
    public void bind(ClassA data) {
        // populate your views
    }
}


public class ClassBHolder extends BindableViewHolder<ClassB> {

    public ClassBHolder(View itemView) {
        super(itemView);
    }

    @Override
    public void bind(ClassB data) {
        // populate your views
    }
}
Damian Jäger
  • 204
  • 2
  • 12
  • I have other controls as well. And there are collapsing and expanding logics need to be handled. Moreover i am using grid layout manager. currently i am handling the layout span by hardcoding the position of the particular view type. If i combine recyclerviews i have to change the grid span logic dynamically. Thats why opted this approach – andro-girl Feb 16 '17 at 05:42