viewBinding在RecyclerView的初步简单使用
作者:互联网
前言
上来先说明一下我的编译器版本是as4.0 beta2。
gradle:4.0.0-beta02,对应gradle-6.2.2-all.zip。
关于viewbiding的作用,我这里直接引用android开发者网站上的话:
通过视图绑定功能,您可以更轻松地编写可与视图交互的代码。在模块中启用视图绑定之后,系统会为该模块中的每个 XML 布局文件生成一个绑定类。绑定类的实例包含对在相应布局中具有 ID 的所有视图的直接引用。
在大多数情况下,视图绑定会替代 findViewById。
打开视图绑定
在as版本和gradle版本同时满足条件后可以在build.gradle里设置开启。注意,这里不知道说是不是我用的版本有所变化,开启的方法和网上查到的3.6版本有所差别,也不知道以后会不会变,请以最新版为准
android {
buildFeatures {
viewBinding = true
}
}
在recyclerview的简单使用
下面只展示几个写起来略有差别的部分
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;//这里就是id
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
//省略掉无关内容
//ChangeAdapter adapter = new ChangeAdapter(one);
recyclerView.setAdapter(adapter);
show();
}
boolean show(){
//获取第一项的viewholder
RecyclerView.ViewHolder veve=recyclerView.findViewHolderForAdapterPosition(0);
//这里使用的是viewbinding的bind方法,将viewholder的view传入
OneLayoutBinding one=OneLayoutBinding.bind(veve.itemView);
//通过viewbinding获取到文本内容并打印
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;
//省略,大体就展示下样子
}
}
@NonNull
@Override
public ChangeAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
OneLayoutBinding one=OneLayoutBinding.inflate(LayoutInflater.from(parent.getContext()));
//常规写法
//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使用起来极易上手,代码简洁,且更安全,虽然现在依然在测试阶段,不过相比也会在未来大行其道。ヾ(≧▽≦*)o
标签:ViewHolder,viewBinding,初步,start,RecyclerView,ChangeAdapter,holder,public 来源: https://blog.csdn.net/weixin_44544354/article/details/104923946