gpt4 book ai didi

android - 如何实现Expandablelistview子自定义布局?

转载 作者:行者123 更新时间:2023-11-29 01:27:44 24 4
gpt4 key购买 nike

我不知道如何在 android 中实现这一点。我有这样的布局:

enter image description here

我希望它在点击加号按钮时会显示完整的布局,如下所示:

enter image description here

此布局是可扩展 ListView 项的子项。因此,当单击 expandablelistview 项目时,它会显示该类别中的项目丢失。然后,如果用户想要查看所有内容,他将单击加号按钮。任何帮助将不胜感激。

最佳答案

创建一个名为 expandable_list_layout 的 XML 布局

<ExpandableListView
android:id="@+id/data_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@id/dynamicHeadingTxt"
android:layout_marginTop="3dp"
android:layout_weight="8"
android:cacheColorHint="#00ffffff"
android:childDivider="@drawable/child_separator"
android:divider="@drawable/child_separator"
android:groupIndicator="@null" >
</ExpandableListView>

创建自定义适配器。我们称它为 InboxExpandableListAdapter。此类应扩展 BaseExpandableListAdapter 并覆盖

public class ada extends BaseExpandableListAdapter{

@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return null;
}

@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}

@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}

@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return 0;
}

@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return null;
}

@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}

如果你看这里,覆盖的方法有两个参数,即一个 groupposition 和一个 childposition。这些建议我们应该为适配器提供某种必须包含组及其子项的列表。我正在为该类创建一个构造函数

public InboxExpandableListAdapter(Activity context, List<String> data, List<String> data_secondry,
List< List<String>> dataCollections) {
this.context = context;
this.dataCollections = dataCollections;
this.data = data;
this.data_secondry = data_secondry;
}

现在 getChildView 方法和 getGroupView 填充每个项目的 View 。所以在这些方法中,膨胀你想为组和他们各自的 child 显示的布局

public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String data = (String) getChild(groupPosition, childPosition);
LayoutInflater inflater = context.getLayoutInflater();

if (convertView == null) {
convertView = inflater.inflate(R.layout.child_item, null);
}

TextView item = (TextView) convertView.findViewById(R.id.data);

item.setText(data);
return convertView;
}

public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {

String laptopName = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_item,
null);
}

TextView item = (TextView) convertView.findViewById(R.id.data);
item.setTypeface(null, Typeface.BOLD);
item.setText(laptopName);
return convertView;
}

关于android - 如何实现Expandablelistview子自定义布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32986839/

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