线性显示 类似于listview:
线性宫格显示 类似于grid view:
用线性宫格显示 类似于瀑布流:
结构图:
测试代码:
activity_main.xml:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <android.support.v7.widget.RecyclerView 7 android:id="@+id/recycler_view" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 android:layout_centerHorizontal="true" 11 android:layout_centerVertical="true" /> 12 13 </RelativeLayout>
MainActivity.java:
1 package com.zzw.testrecyclerview; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.support.v7.widget.GridLayoutManager; 6 import android.support.v7.widget.LinearLayoutManager; 7 import android.support.v7.widget.OrientationHelper; 8 import android.support.v7.widget.RecyclerView; 9 import android.support.v7.widget.RecyclerView.Adapter; 10 import android.support.v7.widget.StaggeredGridLayoutManager; 11 import android.widget.TextView; 12 13 public class MainActivity extends Activity { 14 15 RecyclerView mRecyclerView; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 String[] mTitles = new String[100]; 23 24 for (int i = 0; i < 99; i++) { 25 mTitles[i] = "测试数据" + i; 26 } 27 28 mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); 29 30 // 这里用线性显示 类似于listview 31 // mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 32 33 // 这里用线性宫格显示 类似于grid view 34 // mRecyclerView.setLayoutManager(new GridLayoutManager(this,3)); 35 36 // 这里用线性宫格显示 类似于瀑布流 37 mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, 38 OrientationHelper.VERTICAL)); 39 40 NormalRecyclerViewAdapter adapter = new NormalRecyclerViewAdapter(this, 41 android.R.layout.simple_list_item_1, mTitles); 42 43 mRecyclerView.setAdapter(adapter); 44 } 45 46 }
NormalRecyclerViewAdapter.java:
1 package com.zzw.testrecyclerview; 2 3 import android.content.Context; 4 import android.support.v7.widget.RecyclerView.Adapter; 5 import android.support.v7.widget.RecyclerView.ViewHolder; 6 import android.util.Log; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.view.ViewGroup; 11 import android.widget.TextView; 12 13 public class NormalRecyclerViewAdapter extends 14 Adapter<NormalRecyclerViewAdapter.NormalTextViewHolder> { 15 private final LayoutInflater mLayoutInflater; 16 private final Context mContext; 17 private String[] mTitles; 18 19 public NormalRecyclerViewAdapter(Context context, int item_id, 20 String[] Titles) { 21 mContext = context; 22 mTitles = Titles; 23 mLayoutInflater = LayoutInflater.from(context); 24 } 25 26 @Override 27 public int getItemCount() { 28 29 return mTitles == null ? 0 : mTitles.length; 30 } 31 32 @Override 33 public NormalTextViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) { 34 35 return new NormalTextViewHolder(mLayoutInflater.inflate( 36 android.R.layout.simple_list_item_1, null)); 37 } 38 39 @Override 40 public void onBindViewHolder(NormalTextViewHolder holder, int position) { 41 holder.mTextView.setText(mTitles[position]); 42 } 43 44 public static class NormalTextViewHolder extends ViewHolder { 45 46 TextView mTextView; 47 48 public NormalTextViewHolder(View view) { 49 super(view); 50 mTextView=(TextView) view.findViewById(android.R.id.text1); 51 view.setOnClickListener(new OnClickListener() { 52 53 @Override 54 public void onClick(View v) { 55 Log.d("NormalTextViewHolder", "onClick--> position = " 56 + getPosition()); 57 } 58 }); 59 } 60 } 61 }
相关问题解决:
使用android.support.v7.widget.RecyclerView出现 java.lang.reflect.InvocationTargetException:http://www.cnblogs.com/zzw1994/p/5004564.html