当前位置:网站首页>RecyclerView的应用
RecyclerView的应用
2022-07-05 06:20:00 【v_3483608762】
- 简单记录recyclerView
Adapter有些绕。
viewholder-----连接-------Adapter---------oncreate(里面的setAdapter)。
Person类作为存储数据在Adapter里面要将数据传给viewholder中的各个组件里。
主activity
package com.example.alertdialog;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private String[] strings = {
"xxxx", "wangwnagwnag", "Dddddd"};
private String[] decs = {
"aaaa", "aabbbb", "aabbc"};
private List<Person> datalist=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.datalist = datalist;
for (int i = 0; i < strings.length; i++) {
this.datalist.add(new Person(strings[i],decs[i]));
}
RecyclerView view= findViewById(R.id.re_view);
view.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this );
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
view.setLayoutManager(linearLayoutManager);
Adapter adapter = new Adapter();
view.setOnClickListener(view1 -> {
String x = String.valueOf(Math.random()*10);
Person person = new Person(x, x + "");
adapter.notifyItemInserted(3);
datalist.add(3,person);
adapter.notifyItemRangeChanged(3,adapter.getItemCount());
});
view.setAdapter(adapter);
}
class ViewHolder extends RecyclerView.ViewHolder {
private final RecyclerView.Adapter adapter;
TextView v1;
TextView v2;
public ViewHolder(@NonNull View itemView,RecyclerView.Adapter adapter) {
super(itemView);
v1 = itemView.findViewById(R.id.tv1);
v2 = itemView.findViewById(R.id.tv2);
this.adapter=adapter;
}
}
private class Adapter extends RecyclerView.Adapter<ViewHolder>{
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
return new ViewHolder(view,this);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.v1.setText((CharSequence) datalist.get(position).v1);
holder.v2.setText((CharSequence) datalist.get(position).v2);
}
@Override
public int getItemCount() {
return datalist.size();
}
}
}
class Person类
package com.example.alertdialog;
import android.widget.TextView;
public class Person {
String v1;
String v2;
public Person(String string, String dec) {
v1=string;
v2=dec;
}
}
数据一项一项的排列方法
<?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="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
</LinearLayout>
主排列
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/re_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
边栏推荐
- Basic explanation of typescript
- Matrixdb V4.5.0 was launched with a new mars2 storage engine!
- Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
- There are three kinds of SQL connections: internal connection, external connection and cross connection
- 求组合数 AcWing 889. 满足条件的01序列
- Navicat连接Oracle数据库报错ORA-28547或ORA-03135
- [2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
- 2022-5-第四周日报
- 做 SQL 性能优化真是让人干瞪眼
- 【LeetCode】Easy | 20. Valid parentheses
猜你喜欢
Leetcode stack related
博弈论 AcWing 891. Nim游戏
MySQL advanced part 2: the use of indexes
MPLS experiment
栈 AcWing 3302. 表达式求值
International Open Source firmware Foundation (osff) organization
MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
高斯消元 AcWing 884. 高斯消元解异或線性方程組
MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
RGB LED infinite mirror controlled by Arduino
随机推荐
20220213-CTF MISC-a_ good_ Idea (use of stegsolve tool) -2017_ Dating_ in_ Singapore
Records of some tools 2022
区间问题 AcWing 906. 区间分组
Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
Sqlmap tutorial (II) practical skills I
RGB LED infinite mirror controlled by Arduino
__ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
Simple selection sort of selection sort
SPI details
5.Oracle-表空间
【LeetCode】Easy | 20. Valid parentheses
安装OpenCV--conda建立虚拟环境并在jupyter中添加此环境的kernel
11-gorm-v2-03-basic query
Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
AE tutorial - path growth animation
Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
LeetCode 1200. Minimum absolute difference
The difference between CPU core and logical processor
Leetcode-6110: number of incremental paths in the grid graph
Record the process of configuring nccl and horovod in these two days (original)