简体   繁体   English

如何在 RecyclerView 列表项中显示多个 TextView

[英]How to display multiple TextViews in a RecyclerView list item

I'm using a ReyclerView and I want to display 3 TextViews in each list item.我正在使用 ReyclerView,我想在每个列表项中显示 3 个 TextView。 so far I managed to display 3 TextViews but as 3 different list items.到目前为止,我设法显示了 3 个 TextView,但显示了 3 个不同的列表项。

Here's my adapter code:这是我的适配器代码:

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;
    }

And layout_item.xml:和 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>

只需在 layout_item 中再添加两个 textViews

    <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>

// If you want your textviews next to each other use android:orientation="horizontal" in LinearLayout or if you want your textviews one below the other use android:orientation="vertical" and this will in one view // 如果你想让你的 textviews 彼此相邻使用 android:orientation="horizo​​ntal" in LinearLayout 或者如果你想让你的 textviews 在另一个下面使用 android:orientation="vertical" 并且这将在一个视图中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM