英文:
Display a list in a gui - java/Android Studio
问题
我的目标是显示一个列表,其中每个元素(迭代)都显示有两个按钮:
按钮会更改列表中特定元素的一个变量。
我的问题是:如何在Android Studio中创建这个界面?
英文:
My goal is to display a list where each element (iteration) is displayed with two buttons:
The buttons would change a variable in the specific element of the list.
My problem: How do I create this gui in Android Studio?
答案1
得分: 2
这里我展示了在安卓(Java)中实现RecyclerView的简单示例。您可以根据需要进行更改:
- 在您的布局文件夹中添加 item.xml,并在其中编写以下内容:
<?xml version="1.0" encoding="utf-8"?>
<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">
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/option2Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/option1Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toLeftOf="@+id/option2Button"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这基于您的示例图像。此布局包含在列表的单行中的所有视图。如果您想在每一行中添加更多按钮或文本,必须在此文件中添加它们。
- 您总是需要为RecyclerView创建一个适配器类。因此,请将以下 MyAdapter.java 类添加到您的项目中:
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
// ...(您的代码)
}
- 在 activity_main.xml 中添加一个 RecyclerView:
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView"/>
- 在 MainActivity.java 的 onCreate 方法中添加以下代码:
// 创建列表:
List<String> titles = new ArrayList<>();
titles.add("first item");
titles.add("second item");
titles.add("third item");
// 定义适配器:
MyAdapter adapter = new MyAdapter(this, titles);
// 设置 RecyclerView:
RecyclerView recyclerView = findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
结果:
英文:
Here I implement a simple example of RecyclerView in android (java). You can change it as you need:
-
Add item.xml in your layout folder, and write these lines in it:
<?xml version="1.0" encoding="utf-8"?> <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"> <TextView android:id="@+id/titleTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/option2Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/option1Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toLeftOf="@+id/option2Button" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
this is based on your sample image. this layout contains all views in a single row of list. if you want more buttons or texts in each row, you must add them in this file.
-
You always need an Adapter class for RecyclerViews. So, add MyAdapter.java class to your project as below:
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<String> titles; MyAdapter(Context context, List<String> titles) { this.context = context; this.titles = titles; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { RecyclerView.ViewHolder viewHolder; LayoutInflater inflater = LayoutInflater.from(parent.getContext()); viewHolder = getViewHolder(parent, inflater); return viewHolder; } private RecyclerView.ViewHolder getViewHolder(ViewGroup parent, LayoutInflater inflater) { RecyclerView.ViewHolder viewHolder; View v1 = inflater.inflate(R.layout.item, parent, false); viewHolder = new ItemVH(v1); return viewHolder; } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { ItemVH itemVH = (ItemVH) holder; // set title for each item: itemVH.titleTextView.setText(titles.get(position)); itemVH.option1Button.setText("option 1"); itemVH.option2Button.setText("option 2"); // click listener for first button: itemVH.option1Button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // option 1 button clicked } }); // click listener for second button: itemVH.option2Button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // option 2 button clicked } }); } @Override public int getItemCount() { return titles.size(); } // add item to list: public void addItem(String title) { titles.add(title); notifyItemInserted(titles.size() - 1); } // remove item from list: public void remove(String title) { int position = titles.indexOf(title); if (position > -1) { titles.remove(position); notifyItemRemoved(position); } } protected class ItemVH extends RecyclerView.ViewHolder { private TextView titleTextView; private Button option1Button; private Button option2Button; public ItemVH(View itemView) { super(itemView); titleTextView = itemView.findViewById(R.id.titleTextView); option1Button = itemView.findViewById(R.id.option1Button); option2Button = itemView.findViewById(R.id.option2Button); } } }
The titles list is list of Strings shown on items. You create this list and send it to the MyAdapter class from MainActivity. all views in single item are find in ItemVH class, and you can work with these views in onBindViewHolder function.
-
In activity_main.xml add a RecyclerView:
<androidx.recyclerview.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recyclerView"/>
-
In MainActivity.java add these lines in onCreate:
// create list: List<String> titles = new ArrayList<>(); titles.add("first item"); titles.add("second item"); titles.add("third item"); // define the adapter: MyAdapter adapter = new MyAdapter(this, titles); // set the RecyclerView: RecyclerView recyclerView = findViewById(R.id.recyclerView); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(adapter); recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
Result:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论