赞
踩
在页面上使用RecyclerView实现一个列表型信息展示:
步骤如下:
- <TextView
- android:id="@+id/txt_gnss_info"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="卫星信息"
- android:textColor="#0a0a0a" />
- <android.support.v7.widget.RecyclerView
- android:id="@+id/rv_gnss_country"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="20dp"
- />
layout_gnss_counrty_item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:orientation="horizontal"
- android:layout_height="wrap_content">
-
- <ImageView
- android:id="@+id/iv_item_flag"
- android:layout_width="24dp"
- android:layout_height="24dp"
- android:layout_gravity="center_vertical"
- android:layout_marginLeft="16dp"
- android:src="@drawable/flag_beidou_3"
- android:layout_centerVertical="true"
- />
-
- <TextView
- android:id="@+id/tv_item_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="GNSS国家名称"
- android:layout_centerVertical="true"
- android:textColor="@color/layer_name"
- android:layout_marginLeft="8dp"
- android:layout_gravity="center_vertical"/>
-
- </LinearLayout>
实体类:
- public class GnssCountryInfo {
- public int FlagId;
- public String TextContext;
-
- public GnssCountryInfo(int _id,String _str){
- FlagId = _id;
- TextContext = _str;
- }
- }
Adapter类和ViewHolder类:
- public class GnssCountryAdapter extends RecyclerView.Adapter<GnssCountryAdapter.GnssCountryViewHoder> {
-
- private Context context;
- private List<GnssCountryInfo> dataList;
-
- public GnssCountryAdapter(Context _context, List<GnssCountryInfo> _data){
- this.dataList = _data;
- this.context = _context;
- }
-
- @NonNull
- @Override
- public GnssCountryViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
- View view = View.inflate(context, R.layout.layout_gnss_counrty_item, null);
- GnssCountryViewHoder gnssCountryViewHoder = new GnssCountryViewHoder(view);
- return gnssCountryViewHoder;
- }
-
- @Override
- public void onBindViewHolder(@NonNull GnssCountryViewHoder holder, int position) {
- GnssCountryInfo itemData = dataList.get(position);
- holder.mCountryFlag.setImageResource(itemData.FlagId);
- holder.mTitleContent.setText(itemData.TextContext);
- }
-
- @Override
- public int getItemCount() {
- return dataList.size();
- }
-
-
- class GnssCountryViewHoder extends RecyclerView.ViewHolder {
- ImageView mCountryFlag;
- TextView mTitleContent;
-
- public GnssCountryViewHoder(@NonNull View itemView) {
- super(itemView);
- mCountryFlag = itemView.findViewById(R.id.iv_item_flag);
- mTitleContent = itemView.findViewById(R.id.tv_item_name);
- }
- }
-
- }
-
调用:
- GnssCountryAdapter adapter = new GnssCountryAdapter(this,dataList);
- mRecyclerView.setAdapter(adapter);
- LinearLayoutManager layoutManager = new LinearLayoutManager(this);
- mRecyclerView.setLayoutManager(layoutManager);
注意:
一定要设置:
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
否则会出错。
参考链接:
Android RecyclerView最全使用详解-CSDN博客
RecyclerView相关学习资料:
Android开发,使用RecyclerView实现商品列表_安卓recyclerview设备列表-CSDN博客
Android RecyclerView控件_recycleview版本-CSDN博客
RecyclerView之下拉刷新、下拉加载的实现_androidx.recyclerview.widget.recyclerview下拉刷新 底部加载-CSDN博客
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。