recycleView在界面展示上类似于ListView,但不同于ListView的是它展示的每一个item类型可以不一样:
RecycleAdapter类:
public class RecycleAdapter extends RecyclerView.Adapter<ViewHolder> {
private List<Bean> beans;
public RecycleAdapter(List<Bean> beans) {
super();
this.beans = beans;
}
//不同类型写不同的ViewHolder
public class TextHolder extends RecyclerView.ViewHolder {
public TextView textView;
public TextHolder(View textView) {
super(textView);
this.textView = (TextView) textView.findViewById(R.id.mytext);
}
}
public class ButtonHolder extends RecyclerView.ViewHolder {
public Button button;
public ButtonHolder(View textview) {
super(textview);
this.button = (Button) textview.findViewById(R.id.mybutton);
}
}
public class ImageHoler extends RecyclerView.ViewHolder {
public ImageView Imageview;
public ImageHoler(View textview) {
super(textview);
this.Imageview = (ImageView) textview.findViewById(R.id.myiamge);
}
}
@Override
public int getItemCount() {
return beans.size();
}
@Override
public int getItemViewType(int position) {
return beans.get(position).getType();//获取当前Position的type
};
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
switch (getItemViewType(position)) {
case Bean.X_TYPE://针对不同类型的view绑定不同的Holder
TextHolder textHolder = (TextHolder) holder;
textHolder.textView.setText(beans.get(position).getText());
break;
case Bean.Y_TYPE:
ButtonHolder buttonHolder = (ButtonHolder) holder;
buttonHolder.button.setText(beans.get(position).getText());
break;
case Bean.Z_TYPE:
ImageHoler imageHoler = (ImageHoler) holder;
imageHoler.Imageview.setImageResource(android.R.drawable.checkbox_off_background);
break;
default:
break;
}
}
@SuppressLint("InflateParams")
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewtype) {
View v = null;
ViewHolder holder = null;
switch (viewtype) {
case Bean.X_TYPE://针对不同的View类型,inflater不同的布局
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recylce_item_x, null);
holder = new TextHolder(v);
break;
case Bean.Y_TYPE:
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recylce_item_y, null);
holder = new ButtonHolder(v);
break;
case Bean.Z_TYPE:
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recylce_item_z, null);
holder = new ImageHoler(v);
break;
default:
break;
}
return holder;
}
}
bean类:
public class Bean {
public static final int Y_TYPE = 0;// view类型1
public static final int X_TYPE = 1;// view类型2
public static final int Z_TYPE = 2;// view类型3
private int type;
private String text;
public Bean(int type, String text) {
super();
this.type = type;
this.text = text;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
MainActivity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// 创建布局管理器
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
// 初始化数据
List<Bean> myDataset = new ArrayList<Bean>();
myDataset.add(new Bean(Bean.Z_TYPE, "图片"));
myDataset.add(new Bean(Bean.X_TYPE, "文字"));
myDataset.add(new Bean(Bean.Y_TYPE, "按钮"));
myDataset.add(new Bean(Bean.Z_TYPE, "图片"));
myDataset.add(new Bean(Bean.X_TYPE, "shit"));
myDataset.add(new Bean(Bean.X_TYPE, "我擦"));
myDataset.add(new Bean(Bean.Z_TYPE, "图片"));
myDataset.add(new Bean(Bean.Y_TYPE, "按钮"));
myDataset.add(new Bean(Bean.Y_TYPE, "按钮"));
myDataset.add(new Bean(Bean.X_TYPE, "文字"));
// 创建Adpater
RecycleAdapter mAdapter = new RecycleAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
}
}
activity_main的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
其余的布局省略,都是简单的几个textview imageview
来源:https://www.cnblogs.com/yuanting/p/5038222.html