- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我需要在我的应用程序中使用可扩展的 ListView ,其中每个组都有不同的表格布局。我想为表格中的每一行设置 onClick 监听器。
How is it possible to set different layouts to different groups in a expandable listview?
Or Is it possible to do the same function of Expandable listview with Table Layout by dynamically adding rows(expanding) and removing them (hiding).
最佳答案
您可以基于例如 BaseExpandableListAdapter
创建自己的 ExpandableListAdapter
。接下来,您应该覆盖 getChildView
方法,以便所需的 subview 具有所需的布局。这是一个简短的例子:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
public ArrayList<String> groupItem;
public ArrayList<Object> childItem;
public LayoutInflater minflater;
public MyExpandableListAdapter(ArrayList<String> grList,
ArrayList<Object> childItem, Context context) {
groupItem = grList;
this.childItem = childItem;
this.context = context;
minflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
if (groupItem.get(groupPosition).equals("desired_group_name")) {
convertView = minflater.inflate(R.layout.desired_layout, null);
} else {
convertView = minflater.inflate(R.layout.other_layout, null);
}
}
}
}
如果这个答案不清楚,我可以为您提供整个类(class),但示例很容易在网上找到。
这是我项目中的整个类(class)。我知道 convertView 应该是可重用的,但是当列表折叠然后重新打开时,我对 Views 的顺序有疑问。我只有几个元素,所以从性能角度来看这不是问题。
package pl.psnc.iip.hne.android.hneclient.adapter;
import java.util.ArrayList;
import pl.psnc.iip.hne.android.hneclient.R;
import pl.psnc.iip.hne.android.hneclient.json.Device;
import pl.psnc.iip.hne.android.hneclient.json.Devices;
import pl.psnc.iip.hne.android.hneclient.listeners.ImageViewRegisteredListener;
import pl.psnc.iip.hne.android.hneclient.utility.ConnectionUtils;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.TextView;
public class DeviceExpandableListAdapter extends BaseExpandableListAdapter {
public ArrayList<String> groupItem;
public ArrayList<Device> tempChild;
public ArrayList<Object> childItem;
public LayoutInflater minflater;
ArrayList<ImageViewRegisteredListener> ivrListeners = null;
public Activity activity;
public Context context;
int idIterator;
public void addImageViewRegisteredListener(
ImageViewRegisteredListener listener) {
if (!ivrListeners.contains(listener)) {
ivrListeners.add(listener);
}
}
public void removeImageViewRegisteredListener(
ImageViewRegisteredListener listener) {
if (ivrListeners.contains(listener)) {
ivrListeners.remove(listener);
}
}
public DeviceExpandableListAdapter(ArrayList<String> grList,
ArrayList<Object> childItem, Context context) {
groupItem = grList;
idIterator = 0;
ivrListeners = new ArrayList<ImageViewRegisteredListener>();
this.childItem = childItem;
this.context = context;
minflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setInflater(LayoutInflater mInflater, Activity act) {
this.minflater = mInflater;
activity = act;
}
public int findAppropriateImageForDevice(String type) {
int id = -1;
if (type.equals(Devices.PLOGG)) {
id = R.drawable.plogg;
} else if (type.equals(Devices.RFID)) {
id = R.drawable.rfid;
} else if (type.equals(Devices.BARRIER)) {
// id = -1;
} else if (type.equals(Devices.TEMPLIGHT)) {
// id = -1;
}
return id;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return (Device) ((ArrayList<Device>) childItem.get(groupPosition))
.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
tempChild = (ArrayList<Device>) childItem.get(groupPosition);
Device device = tempChild.get(childPosition);
Log.i(this.getClass().getName(), device.getIp());
if (groupItem.get(groupPosition).equals(Devices.PLOGG)) {
int newId = -1;
Integer oldId = ConnectionUtils.getInstance().getPowerIndicatorHT()
.get(device.getIp());
if (oldId != null) {
newId = oldId;
} else {
++idIterator;
newId = idIterator;
ConnectionUtils.getInstance().getPowerIndicatorHT()
.put(device.getIp(), newId);
}
convertView = minflater.inflate(R.layout.plogg_item, null);
ImageView powerIndicator = (ImageView) convertView
.findViewById(R.id.powerIndicator);
powerIndicator.setId(newId);
Integer foundId = ConnectionUtils.getInstance()
.getPowerIndicatorHT().get(device.getIp());
if (foundId != null) {
for (ImageViewRegisteredListener listener : ivrListeners) {
listener.imageViewRegistered(convertView, foundId,
device.getIp());
}
}
Log.i(DeviceExpandableListAdapter.class.getName(), "newId: "
+ newId);
} else {
convertView = minflater.inflate(R.layout.device_item, null);
}
TextView textTop = null;
textTop = (TextView) convertView.findViewById(R.id.toptext);
TextView textBottom = null;
textBottom = (TextView) convertView.findViewById(R.id.bottomtext);
textTop.setText(device.getIp());
textBottom.setText(device.getDescription());
int id = findAppropriateImageForDevice(device.getType());
ImageView image = (ImageView) convertView.findViewById(R.id.icon);
if (id != -1) {
image.setImageResource(id);
} else {
image.setImageResource(R.drawable.ic_launcher);
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return ((ArrayList<String>) childItem.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public int getGroupCount() {
return groupItem.size();
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = minflater.inflate(R.layout.group_row, null);
}
CheckedTextView ctv = (CheckedTextView) convertView
.findViewById(R.id.textView1);
ctv.setText(groupItem.get(groupPosition));
ctv.setChecked(isExpanded);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
实际情况如下:
关于java - 可扩展 ListView 中不同组的不同布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10990711/
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 4 个月前关闭。 Improve
我已经为这个概念苦苦挣扎了一段时间。我正在尝试提出一种松散耦合的 Azure 组件设计,该设计可以使用队列和辅助角色完全可扩展,从而使项目出列并处理。我可以随意扩展工作角色,并且发布到队列从来都不是问
尝试在 android 中创建可扩展 ListView 。我希望每个组都扩展到一个预设的 XML 布局文件。例如。 A组{ 展开时显示 XML 文件 A:按钮、搜索栏、微调器等 } 乙组{ 展开时显示
在 android 可扩展 ListView 中,我需要显示子项,仅在按下展开折叠节点时..即;我不想让 children 按下可扩展 ListView 的行。我该如何完成? 问候,罗尼 最佳答案 我
我正在开发一个Android代码编辑器,但是当我对它应用语法突出显示时,它会变得很滞后,是否有任何方法可以对其进行优化? 这是我的代码: protected void onCreate(Bundle
我正在开发安卓应用程序。我发现下面的输出图像显示了以下问题。请帮我解决我的问题。 Xml file 输出截图- 最佳答案 使用 setIndicatorBounds。 开发链接:http://deve
最近我在尝试使用 android 的可扩展 ListView ,所以我四处搜索并偶然发现了这个 http://about-android.blogspot.com/2010/04/steps-to-i
在 Control.Exception 的文档中在 base 4.4.0.0 中有一个如何创建异常层次结构的示例。该示例展示了如何通过根据父异常声明 Exception 类的实例来捕获特定异常的概括。
我正在开发一个将显示一些可扩展列表的应用程序。事实上,有时该对象不会有任何子对象。所以我想在我的可扩展列表中仅显示具有子元素的元素。我尝试在 getGroupView 函数中放入 if ,如果该对象没
这是一个有点开放式的问题,但是,制作良好的可扩展 Electron 应用程序的好方法是什么? VSCode、Atom 和许多其他工具都支持扩展,但它们的代码库太大,我无法理解发生了什么。我对 Jupy
我和一些 friend 为 Facebook 编写了一款游戏,但没有过多考虑游戏的实际架构 - 想象一下当我们每月吸引超过 300,000 名独立玩家并且我们的服务器崩溃时我们的惊喜。 现在我们正在努
您好,我正在使用 Android Expandable listview 并在其中用不同的 View 膨胀 child 。我遇到的问题是,当我展开一个 View 然后打开另一个父 View 时,布局中
我的场景是,我有一个可扩展 ListView ,在列表中动态添加我和 child 。我的子布局就是这样。 当我点击相应项目的编辑按钮时,我打开对话框从用户那里获取输入,然后点击更新相应的详细信息更新到
我想使用 RecyclerView 创建项目列表,并希望在单击时展开特定项目(如电话列表)。我想在不使用任何库的情况下实现这一目标。谁能帮忙? 最佳答案 获取子数据列表作为数据集中父数据的成员。并且,
我使用 ExpandableListview ... 我能够将从 Web 服务检索到的值设置为子布局的单个 Textview。现在需要在 Web 服务的子布局中设置两个不同的 Textviews 值我
我的可扩展 ListView 的顺序似乎不正确。 这是我的数据提供者: public class DataProvider { public static HashMap> getInfo() {
所以我试图在我的可扩展列表的子项列表中创建一行,但它不起作用,这是我的布局:
我有一个需要自定义箭头的可扩展 ListView 。我尝试将 groupIndicator 设置为这样的选择器: 但是,出于某种原因,这会扭曲箭头的尺寸,请参见下文: 知道它们为
我正在尝试实现具有 2 种不同布局的 ExpandableListView,到目前为止,我花了一段时间才真正让 1 个 View 组使用一个布局,而其他 2 个 View 组使用另一个。到目前为止,外
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我是一名优秀的程序员,十分优秀!