Android 多级可扩展列表视图
我正在尝试向父groupPosition[0]添加值。但它反映到父列表项的所有组位置。请帮帮我,伙计们Android 多级可扩展列表视图,android,android-expandable-list-view,Android,Android Expandable List View,我正在尝试向父groupPosition[0]添加值。但它反映到父列表项的所有组位置。请帮帮我,伙计们 public class MainActivity extends Activity { ExpandableListView explvlist; ArrayList<String> StringArray = new ArrayList<String>(); ArrayList<String> StringArray
public class MainActivity extends Activity
{
ExpandableListView explvlist;
ArrayList<String> StringArray = new ArrayList<String>();
ArrayList<String> StringArrayChild=new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayValueAddFunction();
ArrayValueAddFunction_Level2();
explvlist = (ExpandableListView)findViewById(R.id.ParentLevel);
explvlist.setAdapter(new ParentLevel(StringArray));
}
private void ArrayValueAddFunction_Level2() {
StringArrayChild.add("Level1 item1");
StringArrayChild.add("Level1 item2");
StringArrayChild.add("Level1 item3");
StringArrayChild.add("Level1 item4");
StringArrayChild.add("Level1 item5");
StringArrayChild.add("Level1 item6");
}
private void ArrayValueAddFunction() {
StringArray.add("ONE");
StringArray.add("TWO");
StringArray.add("THREE");
StringArray.add("FOUR");
StringArray.add("FIVE");
StringArray.add("SIX");
}
public class ParentLevel extends BaseExpandableListAdapter {
ArrayList<String> StringArray = new ArrayList<String>();
private Context context;
public ParentLevel(ArrayList<String> stringArray) {
this.StringArray=stringArray;
}
@Override
public Object getChild(int arg0, int arg1) {
return arg1;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
CustExpListview SecondLevelexplv = new CustExpListview(MainActivity.this);
SecondLevelexplv.setAdapter(new SecondLevelAdapter());
SecondLevelexplv.setGroupIndicator(null);
return SecondLevelexplv;
}
@Override
public int getChildrenCount(int groupPosition) {
return 3;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return StringArray.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView tv = new TextView(MainActivity.this);
if (groupPosition==0) {
tv.append(StringArray.get(0));
}else if (groupPosition==1){
tv.append(StringArray.get(1));
}else if (groupPosition==2){
tv.append(StringArray.get(2));
}else if (groupPosition==3){
tv.append(StringArray.get(3));
}else if (groupPosition==4){
tv.append(StringArray.get(4));
}else if (groupPosition==5){
tv.append(StringArray.get(5));
}
return tv;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public class SecondLevelAdapter extends BaseExpandableListAdapter
{
@Override
public Object getChild(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
TextView tv = new TextView(MainActivity.this);
tv.setText("child");
tv.setPadding(15, 5, 5, 5);
tv.setBackgroundColor(Color.GRAY);
tv.setLayoutParams(new ListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
return tv;
}
@Override
public int getChildrenCount(int groupPosition) {
return 6;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return 1;
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
TextView tv = new TextView(MainActivity.this);
if (groupPosition==0) {
tv.append(StringArrayChild.get(0));
}else if (groupPosition==1){
tv.append(StringArrayChild.get(1));
}else if (groupPosition==2){
tv.append(StringArrayChild.get(2));
}else if (groupPosition==3){
tv.append(StringArrayChild.get(3));
}else if (groupPosition==4){
tv.append(StringArrayChild.get(4));
}else if (groupPosition==5){
tv.append(StringArrayChild.get(5));
}
tv.setPadding(12, 7, 7, 7);
tv.setBackgroundColor(Color.YELLOW);
return tv;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
}
}
基于可扩展ListView的布局设计
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<ExpandableListView
android:layout_width="fill_parent"
android:id="@+id/ParentLevel"
android:groupIndicator="@null"
android:layout_height="fill_parent">
</ExpandableListView>
</LinearLayout>
下面给出的图像是输出,所有项目都进入所有位置,但我需要放置位置[0],但它反映到父列表的所有groupPositins
我认为问题发生在
getGroupView
上groupPosition
始终返回0。检查您的代码以了解该问题。
顺便说一句,你可以用这个。谢谢兄弟,谢谢你宝贵的回复。我只需要根1和根2的扩展子项,其余根我只需要子项。。我急切地等待你的回复你的代码是完全错误的,它使一些不必要的循环。有许多方法可以使
NLevelExpandable列表视图成为可扩展列表视图,例如,检查如何创建可扩展的NLevel列表。@sadraisapanahamlash是否有任何关于为github链接折叠所有项并将所有项一起展开的提示?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<ExpandableListView
android:layout_width="fill_parent"
android:id="@+id/ParentLevel"
android:groupIndicator="@null"
android:layout_height="fill_parent">
</ExpandableListView>
</LinearLayout>