简介
利用RecyclerView创建动态列表,动态添加显示数据。
环境
Android Studio:2020.3.1 这里注意:最新版的2021.1.1在布局编辑器对于material design的某些组件可以会造成无法显示的情况 Android Gradle Plugin Version:7.0.4 Gradle Version:7.0.2
实现步骤
1.设置主界面布局(activity_main.xml)
核心是RecyclerView,外面加上ScrollView和LinnerLayout是便于数据太多后可滑动显示
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</ScrollView>
2.设置单个数据显示的布局(recycle_data.xml)
这里根据自己的情况加入需要显示的图片、文字、按钮等组件,这里加入一个名为data1的TextView用于说明用法。
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/data_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数据1"
android:textAppearance="?attr/textAppearanceHeadline6"
android:textSize="15dp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
3. 设置主界面(RecycleAdapter.java)
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
void init(){
recyclerView = findViewById(R.id.recycleView);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
String[] data = {"这是1","这是2"};
recyclerView.setAdapter(new RecycleAdapter(data));
}
}
3. 设置生成列表的适配器(RecycleAdapter.java)
举例:
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder> {
String[] data;
public RecycleAdapter(String[] data) {
this.data = data;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView data_1;
public ViewHolder(View view) {
super(view);
data_1 = view.findViewById(R.id.data_1);
}
}
@Override
public RecycleAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycle_data, viewGroup, false);
return new RecycleAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder ViewHolder, int position) {
ViewHolder.data_1.setText(data[position]);
}
@Override
public int getItemCount() {
return data.length;
}
}
最终文件:
参考文章
官方文档:使用RecyclerView创建动态列表https://developer.android.com/guide/topics/ui/layout/recyclerview
|