如何在 android 的 ExpandableListView 中添加三级 ListView

Posted

技术标签:

【中文标题】如何在 android 的 ExpandableListView 中添加三级 ListView【英文标题】:How to add Three Level ListView in ExpandableListView in android 【发布时间】:2015-12-29 01:50:41 【问题描述】:

我想在 ExpandableListView 中再添加一层。在当前的 ExpandableListView 是两层时,我该如何再添加一层。我是android开发的新手,请帮助我。提前致谢!

我的 MainActivity.java:-

 private Toolbar mToolbar;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;

//   private FragmentDrawer drawerFragment;
ArrayList<String> arraylist1,arraylist2,arraylist3,arrayList4,arrayList5;
int cnt = 0;

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

    mToolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    expListView = (ExpandableListView) findViewById(R.id.lvExp);

    // preparing list data
    prepareListData();

    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);

   /*      drawerFragment = (FragmentDrawer)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
    drawerFragment.setDrawerListener(this);*/
    // display the first navigation drawer view on app launch
  //      displayView(0);
    Log.d("oncreate method", "");
    arraylist1  = new ArrayList<String>();
    arraylist2  = new ArrayList<String>();
    arraylist3  = new ArrayList<String>();
    arrayList4 =  new ArrayList<String>();
    arrayList5 =  new ArrayList<String>();
    new ProductsAsynTask().execute("http://opencart.codeniques.com/shopping/?route=feed/web_api/menu&key=test123$");



private void prepareListData() 
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();


    // Adding child data
    listDataHeader.add("Top 250");
    listDataHeader.add("Now Showing");
    listDataHeader.add("Coming Soon..");

    // Adding child data
    List<String> top250 = new ArrayList<String>();
    top250.add("The Shawshank Redemption");
    top250.add("The Godfather");
    top250.add("The Godfather: Part II");
    top250.add("Pulp Fiction");
    top250.add("The Good, the Bad and the Ugly");
    top250.add("The Dark Knight");
    top250.add("12 Angry Men");

    List<String> nowShowing = new ArrayList<String>();
    nowShowing.add("The Conjuring");
    nowShowing.add("Despicable Me 2");
    nowShowing.add("Turbo");
    nowShowing.add("Grown Ups 2");
    nowShowing.add("Red 2");
    nowShowing.add("The Wolverine");

    List<String> comingSoon = new ArrayList<String>();
    comingSoon.add("2 Guns");
    comingSoon.add("The Smurfs 2");
    comingSoon.add("The Spectacular Now");
    comingSoon.add("The Canyons");
    comingSoon.add("Europa Report");

    listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
    listDataChild.put(listDataHeader.get(1), nowShowing);
    listDataChild.put(listDataHeader.get(2), comingSoon);


public class ProductsAsynTask extends AsyncTask<String,Void,Void>

    ProgressDialog dialog;

    protected void onPreExecute()
        super.onPreExecute();
        Log.d("In onPreExceute","");
        dialog = new ProgressDialog(MainActivity.this);
        dialog.setMessage("Loading, Please wait");
        dialog.setTitle("Connecting server");
        dialog.show();
        dialog.setCancelable(false);
    

    protected Void doInBackground(String... param)
        try
            Log.d("In doInBackground","");

            HttpClient client= new DefaultHttpClient();
            HttpPost post = new HttpPost(param[0]);
            HttpResponse response = client.execute(post);

            int status = response.getStatusLine().getStatusCode();

            if(status == 200)
                Log.d("Status",""+status);

                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);

                JSONObject jsonObject = new JSONObject(data);
                JSONArray jArray = jsonObject.getJSONArray("categories");

                for(int i = 0;i < jArray.length();i++)
                    cnt++;
                    Log.d("value of array",jArray.length()+"");
                    Log.d("Value of i",""+i);

                    JSONObject jsonObject1 = jArray.getJSONObject(i);


                    arraylist1.add(jsonObject1.getString("name"));
                    //data1 = jsonObject1.getString("name");
              //      Log.d("hello ",data1);

                    JSONArray jsonArray = jsonObject1.getJSONArray("children");
         //           JSONObject jsonObject2 = jsonObject1.getJSONObject("children");

                    for(int j=0;j<jsonArray.length();j++)

                        JSONObject jsonObject2 = jsonArray.getJSONObject(j);

                        arraylist2.add(jsonObject2.getString("name"));
                      //  data2 = jsonObject2.getString("name");
                        JSONArray jsonArray1 = jsonObject2.getJSONArray("children_lv3");

                        for(int k=0;k<jsonArray1.length();k++)
                            JSONObject jsonObject3 = jsonArray1.getJSONObject(k);

                            arraylist3.add(jsonObject3.getString("name"));
                            arrayList4.add(jsonObject3.getString("href"));
                           /*  data3 = jsonObject3.getString("name");
                             data4 = jsonObject3.getString("href");   */
                        
                        arrayList5.add(jsonObject2.getString("href"));
                      // data5 = jsonObject2.getString("href");
                    
                

            
        catch(IOException e)
            Log.e("Error IOException :",e.getMessage());
        catch (JSONException e)
            Log.e("Error JSONException",e.getMessage());
        
            return null;
    

    protected void onPostExecute(Void aVoid) 
        super.onPostExecute(aVoid);
        dialog.dismiss();
        Log.d("Counter value",""+cnt);
    


