解决方案
我是个白痴。我想到了。谢谢你@Krupal你指出我正确的方向。如果你把任何东西当作答案,我会选择你,因为你让我思考正确的方式。
所以我最终在这里做的是我创建了一个自定义列表适配器。我不会添加整个班级来保持简短。关键是要使用适配器来只返回一个孩子。然后在返回的孩子(下面的XML)中,我添加了所有需要的字段。所以现在,当我扩展列表时,只有一个孩子可见。同样在我的getChildView()中,我为孩子们定义了所有需要的布局条目。不知道这是否是完美的方式,但是可以很好地满足我的需求。
这是我使用我的数据实际配置孩子的地方。
@Override
public View getChildView(int groupPosition,final int childPosition,
boolean isLastChild,View convertView,ViewGroup parent){
final String childText =(String)getChild(groupPosition,0);
final String childTextSecond =(String)getChild(groupPosition,1);
final String childTextThird =(String)getChild(groupPosition,2);
final String deviceName =(String)getChild(groupPosition,3);
if(convertView == null){
LayoutInflater infalInflater =(LayoutInflater)this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item,null);
}
TextView txtListChild =(TextView)convertView
.findViewById(R.id.lblListItem);
TextView txtListChildTwo =(TextView)convertView
.findViewById(R.id.lblListItemTwo);
TextView txtListChildThree =(TextView)convertView
.findViewById(R.id.lblListItemThree);
//其余逻辑!
设置孩子数为1
@Override
public int getChildrenCount(int groupPosition){
return 1;
子元素的XML布局
<?xml version =1.0encoding =utf-8?>
< LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
android:orientation =verticalandroid:layout_width =match_parent
android:layout_height =match_parent
android:background =#000000>
< RelativeLayout
android:layout_width =340dp
android:layout_height =wrap_content
android:layout_gravity =center_horizontal | right
机器人:背景= #f9fcff >
< LinearLayout
android:orientation =vertical
android:layout_width =match_parent
android:layout_height =fill_parent
android: layout_centerHorizontal =true
android:layout_alignParentTop =true
android:background =#4d86ff>
< RelativeLayout
android:layout_width =match_parent
android:layout_height =45dp
android:layout_gravity =center_horizontal>
< TextView
android:layout_width =wrap_content
android:layout_height =wrap_content
android:textAppearance =@ android:style / TextAppearance.Medium
android:layout_marginLeft =10dp
android:text =大文本
android:id =@ + id / lblListItem
android:textColor =#ffffff
android:layout_alignTop =@ + id / textView5
android:layout_toRightOf =@ + id / textView5
android:layout_toEndOf =@ + id / textView5/>
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_marginLeft =15dp
android: textAppearance =@ android:style / TextAppearance.Medium
android:text =设备ID:
android:id =@ + id / textView5
android:layout_centerVertical =true
android:layout_alignParentLeft =true
android:layout_alignParentStart =true
android:textColor =#ffffff/>
style =?android:attr / buttonStyleSmall
android:layout_width =80dp
android:layout_height =wrap_content
android:text =Tags
android:id =@ + id / btnTags
android:background =#ffffff
android:singleLine =true
android:layout_alignTop =@ + id / lblListItem
android:layout_alignParentRight =true
android:layout_alignParentEnd =true
android:layout_marginRight =15dp
android: layout_marginEnd =15dp/>
< / RelativeLayout>
< RelativeLayout
android:layout_width =match_parent
android:layout_height =45dp
android:layout_gravity =center_horizontal>
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_marginLeft =15dp
android: textAppearance =@ android:style / TextAppearance.Medium
android:text =Status:
android:id =@ + id / textView6
android:layout_centerVertical =true
android:layout_alignParentLeft =true
android:layout_alignParentStart =true
android:textColor =#ffffff/>
TextView
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_marginLeft =10dp
android: textAppearance =@ android:style / TextAppearance.Medium
android:text =#ffffff
android:id =@ + id / lblListItemTwo
android:layout_alignTop =@ + id / textView6
android:layout_toRightOf =@ + id / textView6
android:layout_toEndOf =@ + id / textView6/>
< / RelativeLayout>
< RelativeLayout
android:layout_width =match_parent
android:layout_height =match_parent
android:layout_gravity =center_horizontal>
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_marginLeft =15dp
android: layout_marginBottom =10dp
android:textColor =#ffffff
android:textAppearance =@ android:style / TextAppearance.Medium
android:text =Description:
android:id =@ + id / textView7
android:layout_marginStart =15dp
android:layout_alignTop =@ + id / lblListItemThree
android:layout_alignParentLeft =true
android:layout_alignParentStart =true/>
TextView
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_marginLeft =20dp
android: layout_marginTop =6dp
android:layout_marginRight =13dp
android:layout_marginBottom =10dp
android:textAppearance =@ android:style / TextAppearance.Medium
android :text =Large Text
android:id =@ + id / lblListItemThree
android:textColor =#ffffff
android:layout_marginStart =39dp
android :layout_alignParentTop =true
android:layout_toRightOf =@ + id / textView7
android:layout_toEndOf =@ + id / textView7/>
< / RelativeLayout>
< / LinearLayout>
< / RelativeLayout>
I currently have an expandable list view that is populating just fine. However when the child view is filling it creates individuals views and rows for each entry. I would like to have one view and multiple textviews in the view as well as a button that I will be adding. The data is being populated from a HASHMAP. I have tried many things and cannot get multiple textviews to populate correctly in the listview.
All data comes from this hashmap:
private HashMap<String, List<String>> _deviceDetails;
This is the get child view method of my custom adapter.
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
This is an image of what I have now with what I wish to do.
I need the data highlighted inside of one row so that I can add buttons etc without a bunch of rows. I genuinely cannot figure out how to do this properly.
解决方案
I'm an idiot. I figured it out. Thank you @Krupal you pointed me in the right direction. If you place anything as an answer i'll select you because you got me thinking the right way.
So what I ended up doing here was I created a custom List Adapter. I won't add the entire class to keep it short. The key was to use set the adapter to only return one child. Then in the child that was returned (XML below) I had all of my needed fields added in. So now when I expanded the list only one child would be visible. Also in my getChildView() I defined all needed entries for the layout for the children. Don't know if this is the perfect way to do it but worked nicely for my needs.
This is where I actually configured the child with my data.
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, 0);
final String childTextSecond = (String) getChild(groupPosition, 1);
final String childTextThird = (String) getChild(groupPosition, 2);
final String deviceName = (String) getChild(groupPosition, 3);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
TextView txtListChildTwo = (TextView) convertView
.findViewById(R.id.lblListItemTwo);
TextView txtListChildThree = (TextView) convertView
.findViewById(R.id.lblListItemThree);
//rest of logic!
}
Setting the children count to 1
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
XML Layout for the child elements
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<RelativeLayout
android:layout_width="340dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|right"
android:background="#f9fcff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:background="#4d86ff">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Medium"
android:layout_marginLeft="10dp"
android:text="Large Text"
android:id="@+id/lblListItem"
android:textColor="#ffffff"
android:layout_alignTop="@+id/textView5"
android:layout_toRightOf="@+id/textView5"
android:layout_toEndOf="@+id/textView5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textAppearance="@android:style/TextAppearance.Medium"
android:text="Device ID: "
android:id="@+id/textView5"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#ffffff" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Tags"
android:id="@+id/btnTags"
android:background="#ffffff"
android:singleLine="true"
android:layout_alignTop="@+id/lblListItem"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textAppearance="@android:style/TextAppearance.Medium"
android:text="Status: "
android:id="@+id/textView6"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="@android:style/TextAppearance.Medium"
android:text="Large Text"
android:id="@+id/lblListItemTwo"
android:textColor="#ffffff"
android:layout_alignTop="@+id/textView6"
android:layout_toRightOf="@+id/textView6"
android:layout_toEndOf="@+id/textView6" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginBottom="10dp"
android:textColor="#ffffff"
android:textAppearance="@android:style/TextAppearance.Medium"
android:text="Description: "
android:id="@+id/textView7"
android:layout_marginStart="15dp"
android:layout_alignTop="@+id/lblListItemThree"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="6dp"
android:layout_marginRight="13dp"
android:layout_marginBottom="10dp"
android:textAppearance="@android:style/TextAppearance.Medium"
android:text="Large Text"
android:id="@+id/lblListItemThree"
android:textColor="#ffffff"
android:layout_marginStart="39dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView7"
android:layout_toEndOf="@+id/textView7" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
这篇关于ExpandableListView Android中的多个Textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!