当前位置:网站首页>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>
边栏推荐
- 博弈论 AcWing 894. 拆分-Nim游戏
- Winter vacation water test 1 Summary
- 5.Oracle-表空间
- 博弈论 AcWing 893. 集合-Nim游戏
- Find the combination number acwing 888 Find the combination number IV
- 2022 winter vacation training game 5
- 微信小程序路由再次跳转不触发onload
- La redirection de l'applet Wechat ne déclenche pas onload
- 博弈论 AcWing 891. Nim游戏
- New title of module a of "PanYun Cup" secondary vocational network security skills competition
猜你喜欢
International Open Source firmware Foundation (osff) organization
Install opencv -- CONDA to establish a virtual environment and add the kernel of this environment in jupyter
MySQL advanced part 2: MySQL architecture
Paper reading report
Idea debug failed
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
Knapsack problem acwing 9 Group knapsack problem
Suppose a bank's ATM machine, which allows users to deposit and withdraw money. Now there is 200 yuan in an account, and both user a and user B have the right to deposit and withdraw money from this a
Inclusion exclusion principle acwing 890 Divisible number
MPLS experiment
随机推荐
C - XOR to all (binary topic)
La redirection de l'applet Wechat ne déclenche pas onload
Winter messenger 2
June 29, 2022 daily
Find the combination number acwing 889 01 sequence meeting conditions
求组合数 AcWing 889. 满足条件的01序列
安装OpenCV--conda建立虚拟环境并在jupyter中添加此环境的kernel
2022-5-第四周日报
H5 模块悬浮拖动效果
栈 AcWing 3302. 表达式求值
Install opencv -- CONDA to establish a virtual environment and add the kernel of this environment in jupyter
Game theory acwing 893 Set Nim game
Day 2 document
MySQL advanced part 2: the use of indexes
1.13 - RISC/CISC
3. Oracle control file management
[leetcode] day95 effective Sudoku & matrix zeroing
VLAN experiment
Find the combination number acwing 888 Find the combination number IV
Leetcode recursion