微信APP实现对有recycleView的页面进行点击跳转设计
功能说明:
在上次微信界面设计的基础上,对有recycleView的页面进行点击跳转设计;
1.选择对应的xml文件,在布局中添加RecyclerView
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"/>
``
2.创建一个item.xml文件,并进行界面布局
```java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/darker_gray"
android:layout_margin="8dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="30sp" />
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内容"
android:textSize="30sp" />
</LinearLayout>
如图:
3.在Fragment文件中初始化RecyclerView
public class settingFragment extends Fragment {
public settingFragment() {
}
private Context context;
private List<String> mList = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.tab04, container, false);
context = view.getContext();
InitData();
RecyclerView recyclerView = view.findViewById(R.id.recyclerview);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(context,mList);
recyclerView.setAdapter(adapter);
LinearLayoutManager manager = new LinearLayoutManager(context);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
recyclerView.addItemDecoration(new DividerItemDecoration(context,LinearLayoutManager.VERTICAL ));
return view;
}
private void InitData() {
mList.add("亚特兰大老鹰");
mList.add("夏洛特黄蜂");
mList.add("迈阿密热火");
mList.add("奥兰多魔术");
mList.add("华盛顿奇才");
mList.add("波士顿凯尔特人");
mList.add("布鲁克林篮网");
mList.add("纽约尼克斯");
mList.add("费城76人");
mList.add("多伦多猛龙");
mList.add("芝加哥公牛");
}
}
4.适配器Adapter设置
Adapter是用来帮助填出数据的中间桥梁,将各种数据以合适的形式显示在View中给用户看。
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.Myviewholder> {
private List<String> mList;
private Context context;
public RecyclerViewAdapter(Context context,List<String> list) {
this.mList = list;
this.context=context;
}
@NonNull
@Override
public Myviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
View view=(View)LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
Myviewholder myviewholder=new Myviewholder((view));
return myviewholder;
}
@Override
public void onBindViewHolder(@NonNull Myviewholder holder, int position) {
holder.tvNum.setText(position + 1 + " ");
holder.tvContent.setText(mList.get(position));
}
@Override
public int getItemCount() {
return mList.size();
}
public class Myviewholder extends RecyclerView.ViewHolder{
TextView tvNum,tvContent;
public Myviewholder(@NonNull View itemView) {
super(itemView);
tvNum=itemView.findViewById((R.id.tv_num));
tvContent=itemView.findViewById(R.id.tv_content);
}
}
}
结果:
点击亚特兰大老鹰
已将代码上传至代码仓库gitee