@Override
public boolean onCreateOptionsMenu(Menu menu) 
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;


@Override
public boolean onOptionsItemSelected(MenuItem item) 
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) 
        return true;
    

    if(id == R.id.action_search)
        Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
        return true;
    

    return super.onOptionsItemSelected(item);



 /*  public void onDrawerItemSelected(View view, int position) 
    displayView(position);


private void displayView(int position) 
    Fragment fragment = null;
    String title = getString(R.string.app_name);
    switch (position) 
        case 0:

           fragment = new HomeFragment();
           title = getString(R.string.title_home);
            break;
        case 1:
            fragment = new FriendsFragment();
            title = getString(R.string.title_friends);
            break;
        case 2:
            fragment = new MessagesFragment();
            title = getString(R.string.title_messages);
            break;
        default:
            break;
    

    if (fragment != null) 
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.commit();

        // set the toolbar title
        getSupportActionBar().setTitle(title);
    
*/

我的 ExpandableListAdapter.java 在这里:-

public class ExpandableListAdapter extends BaseExpandableListAdapter  

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

public ExpandableListAdapter(Context context, List<String> listDataHeader,
                             HashMap<String, List<String>> listChildData) 
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;


@Override
public Object getChild(int groupPosition, int childPosititon) 
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);


@Override
public long getChildId(int groupPosition, int childPosition) 
    return childPosition;


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

    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) 
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.lblListItem);

    txtListChild.setText(childText);
    return convertView;


@Override
public int getChildrenCount(int groupPosition) 
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .size();


@Override
public Object getGroup(int groupPosition) 
    return this._listDataHeader.get(groupPosition);


@Override
public int getGroupCount() 
    return this._listDataHeader.size();


@Override
public long getGroupId(int groupPosition) 
    return groupPosition;


@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) 
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) 
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);
    

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return convertView;


@Override
public boolean hasStableIds() 
    return false;


@Override
public boolean isChildSelectable(int groupPosition, int childPosition) 
    return true;

我的 activity_main.xml 在这里:-

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_
android:layout_>


<LinearLayout
    android:layout_
    android:layout_
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/container_toolbar"
        android:layout_
        android:layout_
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />


    </LinearLayout>

    <FrameLayout
        android:id="@+id/container_body"
        android:layout_
        android:layout_
        android:layout_weight="1" />




</LinearLayout>

<ExpandableListView
    android:id="@+id/lvExp"
    android:layout_
    android:layout_
    android:layout_gravity="start"/>
    <!-- <fragment
    android:id="@+id/fragment_navigation_drawer"
    android:name="com.android.ShoppingMazza.activity.ExpandableListAdapter"
    android:layout_
    android:layout_
    android:layout_gravity="start"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" /> -->
  </android.support.v4.widget.DrawerLayout>

【问题讨论】:

结帐github.com/shahbazhashmi/nested-expandable-recyclerview 【参考方案1】:

你可以试试我下面的示例代码。我已将我的完整项目发布到GitHub

当然,您应该进行更多修改以满足您的所有要求。对于基本情况,我只使用arrays.xml 文件中的数据源。希望这会有所帮助!

arrays.xml:

<resources>
    <string-array name="items_array_expandable_level_one">
        <item>Level 1.1</item>
        <item>Level 1.2</item>
        <item>Level 1.3</item>
    </string-array>
    <string-array name="items_array_expandable_level_one_one_child">
        <item>Level 1.1.1</item>
        <item>Level 1.1.2</item>
    </string-array>
    <string-array name="items_array_expandable_level_one_two_child">
        <item>Level 1.2.1</item>
    </string-array>
    <string-array name="items_array_expandable_other_child">
        <item>Second Level 01</item>
        <item>Second Level 02</item>
        <item>Second Level 03</item>
    </string-array>
    <string-array name="items_array_expandable_level_three">
        <item>Child Level 01</item>
        <item>Child Level 02</item>
    </string-array>
