Simple use of viewBinding in RecyclerView

Keywords: Android Gradle xml Java

Preface

Let's start by saying that my compiler version is as4.0 beta2.
gradle:4.0.0-beta02, corresponding to gradle-6.2.2-all.zip.

As for the role of viewing, I directly quote the words on the android Developer website:
With view binding, you can write code that interacts with views more easily. When view binding is enabled in a module, a binding class is generated for each XML layout file in the module. An instance of the binding class contains a direct reference to all views that have ID S in the corresponding layout.
In most cases, the view binding overrides findViewById.

Open view binding

After the as version and gradle version meet the conditions at the same time, you can set the startup in build.gradle. Note: I don't know whether the version I used has changed, whether the opening method is different from the version 3.6 found on the Internet, or whether it will change in the future. Please refer to the latest version

android {

    buildFeatures {
        viewBinding = true
    }
}

Simple use in recyclerview

Here are just a few parts that are slightly different
change_class.java

public class change_class extends Activity {
    private ChangeLayoutBinding Mbinding;
    private ArrayList<MyClass> myClass;
    private RecyclerView recyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager layoutManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Mbinding=ChangeLayoutBinding.inflate(this.getLayoutInflater());
        setContentView(Mbinding.getRoot());
        recyclerView=Mbinding.Rview;//This is the id
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
 
    	//Omit extraneous content
        //ChangeAdapter adapter = new ChangeAdapter(one);
        recyclerView.setAdapter(adapter);
        show();
    }
    boolean show(){
       //Get the viewholder of the first item
        RecyclerView.ViewHolder veve=recyclerView.findViewHolderForAdapterPosition(0);
        //The bind method of viewbinding is used here to pass in the view of viewholder
        OneLayoutBinding one=OneLayoutBinding.bind(veve.itemView);
        //Get the text content through viewbinding and print it
        System.out.println(one.name.getText().toString());
        
        return true;

    }
}

layout/change_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cback"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/Rview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_marginTop="60dp" />
</RelativeLayout>

ChangeAdapter.java

public class ChangeAdapter extends RecyclerView.Adapter<ChangeAdapter.ViewHolder> {
    
    ArrayList<Integer> start;
    ArrayList<Integer> end;
    MyClass one;
    public ChangeAdapter(MyClass in){
        start=new ArrayList<>();
        end=new ArrayList<>();
        one=in;
        //Omit it, and show it in general
        }

    }
    @NonNull
    @Override
    public ChangeAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        OneLayoutBinding one=OneLayoutBinding.inflate(LayoutInflater.from(parent.getContext()));
        //Conventional writing
        //View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.one_layout,parent,false);
        ViewHolder my=new ViewHolder(one);

        return my;

    }

    @Override
    public void onBindViewHolder(@NonNull ChangeAdapter.ViewHolder holder, int position) {

        if(position==start.size()){
            holder.one.name.setVisibility(View.GONE);
            holder.one.room.setVisibility(View.GONE);
            holder.one.teacher.setVisibility(View.GONE);
         
            holder.one.addClass.setVisibility(View.VISIBLE);
            holder.one.addClass.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    start.add(111-start.size());
                    end.add(111-start.size());
                    notifyItemInserted(start.size()-1);
                }
            });
            return;
        }
        holder.one.name.setText(one.getname());
        holder.one.teacher.setText(one.getteacher());
        holder.one.room.setText(one.getroom());
    }

    @Override
    public int getItemCount() {
        return start.size()+1;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        OneLayoutBinding one;
        public ViewHolder(@NonNull OneLayoutBinding item) {
            super(item.getRoot());
            one=item;
        }
    }
}

viewbinding is easy to use, simple and more secure. Although it is still in the testing stage, it will be popular in the future. ヾ(≧▽≦*)o

Published 5 original articles, won praise 0, visited 116
Private letter follow

Posted by dabnet on Tue, 17 Mar 2020 02:09:22 -0700