【问题标题】:How to display multiple TextViews in a RecyclerView list item如何在 RecyclerView 列表项中显示多个 TextView
【发布时间】:2020-03-18 11:56:14
【问题描述】:

我正在使用 ReyclerView,我想在每个列表项中显示 3 个 TextView。 到目前为止,我设法将 3 个 TextViews 显示为 3 个不同的列表项。

这是我的适配器代码:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
    private String[] mDataset;


    public static class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView textView;
        public MyViewHolder(View v) {
            super(v);
            textView = v.findViewById(R.id.recycler_view_item);
        }
    }

    public CustomAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    @Override
    public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                         int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_item, parent, false);
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

还有layout_item.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="15dp">

    <TextView
        android:id="@+id/recycler_view_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"/>

</LinearLayout>

【问题讨论】:

  • 只需将 3 个TextViews 放入您的layout_item?

标签: java android xml android-recyclerview


【解决方案1】:

只需在 layout_item 中添加两个 textViews

【讨论】:

  • 这样做会发生什么
  • 你到底想要什么,在回收站或持有人里有更多的 textView
  • 很难解释它只是没有按照我想要的方式工作
  • think in holder中的更多textViews
【解决方案2】:
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="15dp">

        <TextView
            android:id="@+id/text_view_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="40sp"/>

 <TextView
            android:id="@+id/text_view_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="40sp"/>

 <TextView
            android:id="@+id/text_view_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="40sp"/>

    </LinearLayout>

// 如果您希望您的文本视图彼此相邻,请在 LinearLayout 中使用 android:orientation="horizo​​ntal",或者如果您希望您的文本视图在另一个下方使用 android:orientation="vertical",这将在一个视图中

【讨论】:

    【解决方案3】:

    如果您想设置三个 Textviews,请使用相对布局而不是线性布局。您可以从要设置这些 Textviews 的设计 Platte 中拖放 textview 会容易得多

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 2021-11-26
      • 2015-05-03
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多