Custom layout file item.xml <? The 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="30dp"> <TextView android:id="@+id/tv_item" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="25dp" android:background="#fb2e" android:gravity="center"/> </LinearLayout>Copy the code
Listview Fragment of the display mode. xml <LinearLayout 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" android:orientation="vertical" tools:context="com.work.work2application.ListFragment" android:background="#faeb"> <! -- TODO: Update blank fragment layout --> <android.support.v7.widget.RecyclerView android:id="@+id/rv_list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#feb"> < / android. Support. V7. Widget. RecyclerView > < / LinearLayout > the GridView display fragments. xml <FrameLayout 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" tools:context="com.work.work2application.GridFragment"> <! -- TODO: Update blank fragment layout --> <android.support.v7.widget.RecyclerView android:id="@+id/rv_grid" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#febc"> < / android. Support. V7. Widget. RecyclerView > < / FrameLayout > waterfalls flow display fragments. xml <FrameLayout 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" tools:context="com.work.work2application.PbFragment"> <! -- TODO: Update blank fragment layout --> <android.support.v7.widget.RecyclerView android:id="@+id/rv_pb" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f2eb"> < / android. Support. V7. Widget. RecyclerView > < / FrameLayout > acvititymain. xml <? The 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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.work.work2application.MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="#faeb"> </android.support.v4.view.ViewPager> </RelativeLayout>Copy the code
The ListView. Java public class ListFragment extends Fragment { private RecyclerView rv_list; private String[] str; public ListFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view=inflater.inflate(R.layout.fragment_list, container, false); rv_list= (RecyclerView) view.findViewById(R.id.rv_list); intiAdapter(); return view; } public void intiAdapter(){ str=new String[10]; for (int i = 0; i < 10; I ++) {STR [I]="Android software developer "; } RecycleAdapter radapter=new RecycleAdapter(str,rv_list.getContext()); rv_list.setLayoutManager(new LinearLayoutManager(rv_list.getContext())); rv_list.setAdapter(radapter); rv_list.addItemDecoration(new DividerItemDecoration(rv_list.getContext(),LinearLayoutManager.VERTICAL)); } the GridView. Java public class GridFragment extends Fragment { private RecyclerView rv_grid; private String[] str; public GridFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_grid, container, false); rv_grid= (RecyclerView) view.findViewById(R.id.rv_grid); intiAdapter(); return view; } public void intiAdapter() { str = new String[10]; for (int i = 0; i < 10; I ++) {STR [I] = "Android software developer "; } RecycleAdapter radapter = new RecycleAdapter(str, rv_grid.getContext()); rv_grid.setLayoutManager(new GridLayoutManager(rv_grid.getContext(), 2)); rv_grid.setAdapter(radapter); } StaggeredGrid. Java public class PbFragment extends Fragment { private RecyclerView rv_pb; private String[] str; public PbFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_pb, container, false); rv_pb= (RecyclerView) view.findViewById(R.id.rv_pb); intiAdapter(); return view; } public void intiAdapter(){ str=new String[10]; for (int i = 0; i < 10; I ++) {STR [I]="Android software developer "; } RecycleAdapter radapter=new RecycleAdapter(str,rv_pb.getContext()); rv_pb.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); rv_pb.setAdapter(radapter); } MainAcvitity. Java public class MainActivity extends AppCompatActivity { private ViewPager vp; private List<Fragment> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vp= (ViewPager) findViewById(R.id.vp); list=new ArrayList<>(); list.add(new com.work.work2application.ListFragment()); list.add(new GridFragment()); list.add(new PbFragment()); FragmentAdapter adapter=new FragmentAdapter(getSupportFragmentManager(),list); vp.setAdapter(adapter); }Copy the code
Customize the Adapter. Java public RecycleAdapter(String[] str, Context context) { this.str = str; this.context = context; inflater=LayoutInflater.from(context); } @Override public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view=inflater.inflate(R.layout.item_layout,parent,false); MyHolder holder=new MyHolder(view); return new MyHolder(view); } @Override public void onBindViewHolder(MyHolder holder, int position) { holder.tv_item.setText(str[position]); holder.tv_item.setTag(position); // // give waterfall flow this is a random height between 100 and 400 // int height= (int) (100+ math.random ()*300); // ViewGroup.LayoutParams params=holder.tv_item.getLayoutParams(); // holder.tv_item.setLayoutParams(params); // holder.tv_item.setText(str[position]); } @Override public int getItemCount() { return str.length; } class MyHolder extends RecyclerView.ViewHolder{ private TextView tv_item; public MyHolder(View itemView) { super(itemView); tv_item= (TextView) itemView.findViewById(R.id.tv_item); }}}Copy the code