MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button list,grid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initListener();
}
private void initView() {
list=(Button)findViewById(R.id.btn_list);
grid=(Button)findViewById(R.id.btn_grid);
}
private void initListener() {
list.setOnClickListener(this);
grid.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_list:
Intent intent=new Intent(MainActivity.this,RecyclerViewActivity.class);
intent.putExtra("type",1);
startActivity(intent);
break;
case R.id.btn_grid:
Intent intent2=new Intent(MainActivity.this,RecyclerViewActivity.class);
intent2.putExtra("type",2);
startActivity(intent2);
break;
}
}
}
RecyclerViewActivity
public class RecyclerViewActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private String[] title = {"Blog : http://blog.csdn.net/Leejizhou.",
"A good laugh and a long sleep are the best cures in the doctor's book.",
"all or nothing, now or never ",
"Be nice to people on the way up, because you'll need them on your way down.",
"Be confident with yourself and stop worrying what other people think. Do what's best for your future happiness!",
"Blessed is he whose fame does not outshine his truth.",
"Create good memories today, so that you can have a good past"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclerview);
mRecyclerView = (RecyclerView) findViewById(R.id.rv_list);
if (getIntent().getIntExtra("type", 0) == 1) {
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
} else if (getIntent().getIntExtra("type", 0) == 2) {
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
}
mRecyclerView.setAdapter(new RecyclerViewAdapter(this, title));
}
}
RecyclerViewAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private LayoutInflater mLayoutInflater;
private Context context;
private String [] titles;
//建立枚举 2个item 类型
public enum ITEM_TYPE {
ITEM1,
ITEM2
}
public RecyclerViewAdapter(Context context,String[] titles){
this.titles = titles;
this.context = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//加载Item View的时候根据不同TYPE加载不同的布局
if (viewType == ITEM_TYPE.ITEM1.ordinal()) {
return new Item1ViewHolder(mLayoutInflater.inflate(R.layout.item1, parent, false));
} else {
return new Item2ViewHolder(mLayoutInflater.inflate(R.layout.item2, parent, false));
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof Item1ViewHolder) {
((Item1ViewHolder) holder).mTextView.setText(titles[position]);
} else if (holder instanceof Item2ViewHolder) {
((Item2ViewHolder) holder).mTextView.setText(titles[position]);
}
}
//设置ITEM类型,可以自由发挥,这里设置item position单数显示item1 偶数显示item2
@Override
public int getItemViewType(int position) {
//Enum类提供了一个ordinal()方法,返回枚举类型的序数,这里ITEM_TYPE.ITEM1.ordinal()代表0, ITEM_TYPE.ITEM2.ordinal()代表1
return position % 2 == 0 ? ITEM_TYPE.ITEM1.ordinal() : ITEM_TYPE.ITEM2.ordinal();
}
@Override
public int getItemCount() {
return titles == null ? 0 : titles.length;
}
//item1 的ViewHolder
public static class Item1ViewHolder extends RecyclerView.ViewHolder{
TextView mTextView;
public Item1ViewHolder(View itemView) {
super(itemView);
mTextView=(TextView)itemView.findViewById(R.id.tv_item1_text);
}
}
//item2 的ViewHolder
public static class Item2ViewHolder extends RecyclerView.ViewHolder{
TextView mTextView;
public Item2ViewHolder(View itemView) {
super(itemView);
mTextView=(TextView)itemView.findViewById(R.id.tv_item2_text);
}
}
}
activity_main.xml
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.bwie.recycleview_testdemo.activity.MainActivity">
<Button
android:id="@+id/btn_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="List" />
<Button
android:id="@+id/btn_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Grid" />
LinearLayout>
activity_recyclerview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
LinearLayout>
item1.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cv_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardBackgroundColor="#ffffff"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_item1_pic"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_weight="1"
android:background="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_item1_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp" />
LinearLayout>
android.support.v7.widget.CardView>
item2.xml
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="4dp"
card_view:cardBackgroundColor="#E040FB"
card_view:cardElevation="4dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_item2_text"
android:padding="20dp"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
LinearLayout>
android.support.v7.widget.CardView>