RecyclerView垂直列表显示示例,显示图片加文字。
1、RecyclerView.Adapter适配器
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
private Context mContext;
private List<TitleBean> titleBeans;
public ListAdapter() {
titleBeans = new ArrayList<>();
}
public void setTitleBeans(List<TitleBean> titleBeans) {
this.titleBeans = titleBeans;
}
@Override
public void onViewRecycled(ViewHolder holder){
holder.itemView.setOnLongClickListener(null);
super.onViewRecycled(holder);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mContext = parent.getContext();
View view = LayoutInflater.from(mContext).inflate(R.layout.item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final TitleBean note = titleBeans.get(position);
holder.itemView.setTag(note);
holder.imageView2.setImageResource(note.getId());
holder.textView2.setText(note.getContent());
}
@Override
public int getItemCount() {
//Log.i(TAG, "###getItemCount: ");
if (titleBeans != null && titleBeans.size()>0){
return titleBeans.size();
}
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView2;
public TextView textView2;
public ViewHolder(View view){
super(view);
imageView2 = view.findViewById(R.id.imageView2);
textView2 = view.findViewById(R.id.textView2);
}
}
}
item布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_main_note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView2"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_marginLeft="25dp"
android:src="@drawable/imageview_style" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:text="极速减脂体验课"
android:textColor="#102611"
android:textSize="22sp" />
</LinearLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
2、recycleview布局xml
<?xml version="1.0" encoding="utf-8"?>
<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.example.mmsx.HomeFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
3、recycleview装载数据
public class MessageFragment extends Fragment { private List<TitleBean> list = new ArrayList<>(); public MessageFragment() { list.add(new TitleBean(R.mipmap.banner1,"青菜")); list.add(new TitleBean(R.mipmap.banner2,"番薯")); list.add(new TitleBean(R.mipmap.banner3,"大米")); list.add(new TitleBean(R.mipmap.banner4,"稻谷")); list.add(new TitleBean(R.mipmap.banner1,"青菜")); list.add(new TitleBean(R.mipmap.banner2,"番薯")); list.add(new TitleBean(R.mipmap.banner3,"大米")); list.add(new TitleBean(R.mipmap.banner4,"稻谷")); } public static MessageFragment newInstance() { MessageFragment fragment = new MessageFragment(); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_message, container, false); RecyclerView recyclerView = view.findViewById(R.id.recyclerView); LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); layoutManager.setOrientation(LinearLayoutManager.VERTICAL);//竖向列表 recyclerView.setLayoutManager(layoutManager); ListAdapter adapter = new ListAdapter(); adapter.setTitleBeans(list); recyclerView.setAdapter(adapter); return view; } }