</resources>

CustomExpListView.java:

public class CustomExpListView extends ExpandableListView

    public CustomExpListView(Context context)
    
        super(context);
    
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    

ParentLevelAdapter.java:

public class ParentLevelAdapter extends BaseExpandableListAdapter 
    private final Context mContext;
    private final List<String> mListDataHeader;
    private final Map<String, List<String>> mListData_SecondLevel_Map;
    private final Map<String, List<String>> mListData_ThirdLevel_Map;
    public ParentLevelAdapter(Context mContext, List<String> mListDataHeader) 
        this.mContext = mContext;
        this.mListDataHeader = new ArrayList<>();
        this.mListDataHeader.addAll(mListDataHeader);
        // Init second level data
        String[] mItemHeaders;
        mListData_SecondLevel_Map = new HashMap<>();
        int parentCount = mListDataHeader.size();
        for (int i = 0; i < parentCount; i++) 
            String content = mListDataHeader.get(i);
            switch (content) 
                case "Level 1.1":
                    mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_level_one_one_child);
                    break;
                case "Level 1.2":
                    mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_level_one_two_child);
                    break;
                default:
                    mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_other_child);
            
            mListData_SecondLevel_Map.put(mListDataHeader.get(i), Arrays.asList(mItemHeaders));
        
        // THIRD LEVEL
        String[] mItemChildOfChild;
        List<String> listChild;
        mListData_ThirdLevel_Map = new HashMap<>();
        for (Object o : mListData_SecondLevel_Map.entrySet()) 
            Map.Entry entry = (Map.Entry) o;
            Object object = entry.getValue();
            if (object instanceof List) 
                List<String> stringList = new ArrayList<>();
                Collections.addAll(stringList, (String[]) ((List) object).toArray());
                for (int i = 0; i < stringList.size(); i++) 
                    mItemChildOfChild = mContext.getResources().getStringArray(R.array.items_array_expandable_level_three);
                    listChild = Arrays.asList(mItemChildOfChild);
                    mListData_ThirdLevel_Map.put(stringList.get(i), listChild);
                
            
        
    
    @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) 
        final CustomExpListView secondLevelExpListView = new CustomExpListView(this.mContext);
        String parentNode = (String) getGroup(groupPosition);
        secondLevelExpListView.setAdapter(new SecondLevelAdapter(this.mContext, mListData_SecondLevel_Map.get(parentNode), mListData_ThirdLevel_Map));
        secondLevelExpListView.setGroupIndicator(null);
        return secondLevelExpListView;
    
    @Override
    public int getChildrenCount(int groupPosition) 
        return 1;
    
    @Override
    public Object getGroup(int groupPosition) 
        return this.mListDataHeader.get(groupPosition);
    
    @Override
    public int getGroupCount() 
        return this.mListDataHeader.size();
    
    @Override
    public long getGroupId(int groupPosition) 
        return groupPosition;
    
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) 
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) 
            LayoutInflater layoutInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.drawer_list_group, parent, false);
        
        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setTextColor(Color.CYAN);
        lblListHeader.setText(headerTitle);
        return convertView;
    
    @Override
    public boolean hasStableIds() 
        return true;
    
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) 
        return true;
     

SecondLevelAdapter.java:

public class SecondLevelAdapter extends BaseExpandableListAdapter

    private final Context mContext;
    private final List<String> mListDataHeader;
    private final Map<String, List<String>> mListDataChild;
    public SecondLevelAdapter(Context mContext, List<String> mListDataHeader, Map<String, List<String>> mListDataChild) 
        this.mContext = mContext;
        this.mListDataHeader = mListDataHeader;
        this.mListDataChild = mListDataChild;
    
    @Override
    public Object getChild(int groupPosition, int childPosition)
    
        return this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
                .get(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)
    
        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) 
            LayoutInflater layoutInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.drawer_list_item, parent, false);
        
        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);
        txtListChild.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
        txtListChild.setText(childText);
        return convertView;
    
    @Override
    public int getChildrenCount(int groupPosition)
    
        try 
            return this.mListDataChild.get(this.mListDataHeader.get(groupPosition)).size();
         catch (Exception e) 
            return 0;
        
    
    @Override
    public Object getGroup(int groupPosition)
    
        return this.mListDataHeader.get(groupPosition);
    
    @Override
    public int getGroupCount()
    
        return this.mListDataHeader.size();
    
    @Override
    public long getGroupId(int groupPosition)
    
        return groupPosition;
    
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent)
    
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) 
            LayoutInflater layoutInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.drawer_list_group_second, parent, false);
        
        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setText(headerTitle);
        lblListHeader.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        lblListHeader.setTextColor(Color.YELLOW);
        return convertView;
    
    @Override
    public boolean hasStableIds() 
        return true;
    
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) 
        return true;
    

