- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用可扩展 ListView 的自定义实现来创建一个可无限缩放的 ListView 。我的代码基于 How to display more than 3- levels of expandable List View? .
不幸的是,我的 ListView 仅显示三个级别,即使树包含更多嵌套项目,也无法扩展至此之外。
这是我的代码:
public class RootAdapter extends BaseExpandableListAdapter {
private UserObject root;
private final LayoutInflater listLayoutInflater;
public class Entry {
public final CustExpListview cls;
public final SecondLevelAdapter sadpt;
public Entry(CustExpListview cls, SecondLevelAdapter sadpt) {
this.cls = cls;
this.sadpt = sadpt;
}
}
public Entry[] lsfirst;
// you can change the constructor depending on which listeners you wan't to use.
public RootAdapter(Context context, UserObject root, ExpandableListView.OnGroupClickListener grpLst,
final ExpandableListView.OnChildClickListener childLst, ExpandableListView.OnGroupExpandListener grpExpLst) {
this.root = root;
this.listLayoutInflater = LayoutInflater.from(context);
lsfirst = new Entry[root.mChildren.size()];
for (int i = 0; i < root.mChildren.size(); i++) {
final CustExpListview celv = new CustExpListview(context);
SecondLevelAdapter adp = new SecondLevelAdapter(root.mChildren.get(i),context);
celv.setLongClickable(true);
celv.setAdapter(adp);
celv.setGroupIndicator(null);
celv.setOnChildClickListener(childLst);
celv.setOnGroupClickListener(grpLst);
celv.setOnGroupExpandListener(grpExpLst);
lsfirst[i] = new Entry(celv, adp);
}
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return root.mChildren.get(groupPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
// second level list
return lsfirst[groupPosition].cls;
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public UserObject getGroup(int groupPosition) {
return root.mChildren.get(groupPosition);
}
@Override
public int getGroupCount() {
return root.mChildren.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
// first level
View layout = convertView;
GroupViewHolder holder;
final UserObject item = (UserObject) getGroup(groupPosition);
if (layout == null) {
layout = listLayoutInflater.inflate(R.layout.root_element, parent, false);
holder = new GroupViewHolder();
holder.title = (TextView) layout.findViewById(R.id.rootTitle);
layout.setTag(holder);
} else {
holder = (GroupViewHolder) layout.getTag();
}
holder.title.setText(item.mUserName.trim());
return layout;
}
private static class GroupViewHolder {
TextView title;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
然后是我的二级适配器实现(即:叶节点)
public class SecondLevelAdapter extends BaseExpandableListAdapter {
public UserObject child;
Context mContext;
LayoutInflater inflater;
public SecondLevelAdapter(UserObject child,Context context) {
this.child = child;
this.mContext=context;
inflater = LayoutInflater.from(mContext);
}
@Override
public UserObject getChild(int groupPosition, int childPosition) {
return child.mChildren.get(groupPosition).mChildren.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
// third level
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
View layout = convertView;
final UserObject item = (UserObject) getChild(groupPosition, childPosition);
ChildViewHolder holder;
if (layout == null) {
layout = inflater.inflate(R.layout.item_child, parent, false);
holder = new ChildViewHolder();
holder.title = (TextView) layout.findViewById(R.id.childTitle);
layout.setTag(holder);
} else {
holder = (ChildViewHolder) layout.getTag();
}
holder.title.setText(item.mUserName.trim());
return layout;
}
@Override
public int getChildrenCount(int groupPosition) {
return child.mChildren.get(groupPosition).mChildren.size();
}
@Override
public UserObject getGroup(int groupPosition) {
return child.mChildren.get(groupPosition);
}
@Override
public int getGroupCount() {
return child.mChildren.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
// Second level
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
View layout = convertView;
ViewHolder holder;
final UserObject item = (UserObject) getGroup(groupPosition);
if (layout == null) {
layout = inflater.inflate(R.layout.root_element, parent, false);
holder = new ViewHolder();
holder.title = (TextView) layout.findViewById(R.id.rootTitle);
layout.setTag(holder);
} else {
holder = (ViewHolder) layout.getTag();
}
holder.title.setText(item.mUserName.trim());
return layout;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
super.registerDataSetObserver(observer);
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
Log.d("SecondLevelAdapter", "Unregistering observer");
if (observer != null) {
super.unregisterDataSetObserver(observer);
}
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private static class ViewHolder {
TextView title;
}
private static class ChildViewHolder {
TextView title;
}
}
然后我在主 Activity 中调用此代码:
public class Test extends navBar {
private List<UserObject> mUserObjects;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View base = getLayoutInflater().inflate(R.layout.activity_admin__tool, frameLayout);
mUserObjects = new ArrayList<>();
UserObject obj = new UserObject();
obj.mChildren = new ArrayList<>();
for(int i = 0;i<Constant.state.length;i++) {
root = new UserObject();
root.mUserName = Constant.state[i];
root.mChildren = new ArrayList<>();
for (int j = 0; j < Constant.parent[i].length; j++) {
UserObject parent = new UserObject();
parent.mUserName = Constant.parent[i][j];
parent.mChildren = new ArrayList<>();
for (int k = 0; k < Constant.child[i][j].length; k++) {
UserObject child = new UserObject();
child.mUserName = Constant.child[i][j][k];
UserObject test = new UserObject();
test.mUserName = "test";
child.mChildren.add(test);
parent.mChildren.add(child);
}
root.mChildren.add(parent);
}
obj.mChildren.add(root);
}
if (!obj.mChildren.isEmpty()) {
final ExpandableListView elv = (ExpandableListView) base.findViewById(R.id.expandableListView);
/* Item click listeners below */
try {
// First level items in the ExpandableListView
elv.setLongClickable(true);
elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView eListView, View view, int groupPosition,
long id) {
ImageView tv = (ImageView) view.findViewById(R.id.rootImage);
if(!eListView.isGroupExpanded(groupPosition)){
tv.setImageResource(R.drawable.ic_expand_less_black_24dp);
}
else
tv.setImageResource(R.drawable.ic_expand_more_black_24dp);
return false /* or true depending on what you need */;
}
});
elv.setOnItemLongClickListener(new ExpandableListView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View childView, int flatPos, long id) {
if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
// do whatever you want with groupPos and childPos here - I used these to get my object from list adapter.
return false;
}
return true;
}
});
}
catch (Exception e){
Log.e("Admin_Tool",e.getMessage());
}
// Second level items in the ExpandableListView
ExpandableListView.OnGroupClickListener grpLst = new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView eListView, View view, int groupPosition,
long id) {
ImageView tv = (ImageView) view.findViewById(R.id.rootImage);
if(!eListView.isGroupExpanded(groupPosition)){
tv.setImageResource(R.drawable.ic_expand_less_black_24dp);
}
else
tv.setImageResource(R.drawable.ic_expand_more_black_24dp);
return false /* or true depending on what you need */;
}
};
// Third (and last) level items in the ExpandableListView
ExpandableListView.OnChildClickListener childLst = new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView eListView, View view, int groupPosition,
int childPosition, long id) {
// TODO: whatever you need
return false /* or true depending on what you need */;
}
};
ExpandableListView.OnGroupExpandListener grpExpLst = new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
/* this one is not required of course, you can delete it from the RootAdapter Constructor
* it is just an example as to how to implement Listeners on the second level items */
}
};
final RootAdapter adapter = new RootAdapter(this, obj, grpLst, childLst, grpExpLst);
elv.setAdapter(adapter);
}
}
最后,常量设置如下:
public class Constant {
static String[] state = {"A","B","C"};
static String[][] parent = {
{"aa","bb","cc","dd","ee"},
{"ff","gg","hh","ii","jj"},
{"kk","ll","mm","nn","oo"}
};
static String[][][] child = {
{
{"aaa","aab","aac","aad","aae"},
{"bba","bbb","bbc","bbd","bbe"},
{"cca","ccb","ccc","ccd","cce","ccf","ccg"},
{"dda","ddb","dddc","ddd","dde","ddf"},
{"eea","eeb","eec"}
},
{
{"ffa","ffb","ffc","ffd","ffe"},
{"gga","ggb","ggc","ggd","gge"},
{"hha","hhb","hhc","hhd","hhe","hhf","hhg"},
{"iia","iib","iic","iid","iie","ii"},
{"jja","jjb","jjc","jjd"}
},
{
{"kka","kkb","kkc","kkd","kke"},
{"lla","llb","llc","lld","lle"},
{"mma","mmb","mmc","mmd","mme","mmf","mmg"},
{"nna","nnb","nnc","nnd","nne","nnf"},
{"ooa","oob"}
}
};
}
我将“test”作为节点添加到所有第三级节点,以确保它能够测试第四级。所以一个例子是 a->aa->aaa->testTest 永远不会显示,并且 aaa 始终被视为最终节点。我如何通过三个级别?
最佳答案
checkout 此库:AndroidTreeView
向您的项目添加依赖项
compile 'com.github.bmelnychuk:atv:1.2.+'
创建你的树
TreeNode root = TreeNode.root();
添加节点(使用自定义对象作为构造函数参数)
TreeNode parent = new TreeNode("MyParentNode");
TreeNode child0 = new TreeNode("ChildNode0");
TreeNode child1 = new TreeNode("ChildNode1");
parent.addChildren(child0, child1);
root.addChild(parent);
将 TreeView 添加到布局
AndroidTreeView tView = new AndroidTreeView(getActivity(), root);
containerView.addView(tView.getView());
关于java - 无限级别 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39993239/
Android 项目中最低(最低 sdk)和最高(目标 sdk)级别是否有任何影响。这些东西是否会影响项目的可靠性和效率。 最佳答案 没有影响,如果您以 SDK 级别 8 为目标,那么您的应用将以 9
我将现有的 android 项目升级到 API 级别 31。我使用 Java 作为语言。我改变了 build.gradle compileSdkVersion 31 defaultConfig {
我正在使用 ionic 3 创建一个 android 应用程序,当我尝试上传到 playstore 时,我收到一个错误,提示我的应用程序以 api 25 为目标,当我检查我的 project.prop
我刚刚尝试将应用程序的目标和编译 API 级别更新为 29 (Android 10),并注意到我无法再编译,因为 LocationManager.addNmeaListener 只接受 OnNmeaM
我的代码没有在 Kitkat 上显示工具栏。 这是我的两个 Android 版本的屏幕截图。 Kitkat 版本: Lollipop 版: 这背后的原因可能是什么。 list 文件
我正在构建面向 API 级别 8 的 AccessabilityService,但我想使用 API 级别 18 中引入的功能 (getViewIdResourceName())。这应该可以通过使用 A
当我想在我的电脑上创建一个 android 虚拟机时,有两个选项可以选择目标设备。它们都用于相同的 API 级别。那么我应该选择哪一个呢?它们之间有什么区别? 最佳答案 一个是基本的 Android,
当我选择 tagret 作为 Android 4.2.2(API 级别 17)时,模拟器需要很长时间来加载和启动。 所以我研究它并通过使用 找到了解决方案Intel Atom(x86) 而不是 ARM
我有一个使用 Android Studio 创建的 Android 项目。我在项目中添加了一些第三方依赖项,但是当我尝试在 Android Studio 中编译时,我遇到了以下错误: Error:Ex
如上所述,如何使用 API 8 获取移动设备网络接口(interface)地址? 最佳答案 NetworkInterface.getInetAddresses() 在 API8 中可用。 关于andr
我想显示 Snackbar并使用图像而不是文本进行操作。 我使用以下代码: val imageSpan = ImageSpan(this, R.drawable.star) val b
我有一个用 python 编写的简单命令行程序。程序使用按以下方式配置的日志记录模块将日志记录到屏幕: logging.basicConfig(level=logging.INFO, format='
使用下面的代码,实现游戏状态以控制关卡的最简单和最简单的方法是什么?如果我想从标题画面开始,然后加载一个关卡,并在完成后进入下一个关卡?如果有人能解释处理这个问题的最简单方法,那就太好了! impor
我想创建一个可以找到嵌套树结构深度的属性。下面的静态通过递归找出深度/级别。但是是否可以将此函数作为同一个类中的属性而不是静态方法? public static int GetDepth(MenuGr
var myArray = [{ title: "Title 1", children: [{ title: "Title 1.1", children: [{
通过下面的代码,实现游戏状态来控制关卡的最简单、最容易的方法是什么?如果我想从标题屏幕开始,然后加载一个关卡,并在完成后进入下一个关卡?如果有人可以解释处理这个问题的最简单方法,那就太好了! impo
我有一个树结构,其中每个节点基本上可以有无限个子节点,它正在为博客的评论建模。 根据特定评论的 ID,我试图找出该评论在树中的深度/级别。 我正在关注 this guide that explains
考虑任何给定的唯一整数的数组,例如[1,3,2,4,6,5] 如何确定“排序度”的级别,范围从 0.0 到 1.0 ? 最佳答案 一种方法是评估必须移动以使其排序的项目数量,然后将其除以项目总数。 作
我如何定义一个模板类,它提供一个整数常量,表示作为输入模板参数提供的(指针)类型的“深度”?例如,如果类名为 Depth,则以下内容为真: Depth::value == 3 Depth::value
我的场景是:文件接收器应该包含所有内容。另一个接收器应包含信息消息,但需要注意的是 Microsoft.* 消息很烦人,因此这些消息应仅限于警告。两个sink怎么单独配置?我尝试的第一件事是: str
我是一名优秀的程序员,十分优秀!