当前位置:网站首页>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>
边栏推荐
- Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
- How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
- __ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
- 3.Oracle-控制文件的管理
- What is socket? Basic introduction to socket
- LeetCode 1200. Minimum absolute difference
- Redis-02.Redis命令
- 【LeetCode】Day95-有效的数独&矩阵置零
- Modnet matting model reproduction
- Install opencv -- CONDA to establish a virtual environment and add the kernel of this environment in jupyter
猜你喜欢
4. Object mapping Mapster
【LeetCode】Easy | 20. Valid parentheses
Game theory acwing 893 Set Nim game
Alibaba established the enterprise digital intelligence service company "Lingyang" to focus on enterprise digital growth
What is socket? Basic introduction to socket
MySQL advanced part 2: the use of indexes
Knapsack problem acwing 9 Group knapsack problem
How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
Idea debug failed
Redis-01.初识Redis
随机推荐
2021apmcm post game Summary - edge detection
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
【高德地图POI踩坑】AMap.PlaceSearch无法使用
Leetcode heap correlation
Niu Mei's math problems
Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations
[learning] database: MySQL query conditions have functions that lead to index failure. Establish functional indexes
Leetcode-9: palindromes
博弈论 AcWing 891. Nim游戏
C - XOR to all (binary topic)
求组合数 AcWing 887. 求组合数 III
How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
Presentation of attribute value of an item
Gauss Cancellation acwing 884. Solution d'un système d'équations Xor linéaires par élimination gaussienne
C job interview - casting and comparing - C job interview - casting and comparing
安装OpenCV--conda建立虚拟环境并在jupyter中添加此环境的kernel
Idea debug failed
Nested method, calculation attribute is not applicable, use methods
Day 2 document
AE tutorial - path growth animation