当前位置:网站首页>Application of recyclerview
Application of recyclerview
2022-07-05 06:27:00 【v_ three billion four hundred and eighty-three million six hund】
- Simple record recyclerView
Adapter Some winding .
viewholder----- Connect -------Adapter---------oncreate( Inside setAdapter).
Person Class as stored data in Adapter It needs to transmit data to viewholder In each component of .
Lord 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 class
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;
}
}
The arrangement of data item by item
<?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>
Main arrangement
<?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>
边栏推荐
- Day 2 document
- 【LeetCode】Easy | 20. Valid parentheses
- C - XOR to all (binary topic)
- 高斯消元 AcWing 884. 高斯消元解异或线性方程组
- Quickly use Amazon memorydb and build your own redis memory database
- Daily question 1189 Maximum number of "balloons"
- [leetcode] day94 reshape matrix
- 1.手动创建Oracle数据库
- Vant Weapp SwipeCell设置多个按钮
- [learning] database: MySQL query conditions have functions that lead to index failure. Establish functional indexes
猜你喜欢

Find the combination number acwing 888 Find the combination number IV

Client use of Argo CD installation

1.15 - input and output system

Find the combination number acwing 887 Find combination number III

ADG5412FBRUZ-RL7应用 双电源模拟开关和多路复用器IC

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

Alibaba established the enterprise digital intelligence service company "Lingyang" to focus on enterprise digital growth

MySQL advanced part 1: index

Inclusion exclusion principle acwing 890 Divisible number

Quickly use Amazon memorydb and build your own redis memory database
随机推荐
Bash exercise 17 writing scripts to install the server side of FRP reverse proxy software
安装OpenCV--conda建立虚拟环境并在jupyter中添加此环境的kernel
Knapsack problem acwing 9 Group knapsack problem
Filter the numbers and pick out even numbers from several numbers
【LeetCode】Day95-有效的数独&矩阵置零
2048项目实现
TypeScript入门
H5 module suspension drag effect
Client use of Argo CD installation
C job interview - casting and comparing - C job interview - casting and comparing
VLAN experiment
[wustctf2020] plain_ WP
New title of module a of "PanYun Cup" secondary vocational network security skills competition
ADG5412FBRUZ-RL7应用 双电源模拟开关和多路复用器IC
P2575 master fight
1.15 - input and output system
Vant weave swipecell sets multiple buttons
What is socket? Basic introduction to socket
How to understand the definition of sequence limit?
5. Oracle TABLESPACE