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);
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.