动态添加textview到CardView

Adding textview dynamically to CardView

所以,我创建了一个卡片视图,如下所示:

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

在我的适配器中 class 我有以下代码来填充卡片视图。

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

这正确地显示了我期望的输出。问题是 CardView 只显示我的 3 个 TextView 中的最多 3 个项目,然后继续创建最多包含 3 个项目的卡片以显示其余项目。 我想做的是将textview "timeline" 用作header,然后动态添加textview "title" only 的内容。因此,例如,我可能有一张卡片,其中包含 1 "timeline" TextView、0、2、5 等 "title" TextView、1 "website" TextView

CardView 可以做到这一点吗?如果是,我将不胜感激任何有助于我入门的指导。

如果我理解正确,你只想动态放置部分数据的问题。因此,在 bindView holder 中,仅绑定静态的动态部分,您可以使用字符串值在 xml 文件中进行硬编码。

转换你的 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>

现在在你的 onBindViewHolder 中添加动态 TextViews 到标题:

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