简体   繁体   中英

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. so far I managed to display 3 TextViews but as 3 different list items.

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:

<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,请使用相对布局而不是线性布局。您可以从设计 Platte 拖放 textview 来设置这些 Textviews 会容易得多

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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