Add Recyclerview to RecyclerViewPager

I added Recyclerview to a single RecyclerViewPager element ( https://github.com/lsjwzh/RecyclerViewPager ). And I want to scroll through the Recyclerview when I touch it.

I tried:

  View.OnTouchListener listener = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_DOWN: mRecyclerViewPager.requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: mRecyclerViewPager.requestDisallowInterceptTouchEvent(false); break; } return false; } }; mRecyclerView.setOnTouchListener(listener); 

But sometimes I can scroll through the RecyclerView.

I think it can be ported by implementing the NestedScrollingParent in the RecyclerViewPager or changing the onTouchEvent in the RecyclerViewPager . But I am not familiar with them.

enter image description here

+7
android-recyclerview android-nestedscrollview
source share
1 answer

I followed the steps to set up and create a simple example using the Github documentation.

XML core business

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.stackoverflow.recyclerviewstack.MainActivity"> <com.lsjwzh.widget.recyclerviewpager.RecyclerViewPager android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="1dp" android:paddingRight="1dp" app:rvp_triggerOffset="0.1" app:rvp_singlePageFling="true" android:clipToPadding="false" /> </RelativeLayout> 

Main class Activiy

 package com.stackoverflow.recyclerviewstack; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import com.lsjwzh.widget.recyclerviewpager.RecyclerViewPager; public class MainActivity extends AppCompatActivity { RecyclerAdapter mAdapter; RecyclerViewPager mRecycler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecycler = (RecyclerViewPager) findViewById(R.id.list); // setLayoutManager like normal RecyclerView, you do not need to change any thing. LinearLayoutManager layout = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); mRecycler.setLayoutManager(layout); //set adapter //You just need to implements ViewPageAdapter by yourself like a normal RecyclerView.Adpater. mAdapter = new RecyclerAdapter(ItemListGenerator.generateCollection(15, "OutItem ")); mRecycler.setAdapter(mAdapter); } } 

RecyclerAdapter

 public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> { List<String> collection; public RecyclerAdapter(List<String> collection) { this.collection = collection; } public class ViewHolder extends RecyclerView.ViewHolder { public TextView item; RecyclerView mInnerRecycler; public ViewHolder(View view) { super(view); item = (TextView) view.findViewById(R.id.title); mInnerRecycler = (RecyclerView) view.findViewById(R.id.innerRecycler); LinearLayoutManager layout = new LinearLayoutManager(view.getContext(), LinearLayoutManager.HORIZONTAL, false); mInnerRecycler.setLayoutManager(layout); mInnerRecycler.setAdapter(new InnerAdapter()); } } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { Context context = parent.getContext(); LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.outer_collection, parent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { if(position < 0 || position > getItemCount()) return; String itemString = collection.get(position); holder.item.setText(itemString); } @Override public int getItemCount() { return collection.size(); } } 

using layout with RecyclerViewPager to create a ViewHolder

ViewHolder uses the External_collection layout:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dip" android:layout_margin="2dip" android:background="@color/colorPrimary"/> <android.support.v7.widget.RecyclerView android:id="@+id/innerRecycler" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> 

Internal adapter

 public class InnerAdapter extends RecyclerView.Adapter<InnerAdapter.ViewHolder> { List<String> collection; public InnerAdapter() { this.collection = ItemListGenerator.generateCollection(15, "Inner "); } public class ViewHolder extends RecyclerView.ViewHolder { public TextView item; public ViewHolder(View itemView) { super(itemView); item = (TextView) itemView.findViewById(R.id.item); } } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { Context context = parent.getContext(); LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.simple_item, parent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { if(position < 0 || position > getItemCount()) return; holder.item.setText(collection.get(position)); } @Override public int getItemCount() { return collection.size(); } } 

Tip

For RecyclerViewPager, I used this orientation:

  LinearLayoutManager layout = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); 

And for the RecyclerView inside this:

 LinearLayoutManager layout = new LinearLayoutManager(view.getContext(), LinearLayoutManager.HORIZONTAL, false); 

If you use VERTICAL in a PagerView, you can move in a horizontal collection or change the orientation of PORPOROS HORIZONTAL, and you can scroll your internal elements in the VERTICAL orientation.

You must evaluate how you want to use it. Hope this code helps solve your problem and also rethink your design. This may not be a good practice, or the UX use the same orientation for both containers. I am not a UX expert.

I would like to be more helpful.

+2
source share

All Articles