如何在ListView中的LinearLayout中添加Views?我想在ListView中显示一些额外的信息,我想在for循环中添加它们来处理要添加的项目。这是我目前的代码:
的活动:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_marginTop="0dp" />
</LinearLayout>
list_recycler:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="6dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp">
<TextView android:id="@+id/list_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/textColor"
android:textSize="16sp"
android:layout_alignParentLeft="true"/>
<TextView android:id="@+id/list_title_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/textColor"
android:textSize="16sp"
android:layout_alignParentLeft="true"/>
<!-- VIEWS SHOULD BE ADDED HERE -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
</LinearLayout>
适配器:
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.RecyclerViewHolder> {
public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
TextView personName;
TextView personAge;
RecyclerViewHolder(View itemView) {
super(itemView);
personName = (TextView)itemView.findViewById(R.id.person_name);
personAge = (TextView)itemView.findViewById(R.id.person_age);
}
}
List<Items> list;
public RVAdapter(List<Items> list){
this.list = list;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_recycler, viewGroup, false);
return new RecyclerViewHolder(v);
}
@Override
public void onBindViewHolder(RecyclerViewHolder recyclerViewHolder, int i) {
recyclerViewHolder.personName.setText(persons.get(i).name);
recyclerViewHolder.personAge.setText(persons.get(i).age);
}
@Override
public int getItemCount() {
return persons.size();
}
}
产品:
public class Items {
String name;
String age;
public Items(String name, String age) {
this.name = name;
this.age = age;
}
}
如何在ListView中的LinearLayout中添加视图?不同ListView项目中的每个LinearLayout应包含不同的视图,因此它们不能相同。
我的数据如何:
ListView
Entry1
LinearLayout
SubEntry1
SubEntry2
SubEntry3
Entry2
LinearLayout
SubEntry1
SubEntry2
SubEntry3
Entry3
LinearLayout
SubEntry1
SubEntry2
SubEntry3
所以Entry1..Entry3是ListViews,所有子条目应该是TextViews,通过代码添加到其对应的ListView条目中的LinearLayout
答案 0 :(得分:3)
为布局添加id
,说@+id/item_content
,然后在getView
中执行:
itemContent = (LinearLayout)convertView.findViewById(R.id.item_content);
itemContent.removeAllViews();
//then you can add your views to the layouts depending on each item here using
itemContent.addView(yourView);
您不能简单地遍历ListView
项并向其添加内容。这就是我们的适配器。更新数据并在数据发生更改时通知适配器。
我建议你在这种情况下使用RecyclerView
。
修改强>
您添加的内容不是数据结构,而是您的布局结构。无论如何,你应该有这个。
public class RowItem {
private final String title;
private final String desc;
private ArrayList<String> contents;
}
然后你会这样做;
itemContent = (LinearLayout)convertView.findViewById(R.id.item_content);
itemContent.removeAllViews();
//then you can add your views to the layouts depending on each item here using
for(String item : rowItem.getContents()){
TextView textView = new TextView(parent.getContext());
textView.setText(item);
itemContent.addView(textView);
}
阅读RecyclerView
和ExpandableListView