gpt4 book ai didi

java - 无限级别 ListView

转载 作者:行者123 更新时间:2023-12-02 03:18:19 24 4
gpt4 key购买 nike

我正在尝试使用可扩展 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/

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