gpt4 book ai didi

java - 在android中的可扩展 ListView 中单击父列表项时加载子项

转载 作者:行者123 更新时间:2023-11-30 02:42:34 25 4
gpt4 key购买 nike

我正在开发一个包含一个可扩展 ListView 的 Android 应用程序,用于从 Web 服务加载数据,但我不想在启动时加载整个数据,而是先加载父列表,然后在展开 ListView 的特定列表项时加载特定项目的子项目,所以首先是否可能?

我的示例代码:

package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.rk.socialsync.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import utils.Global;
import utils.OnlineProperty;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<Date> _listDataHeader;
private HashMap<Date, List<OnlineProperty>> _listDataChild;

public ExpandableListAdapter(Context context, List<Date> listDataHeader,
HashMap<Date, List<OnlineProperty>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}

@Override
public OnlineProperty getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

OnlineProperty onlineProperty = getChild(groupPosition, childPosition);

String child1Text = onlineProperty.getOnlineTime();
String child2Text = onlineProperty.getOfflineTime();

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.listelementchild, null);
}

TextView textOnline = (TextView) convertView.findViewById(R.id.textTimeOnline);
TextView textOffline = (TextView) convertView.findViewById(R.id.textTimeOffline);
TextView textView = (TextView) convertView.findViewById(R.id.textTo);

textView.setTypeface(Global.AppsFont);
textOnline.setText(child1Text);
textOffline.setText(child2Text);

textOffline.setTypeface(Global.AppsFont);
textOnline.setTypeface(Global.AppsFont);
return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}

@Override
public String getGroup(int groupPosition) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
return dateFormat.format(this._listDataHeader.get(groupPosition));
}

@Override
public int getGroupCount() {
return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.listelementexpandable, null);
}

TextView textHeader = (TextView) convertView.findViewById(R.id.textExpandable);
textHeader.setTypeface(Global.AppsFont);
textHeader.setText(headerTitle);

return convertView;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

}

其他问题是我想自定义布局更改,例如我在图像中指向的展开按钮,例如 + 和 -

enter image description here

最佳答案

是的,这是可能的,应该通过您的 ExpandableListViewOnGroupClickListener 来完成。首先将适配器中的子数组公开并默认初始化:

public HashMap<Date, List<OnlineProperty>> _listDataChild = 
new HashMap<Date, List<OnlineProperty>>();

下一步覆盖 groupClickListener:

mExpList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView p, View v, int grpPos, long id) {
// Check if children not yet fetched
if (mExpAdapter._listDataChild.get(date) == null) {
// Fetch list of children for group "grpPos"

// Add the children to your adapters HashMap
mExpAdapter._listDataChild.put(date, children);
}
return false;
}
});

注释:

  • 为避免加载过多,您可以重写适配器中的 onGroupCollapsed 以使其卸载未使用的子项。
  • 我提供的代码可能不正确,因为我不知道您的数据结构。你必须适应。

关于java - 在android中的可扩展 ListView 中单击父列表项时加载子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25565880/

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