gpt4 book ai didi

Android 使用 expandableListView 显示列表

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:19 26 4
gpt4 key购买 nike

我已经按照这个示例教程开发了一个示例应用程序 http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

enter image description here

如果我点击 Core 我想在 Core 下的同一屏幕中加载一个列表,我该怎么做?使用我下面的代码我能够在另一个屏幕中加载子列表,有帮助吗?

我的 ChildClickListener 代码在这里:

        @Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub

Log.d("onChildClick", "onChildClick");

String position = (String) parentItems.get(groupPosition);
Log.d("position", position);

String child = listDataChild.get(position).get(childPosition);

Log.d("child", child);

if (child.equalsIgnoreCase("Core")) {

ArrayList<String> parentItems = new ArrayList<String>();

HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();
ArrayList<String> childItems = new ArrayList<String>();
childItems.add("corejava");
childItems.add("corejava");
childItems.add("corejava");
childItems.add("corejava");

parentItems.add(position);
listDataChild.put(parentItems.get(0), childItems);
expandableList = (ExpandableListView) findViewById(R.id.lvExp);

CoreAdapter adapter = new CoreAdapter(parentItems,
listDataChild);
CustExpListview SecondLevelexplv = new CustExpListview(
MainActivity.this);
adapter.setInflater(
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
MainActivity.this);

SecondLevelexplv.setAdapter(adapter);
SecondLevelexplv.setGroupIndicator(null);

expandableList.setAdapter(adapter);

}

最佳答案

我为您编写了一个 treeView 适配器,您可以使用它并根据需要放置多个级别,为此您只需复制以下代码:

主要 Activity :

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// add data for test on one list<AccountHierarchical>


AccountHierarchical obj = new AccountHierarchical();
obj.setLevelId(1);
obj.setTitle("level 1");


AccountHierarchical obj2 = new AccountHierarchical();
obj2.setLevelId(1);
obj2.setTitle("level 2");


AccountHierarchical obj3 = new AccountHierarchical();
obj3.setLevelId(1);
obj3.setTitle("level 3");


List<AccountHierarchical> emptyList = new ArrayList<AccountHierarchical>();
obj3.setList(emptyList);

List<AccountHierarchical> list2 = new ArrayList<AccountHierarchical>();
list2.add(obj3);list2.add(obj3);list2.add(obj3);list2.add(obj3);list2.add(obj3);
obj2.setList(list2);

List<AccountHierarchical> list = new ArrayList<AccountHierarchical>();
list.add(obj2);list.add(obj2);list.add(obj2);list.add(obj2);list.add(obj2);
obj.setList(list);


List<AccountHierarchical> result = new ArrayList<AccountHierarchical>();
result.add(obj);result.add(obj);result.add(obj);result.add(obj);



// create Adapter
TreeView adapter = new TreeView(result, this,
getLayoutInflater() , false, 0);

ExpandableListView expandList = (ExpandableListView)findViewById(R.id.expandableList_tree);
expandList.setGroupIndicator(null);
expandList.setAdapter(adapter);
}

客户 ListView :

public class CustExpListview extends ExpandableListView {

int intGroupPosition, intChildPosition, intGroupid;

public CustExpListview(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// widthMeasureSpec = MeasureSpec.makeMeasureSpec(400,
// MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(600,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}


}

帐户层次结构:

public class AccountHierarchical {

List<AccountHierarchical> list;
private String Title;
private int Id;


public int getId() {
return Id;
}

public void setLevelId(int Id) {
this.Id = Id;
}

public String getTitle() {
return Title;
}

public void setTitle(String title) {
Title = title;
}

public List<AccountHierarchical> getList() {
return list;
}

public void setList(List<AccountHierarchical> list) {
this.list = list;
}

}

TreeView :

public class TreeView extends BaseExpandableListAdapter implements
OnClickListener {

List<AccountHierarchical> list;
LayoutInflater inflatter;
static Context context;
boolean checkGroup;
int position;

static ProgressDialog ProgressDialog;

public TreeView( List<AccountHierarchical> list, Context context,
LayoutInflater inflatter, boolean checkGroup, int position
) {
this.list = list;
TreeView.context = context;
this.inflatter = inflatter;
this.checkGroup = checkGroup; // this is true when you call from inner.
this.position = position;



}

@Override
public Object getChild(int groupPosition, int childPosition) {

return list.get(groupPosition).getList().get(childPosition);
}

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

return list.get(groupPosition).getList().get(childPosition).getId();
}

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

if (checkGroup)
groupPosition = position;




List<AccountHierarchical> childtemp = list.get(groupPosition)
.getList();

// call this adapter again for creating another level
if (childtemp.get(childPosition).getList().size() > 0) {
CustExpListview SecondLevelexplv = new CustExpListview(context);
TreeView adapter = null;

adapter = new TreeView(childtemp, context,
inflatter, true, childPosition);
SecondLevelexplv.setGroupIndicator(null);
SecondLevelexplv.setAdapter(adapter);
return SecondLevelexplv;
}
// call one layout, this is last child
else {

convertView = inflatter.inflate(R.layout.grouprow, null);
TextView tv = (TextView) convertView.findViewById(R.id.grouprow);



tv.setText(list.get(groupPosition).getList().get(childPosition)
.getTitle());
tv.setPadding(0, 0, 20, 0);
convertView.setTag(list.get(groupPosition).getList()
.get(childPosition) );
convertView.setId(position);
convertView.setOnClickListener(this);
return convertView;
}

}

@Override
public int getChildrenCount(int groupPosition) {
return list.get(groupPosition).getList().size();
}

@Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition);
}

@Override
public int getGroupCount() {

if (checkGroup)
return 1;

return list.size();
}

@Override
public long getGroupId(int groupPosition) {
return list.get(groupPosition).getId();
}

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

if (convertView == null)
convertView = inflatter.inflate(R.layout.grouprow, null);

TextView tv = (TextView) convertView.findViewById(R.id.grouprow);

if (checkGroup)
tv.setText(list.get(position).getTitle());
else
tv.setText(list.get(groupPosition).getTitle());

return convertView;
}

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

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {

return false;
}

@Override
public void onClick(View v) {
// your onClick method
}

}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<ExpandableListView
android:id="@+id/expandableList_tree"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >

</ExpandableListView>

</RelativeLayout>

组行.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/grouprow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:text=""/>



</RelativeLayout>

快照:

enter image description here

关于Android 使用 expandableListView 显示列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22190496/

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