如何通过单击android中的父节点在expandablelistview中动态获取子元素

时间:2014-06-23 13:58:20

标签: java android expandablelistview

我对android很新,所以任何人都可以非常轻松地解释如何通过单击父级来使childList动态化吗?

我的问题:我在expandablelistview中有一些静态父级。我的意图是,当我选择一个父母看到它的孩子时,孩子将动态地形成数据库并且它们将是可导航的(我想看到更多细节或者去另一个页面。)

我分三步完成了静态孩子。

  1. 使用expandablelistview创建XML文件。
  2. 在主java调用适配器
  3. 使用expandablelistview绑定适配器。创建自定义适配器。
  4. 适配器如下:

    package com.example.eschool_only_accrdion_straggle;
    
    import android.R.color;
    import android.content.Context;
    import android.graphics.Color;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.TextView;
    
    public class My_custom_adapter extends BaseExpandableListAdapter {
    
        private Context context;
    
        String [] parentList={"Summer Fruit", "Winter Fruit", "Hard fruit", "Watery Fruit"};
        // String [][] childList={{"Lychy", "Mango"},{"Banana"},{"Pomy Granade"},{"pani Fruit"}};
    
    
        public My_custom_adapter(Context context) {
            this.context = context;
        }
    
        // In Here 1st Parameter is for PARENT and 2nd one is for CHILD
    
        @Override
        public Object getChild(int groupPosition, int childPosition) { 
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return 0;
        }
    
        @Override
        public View getChildView(int groupPosition, int childPosition, 
                boolean arg2, View arg3, ViewGroup arg4) {
    
            TextView TxtV_child=new TextView(context);
            TxtV_child.setText(childList[groupPosition][childPosition]);
            TxtV_child.setPadding(35, 0, 0, 0);
            TxtV_child.setTextColor(Color.CYAN);
            TxtV_child.setTextSize(17);
            return TxtV_child;
        }
    
        @Override
        public int getChildrenCount(int groupPosition) {
    
            return childList[groupPosition].length;
        }
    
        @Override
        public Object getGroup(int groupPosition) {
    
            return groupPosition;
        }
    
        @Override
        public int getGroupCount() {
            // TODO Auto-generated method stub
            return parentList.length;
        }
    
        @Override
        public long getGroupId(int groupPosition) {
            // TODO Auto-generated method stub
            return groupPosition;
        }
    
        @Override
        public View getGroupView(int groupPosition, boolean childPosition, View arg2, ViewGroup arg3) {
            TextView TxtV_Parent=new TextView(context);
            TxtV_Parent.setText(parentList[groupPosition]);
            TxtV_Parent.setPadding(5, 5, 0, 5);
            TxtV_Parent.setTextColor(Color.MAGENTA);
            TxtV_Parent.setTextSize(25);
            return TxtV_Parent;
        }
    
        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return false;
        }
    
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return true;
        }
    
    }                              
    

1 个答案:

答案 0 :(得分:0)

创建一个扩展“ExpandableListActivity”的活动,例如这个tutorial

初始化您的数据

private ArrayList<String> groups = new ArrayList<String>() ;
private ArrayList<ArrayList<String>> children; 
private NewAdapter mNewAdapter = null;

public void onCreate(Bundle savedInstanceState) {

    // ....
    groups.add("Summer Fruit");
    groups.add("Winter Fruit");
    groups.add("Hard fruit");
    groups.add("Watery Fruit");

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

    ArrayList<String> a = new ArrayList<String>();
    a.add("Lychy");
    a.add("Mango");
    children.add(a);

    a = new ArrayList<String>();
    a.add("Banana");
    children.add(a);

    a = new ArrayList<String>();
    a.add("Pomy Granade");
    children.add(a);


    a = new ArrayList<String>();
    a.add("pani Fruit");
    children.add(a);

    mNewAdapter = new NewAdapter(groups, children);
    mNewAdapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),this);
    getExpandableListView().setAdapter(mNewAdapter);
    int cpt = mNewAdapter.getGroupCount();
    for(int i = 0; i < cpt; i++ ){
        getExpandableListView().expandGroup(i);
    }
}

然后务必将以下方法覆盖到您的活动中

@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    // Do here what you need to do when a child is selected
    return true;
}

添加最后添加适配器

class NewAdapter extends BaseExpandableListAdapter{

    public LayoutInflater minflater;
    public Activity activity;
    public ArrayList<String> groups;
    public ArrayList<ArrayList<String>> points;

    public NewAdapter(ArrayList<String> groups, ArrayList<ArrayList<String>> points){
        this.groups = groups;
        this.points = points;
    }

    public void setInflater(LayoutInflater mInflater, Activity act) {
        this.minflater = mInflater;
        activity = act;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return ((ArrayList<String>) points.get(groupPosition)).size();
    }

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

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public View getGroupView(int groupPosition, boolean childPosition, View arg2, ViewGroup arg3) {
        // ...
        return v;
    }

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

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

    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View v, ViewGroup parent) {
        // ....
        return v;
    }
}

在适配器中,确保isChildSelectable(int groupPosition,int childPosition)返回“true”。