gpt4 book ai didi

java - 动态添加textview到CardView

转载 作者:行者123 更新时间:2023-12-02 04:06:46 24 4
gpt4 key购买 nike

因此,我创建了一个卡片 View ,如下所示:

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

在我的适配器类中,我有以下代码来填充卡片 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();
}

这正确地显示了我期望的输出。问题是,CardView 仅在 3 个 TextView 中显示最多 3 个项目,然后继续创建更多包含最多 3 个项目的卡片来向我显示其余内容。 我想做的是使用textview“时间轴”作为标题,然后动态添加textview“标题”的内容。例如,我可能有一张卡片,其中有 1 个“时间轴”TextView、0、2、5 等“标题”TextView、1 个“网站”TextView

这是可以用 CardView 完成的事情吗?如果是,我将不胜感激任何有用的指示来帮助我开始。

最佳答案

转换您的 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 中添加动态 TextView 到标题:

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

关于java - 动态添加textview到CardView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56709337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com