0

So, I have a card view created as follows:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/timeline"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp" />
         <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/website"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp" />
    </LinearLayout>

And in my adapter class I have the following code to populate the card view.

public class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_title, tv_timeline, tv_website;

        public MyViewHolder(View view) {
            super(view);
            tv_title = (TextView) view.findViewById(R.id.title);
            tv_timeline = (TextView) view.findViewById(R.id.timeline);
            tv_website = (TextView) view.findViewById(R.id.website);
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.activity_row_data, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

            holder.tv_title.setText(mTitleList.get(position));                
      holder.tv_timeline.setText(mtimelineList.get(position));
            holder.tv_website.setText(mSiteSrc.get(position));

    }

    @Override
    public int getItemCount() {
        return mTitleList.size();
    }

This correctly shows me the output I am expecting. The problem is that a CardView only show me up to 3 items in my 3 TextView, and then keep creating more cards holding up to 3 items to show me the rest. What I would like to do is to use the textview "timeline" as a header, and then dynamically add content for textview "title" only. So for example, I might have a card which has 1 "timeline" TextView, 0, 2, 5 etc "title" TextView, 1 "website" TextView

Is this something that can be done with CardView? If yes, I'd appreciate any helpful pointers to help me get started.

Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71
Yasuo
  • 11
  • 1
  • 6

2 Answers2

0

If I understand right the question that you want to put dynamically only part of the data. So in bindView holder bind only dynamic part which is static you can hardcoded in xml file with string value.

Bo Z
  • 2,359
  • 1
  • 13
  • 31
0

Convert your xml :

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/timeline"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" />
     <LienarLayout
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="vertical"/>
    <TextView
        android:id="@+id/website"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" />
</LinearLayout>

Now in your onBindViewHolder add dynamic TextViews to title:

   public class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_timeline, tv_website;
        private LinearLayout title; 

        public MyViewHolder(View view) {
            super(view);
            title = (LinearLayout) view.findViewById(R.id.title);
            tv_timeline = (TextView) view.findViewById(R.id.timeline);
            tv_website = (TextView) view.findViewById(R.id.website);
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.activity_row_data, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

      holder.tv_timeline.setText(mtimelineList.get(position));
      holder.tv_website.setText(mSiteSrc.get(position));
      for(int i = 0; i < 5; i++) { // 5 is the count how many times you want to add textview
          LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
          TextView tv=new TextView(holder.title.getContext());
          tv.setLayoutParams(lparams);
          tv.setText("test = "+i);
          holder.title.addView(tv);
}

    }

    @Override
    public int getItemCount() {
        return mTitleList.size();
    }
Deˣ
  • 4,191
  • 15
  • 24
  • yes, that helped thanks! Just a quick note for your example you also need to add private LinearLayout title; – Yasuo Jun 23 '19 at 09:08