MainActivity.java:

public class MainActivity extends AppCompatActivity 
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        // Init top level data
        List<String> listDataHeader = new ArrayList<>();
        String[] mItemHeaders = getResources().getStringArray(R.array.items_array_expandable_level_one);
        Collections.addAll(listDataHeader, mItemHeaders);
        ExpandableListView mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView_Parent);
        if (mExpandableListView != null) 
            ParentLevelAdapter parentLevelAdapter = new ParentLevelAdapter(this, listDataHeader);
            mExpandableListView.setAdapter(parentLevelAdapter);
        
    

截图结果:

【讨论】:

此列表包含我想要在工具栏下方列出的全宽 当然,如截图,action bar下方的列表,如果要全高屏,用NavigationView代替 @bc120201817ZohaibSiddiq,在return secondLevelExpListView;行上方,可以加secondLevelExpListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) String seletectText = ((TextView) v).getText().toString().toLowerCase(); Toast.makeText(mContext, seletectText, Toast.LENGTH_SHORT).show(); return false; ); 谢谢,这(与 github 存储库一起)非常有帮助。有时我也希望看到使用游标实现它;例如使用 SimpleCursorTreeAdapter。这样数据就可以来自数据库。 @LarsH 谢谢,其实我现在正忙于我的项目,所以在未来,如果您实施或找到该解决方案,请分享:)【参考方案2】:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_
    android:layout_>

    <ExpandableListView
     android:layout_
     android:id="@+id/ParentLevel"
     android:groupIndicator="@null"
     android:layout_>
    </ExpandableListView>

</LinearLayout>

MainActivity.Java

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;

import android.widget.TextView;

import android.widget.LinearLayout.LayoutParams;


public class Home extends Activity 


ExpandableListView explvlist;  
@Override
public void onCreate(Bundle savedInstanceState) 

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    explvlist = (ExpandableListView)findViewById(R.id.ParentLevel);
    explvlist.setAdapter(new ParentLevel());



public class ParentLevel extends BaseExpandableListAdapter


      @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(Home.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 5;
      

      @Override
      public long getGroupId(int groupPosition) 
         
       return groupPosition;
      

      @Override
      public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) 
      
       TextView tv = new TextView(Home.this);
       tv.setText("->FirstLevel");
       tv.setBackgroundColor(Color.BLUE);
       tv.setPadding(10, 7, 7, 7); 

       return tv;
      

  @Override
  public boolean hasStableIds() 
  
   return true;
  

    @Override
  public boolean isChildSelectable(int groupPosition, int childPosition) 
  
   return true;
       


public class CustExpListview extends ExpandableListView


    int intGroupPosition, intChildPosition, intGroupid;

      public CustExpListview(Context context) 
      
       super(context);     
      

      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
      
       widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
       heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
       super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        


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(Home.this);
    tv.setText("child");
    tv.setPadding(15, 5, 5, 5);
    tv.setBackgroundColor(Color.YELLOW);
    tv.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    return tv;
  

  @Override
  public int getChildrenCount(int groupPosition) 
  
    return 5;
  

  @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(Home.this);
       tv.setText("-->Second Level");
       tv.setPadding(12, 7, 7, 7);
       tv.setBackgroundColor(Color.RED);       
       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;




【讨论】:

【参考方案3】:

在这种情况下,您应该创建自己的 Expandable 类。

参考此链接。 http://androidcodesnips.blogspot.in/2011/09/three-level-expandable-list.html

【讨论】:

在您的代码中,子菜单计数在所有父菜单中都相同【参考方案4】:

再创建一个扩展 BaseExpandableListAdapter 的可扩展列表适配器,并在您的 ExpandableListAdapter getChildView() 中调用新的可扩展列表适配器。

我已经使用以下链接实现了相同的功能: https://github.com/talhahasanzia/Three-Level-Expandable-Listview

【讨论】:

以上是关于如何在 android 的 ExpandableListView 中添加三级 ListView的主要内容,如果未能解决你的问题,请参考以下文章

如何在android原生中加载RN页面

Android File Transfer Mac: 如何在 macOS 和 Android 系统之间移动数据

如何在 Android 上将 TextView 居中?

如何在Ubuntu下配置Android开发环境

如何在Android中启动JAVA程序

Android:如何在 android studio 中配置 FFMPEG 最新版本?