展示一个列表在GUI中 – Java / Android Studio

huangapple go评论78阅读模式
英文:

Display a list in a gui - java/Android Studio

问题

我的目标是显示一个列表,其中每个元素(迭代)都显示有两个按钮:展示一个列表在GUI中 – Java / Android Studio

按钮会更改列表中特定元素的一个变量。

我的问题是:如何在Android Studio中创建这个界面?

英文:

My goal is to display a list where each element (iteration) is displayed with two buttons:
展示一个列表在GUI中 – Java / Android Studio

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的简单示例。您可以根据需要进行更改:

  1. 在您的布局文件夹中添加 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>

这基于您的示例图像。此布局包含在列表的单行中的所有视图。如果您想在每一行中添加更多按钮或文本,必须在此文件中添加它们。

  1. 您总是需要为RecyclerView创建一个适配器类。因此,请将以下 MyAdapter.java 类添加到您的项目中:
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    // ...(您的代码)
}
  1. 在 activity_main.xml 中添加一个 RecyclerView:
<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recyclerView"/>
  1. 在 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));

结果:

展示一个列表在GUI中 – Java / Android Studio

英文:

Here I implement a simple example of RecyclerView in android (java). You can change it as you need:

  1. Add item.xml in your layout folder, and write these lines in it:

     &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
     &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
         xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
         android:layout_width=&quot;match_parent&quot;
         android:layout_height=&quot;wrap_content&quot;&gt;
    
     &lt;TextView
         android:id=&quot;@+id/titleTextView&quot;
         android:layout_width=&quot;wrap_content&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:layout_margin=&quot;20dp&quot;
         app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
         app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
         app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
    
     &lt;Button
         android:id=&quot;@+id/option2Button&quot;
         android:layout_width=&quot;wrap_content&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:layout_margin=&quot;20dp&quot;
         app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
         app:layout_constraintRight_toRightOf=&quot;parent&quot;
         app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
    
     &lt;Button
         android:id=&quot;@+id/option1Button&quot;
         android:layout_width=&quot;wrap_content&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:layout_margin=&quot;20dp&quot;
         app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
         app:layout_constraintRight_toLeftOf=&quot;@+id/option2Button&quot;
         app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
    
     &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
    

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.

  1. You always need an Adapter class for RecyclerViews. So, add MyAdapter.java class to your project as below:

     public class MyAdapter extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt;
     {
         private Context context;
         private List&lt;String&gt; titles;
    
         MyAdapter(Context context, List&lt;String&gt; 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(&quot;option 1&quot;);
             itemVH.option2Button.setText(&quot;option 2&quot;);
    
             // 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 &gt; -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.

  1. In activity_main.xml add a RecyclerView:

     &lt;androidx.recyclerview.widget.RecyclerView
         android:layout_width=&quot;match_parent&quot;
         android:layout_height=&quot;match_parent&quot;
         android:id=&quot;@+id/recyclerView&quot;/&gt;
    
  2. In MainActivity.java add these lines in onCreate:

     // create list:
     List&lt;String&gt; titles = new ArrayList&lt;&gt;();
     titles.add(&quot;first item&quot;);
     titles.add(&quot;second item&quot;);
     titles.add(&quot;third item&quot;);
    
     // 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:

展示一个列表在GUI中 – Java / Android Studio

huangapple
  • 本文由 发表于 2020年8月31日 16:16:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63667201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定