Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RecyclerView with LinearLayout

I have been reading about Google's Material Design and ran across the new RecyclerView for Android. I have been following Google's tutorial but their example is very slim and I want to do more dynamic things with my RecylerView and I was hoping someone could help explain how to do so.

This is Google's RecyclerView Adapter Example

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private String[] mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                               .inflate(R.layout.my_text_view, parent, false);
        // set the view's size, margins, paddings and layout parameters
        ...
        ViewHolder vh = new ViewHolder((TextView)v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.mTextView.setText(mDataset[position]);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

I've implemented this into my Android app and it successfully worked but the Layout they use only contains a single TextView.

Can someone explain how I can use this example for a layout that has multiple textviews?

For example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="?attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingLeft="8dp"
    android:paddingRight="8dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_margin="8dp"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_margin="8dp"/>

</LinearLayout>

How can I access the TextView's separately and possibly change the attributes for the LinearLayout?

This is where I have been following the Google tutorial Google RecyclerView

like image 751
Gary Holiday Avatar asked Oct 18 '25 16:10

Gary Holiday


1 Answers

You can extract the TextViews using findViewById from the inflated linear layout.

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private String[] mDataset;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTitle;
        public TextView mText
        public ViewHolder(LinearLayout v) {
            super(v);
            mTitle = (TextView) v.findViewById(R.id.title);
            mText = (TextView) v.findViewById(R.id.text);
        }
    }
    .....
    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                           .inflate(R.layout.my_text_view, parent, false);
        // set the view's size, margins, paddings and layout parameters
        ViewHolder vh = new ViewHolder((LinearLayout)v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTitle.setText(....);
        holder.mText.setText(.....);
    }
}
like image 79
Andre Compagno Avatar answered Oct 20 '25 07:10

Andre Compagno