如何将 Android View Binding with RecyclerView 一起使用

2022-09-01 18:36:50

这不是数据绑定,这是视图绑定,这是Android Studio 3.6 Canary 11 +的新功能,如下所述。

对于在活动内部使用,很明显,您只需像这样使用它:

假设我们有一个名为activity_main.xml

然后在代码中,我们可以像这样使用它:

public class MainActivity extends Activity{

    ActivityMainBinding binding; //Name of the layout in camel case + "Binding"

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
    }
}

但是,不清楚如何将视图绑定与 RecyclerView 一起使用?

编辑:请用Java解释。


答案 1

假设我们里面有一个 RecyclerView,它的 id 是activity_main.xml"@+id/rv_test"

在活动内部,我们可以像这样使用它:

public class MainActivity extends Activity{

    ActivityMainBinding binding; //Name of the layout in camel case + "Binding"

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        binding.rvTest.setLayoutManager(layoutManager);
    }
}

让我们为 Recycler 的布局命名查看项目test_list_item.xml

然后,我们可以在活动内部实现我们的适配器,如下所示:

public class MainActivity extends Activity{

    ActivityMainBinding binding; //Name of the layout in camel case + "Binding"

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        binding.rvTest.setLayoutManager(layoutManager);

        List<String> items = Arrays.asList("item", "item", "item");

        binding.rvTest.setAdapter(new MyAdapter(items));
    }

    private class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{

        private List<String> items;

        private class MyViewHolder extends RecyclerView.ViewHolder{

            TestListItemBinding binding;//Name of the test_list_item.xml in camel case + "Binding"

            public MyViewHolder(TestListItemBinding b){
                super(b.getRoot());
                binding = b;
            }
        }

        public MyAdapter(List<String> items){
            this.items = items;
        }

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
            return new MyViewHolder(TestListItemBinding.inflate(getLayoutInflater()));
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position){
            String text = String.format(Locale.ENGLISH, "%s %d", items.get(position), position);

            //An example of how to use the bindings
            holder.binding.tvTest.setText(text);
        }

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

}

答案 2
class LanguageAdapter(val context: Context, var listData: MutableList<ListData>, val listener: onCheckListner) : RecyclerView.Adapter<LanguageAdapter.ViewHolder>() {

lateinit var bindind: LangugaeItemBinding

fun onRefresh(listData: MutableList<ListData>) {
    this.listData = listData
    notifyDataSetChanged()
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    bindind = LangugaeItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)

    return ViewHolder(bindind)
}

override fun getItemCount(): Int {
    return listData.size
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.setData(listData[position])

}

inner class ViewHolder(private val binding: LangugaeItemBinding) : RecyclerView.ViewHolder(binding.getRoot()), View.OnClickListener {
    override fun onClick(v: View?) {
        when (v) {
            itemView -> {

            }
        }
    }

    init {
        itemView.radio_button.setOnClickListener(this)
    }

    fun setData(model: ListData) {
        with(binding) {
            data = model
            executePendingBindings()
        }
    }

}
}

只需将模型或数组列表传递到适配器中并在绑定视图中设置


推荐