iT邦幫忙

2021 iThome 鐵人賽

DAY 16
0
Mobile Development

android studio 30天學習筆記系列 第 16

android studio 30天學習筆記-day 16-databinding Recyclerview

  • 分享至 

  • xImage
  •  

今天會使用databinding的方式去實作一個Recyclerview。

建立model

public class DBData {
    public final ObservableField<String> name=new ObservableField<>();
    public final ObservableField<String> sex=new ObservableField<>();
    public final ObservableField<Integer> age=new ObservableField<>();
}

設置item_data_list.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="data"
            type="com.example.newtest.datadindRecyclerviewTest.DBData" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/UserName"
                android:textSize="30sp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{data.name}"
                android:textSize="30sp"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/UserSex"
                android:textSize="30sp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{data.sex}"
                android:textSize="30sp"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/UserAge"
                android:textSize="30sp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{String.valueOf(data.age)}"
                android:textSize="30sp"/>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@color/colorPrimary"/>
    </LinearLayout>
</layout>

這裡會發現在data.age加了一個String.valueOf()data.age是int,如果不加String.valueOf()會報錯。

建立adapter

public class BindRecyclerViewAdapter extends RecyclerView.Adapter<BindRecyclerViewAdapter.ViewHolder> {
    AppCompatActivity activity;
    List<DBData> dBDataList;

    public BindRecyclerViewAdapter(AppCompatActivity activity,List<DBData> dBDataList){
        this.activity=activity;
        this.dBDataList=dBDataList;

    }

    @Override
    public ViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(activity);
        ItemDataListBinding binding=ItemDataListBinding.inflate(inflater,parent,false);

        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder( ViewHolder holder, int position) { 
        DBData dbData =dBDataList.get(position);
        holder.bind(dbData);
    }

    @Override
    public int getItemCount() {
        return dBDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ItemDataListBinding itemDataListBinding;
            public ViewHolder(ItemDataListBinding binding) {
            super(binding.getRoot());
                itemDataListBinding=binding;
        }
        void bind(DBData dbData){
            itemDataListBinding.setData(dbData);
            itemDataListBinding.executePendingBindings();
        }
    }
}

將ViewHolder的參數從view改成ItemDataListBinding,onBindViewHolder將原本用setText的方式顯示UI改成bind(dbData),而在bind(dbData)記得要加executePendingBindings()executePendingBindings()會立即更新UI。

MainActivity

設定要傳入的資料

    private void init() {
        recyclerView=findViewById(R.id.recyclerview1);
        for (int i=1;i<11;i++){
            DBData dBdata=new DBData();
            dBdata.name.set("eles"+i);
            dBdata.sex.set("boy");
            dBdata.age.set(i+15);
            dBDataList.add(dBdata);

        }
        bindRecyclerViewAdapter =new BindRecyclerViewAdapter(this,dBDataList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(bindRecyclerViewAdapter);
    }

MainActivity完整程式碼

public class MainActivity3 extends AppCompatActivity {
    BindRecyclerViewAdapter bindRecyclerViewAdapter;
    RecyclerView recyclerView;
    ArrayList<DBData> dBDataList=new ArrayList<>();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        init();
    }

    private void init() {
        recyclerView=findViewById(R.id.recyclerview1);
        for (int i=1;i<11;i++){
            DBData dBdata=new DBData();
            dBdata.name.set("eles"+i);
            dBdata.sex.set("boy");
            dBdata.age.set(i+15);
            dBDataList.add(dBdata);

        }
        bindRecyclerViewAdapter =new BindRecyclerViewAdapter(this,dBDataList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(bindRecyclerViewAdapter);
    }
}

上一篇
android studio 30天學習筆記-day 15-databinding 雙向綁定
下一篇
android studio 30天學習筆記-day 17-TabLayout+TabItem
系列文
android studio 30天學習筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言