3

我正在尝试实现如下图所示的效果,我认为可扩展的列表视图可以帮助我,但它不起作用。展开 parent 时,没有任何显示。

我使用可扩展列表视图做错了吗?,有什么我应该去的吗? 图片

请帮忙..

编辑:适配器类代码:

`公共类 ExpandableListAdapter 扩展 android.widget.BaseExpandableListAdapter {

private Activity context;
private List<String> students;


public ExpandableListAdapter(Activity context, List<String> studentIds) {
     this.context = context;
     this.students = studentIds;
}

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

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

public View getChildView(final int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    final String laptop = (String) getChild(groupPosition, childPosition);
    LayoutInflater inflater = context.getLayoutInflater();

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.attendance_view, null);
    }

    CheckBox isPresent = (CheckBox) convertView.findViewById(R.id.is_present);
    EditText absentReason = (EditText) convertView.findViewById(R.id.absent_reason);

    return convertView;
}

public int getChildrenCount(int groupPosition) {
    return 1;
}

public Object getGroup(int groupPosition) {
    return laptops.get(groupPosition);
}

public int getGroupCount() {
    return students.size();
}

public long getGroupId(int groupPosition) {
    return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    String studentName= (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.group_item,
                null);
    }
    TextView item = (TextView) convertView.findViewById(R.id.studentIds);
    item.setTypeface(null, Typeface.BOLD);
    item.setText(studentName);
    return convertView;
}

public boolean hasStableIds() {
    return true;
}

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

}`

4

1 回答 1

2

首先我想告诉你,在 get childcount 方法中应该返回,你想要多少父项的子项......

就像如果你想要两个子项,那么获取子项应该返回 2。

我会给你确切的工作代码..

package com.example.n_level_list;
import java.util.ArrayList; 
import android.os.Bundle;
import android.animation.ArgbEvaluator;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class MainActivity extends Activity {
ExpandableListView mExpandableListView;
public static ArrayList<String> mArrayList,mArrayList2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mExpandableListView=(ExpandableListView)findViewById(R.id.expandableListView1); 
        mArrayList=new ArrayList<String>();
        mArrayList.add("first");
        mArrayList.add("second");
        mArrayList.add("third");
        mArrayList.add("fourth");
        mArrayList.add("fifth");
        Log.i("chk_size", "aa"+mArrayList.size());
          mArrayList2=new ArrayList<String>();

        ParentLevel mParentLevel=new ParentLevel(mArrayList);
        mExpandableListView.setAdapter(mParentLevel);
    }


    public class ParentLevel extends BaseExpandableListAdapter
    {
        ArrayList<String> mArrayList;
        public ParentLevel(ArrayList<String> mArrayList) 
        {
            this.mArrayList=mArrayList;
        }



        public ParentLevel() 
        {
            // TODO Auto-generated constructor stub
        }



        @Override
        public Object getChild(int groupPosition, int childPosition) 
        {
            // TODO Auto-generated method stub
            return childPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return childPosition;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            CustExpListview SecondLevelexplv = new CustExpListview(MainActivity.this);
            mArrayList2.add("child_first");
            mArrayList2.add("child_second");
            SecondLevelAdapter mSecondLevelAdapter=new SecondLevelAdapter(mArrayList2);
            SecondLevelexplv.setAdapter(mSecondLevelAdapter);
            SecondLevelexplv.setGroupIndicator(null);
            return SecondLevelexplv;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            // TODO Auto-generated method stub
            return mArrayList2.size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            // TODO Auto-generated method stub
            return groupPosition;
        }

        @Override
        public int getGroupCount() {
            // TODO Auto-generated method stub
            return mArrayList.size();
        }

        @Override
        public long getGroupId(int groupPosition) {
            // TODO Auto-generated method stub
            return groupPosition;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) 
        {
            LayoutInflater mInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=mInflater.inflate(R.layout.inflate_activity, null);
            TextView mTextView=(TextView)convertView.findViewById(R.id.textView1); 
            mTextView.setText(mArrayList.get(groupPosition));
            return convertView;
        }

        @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;
        }


    }





    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
    {
        ArrayList<String> mArrayList2;
        public SecondLevelAdapter(ArrayList<String> mArrayList2) 
        {
             this.mArrayList2=mArrayList2;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return childPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) 
        {
            // TODO Auto-generated method stub
            return childPosition;
        }

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

            return null;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Object getGroup(int groupPosition) {
            // TODO Auto-generated method stub
            return groupPosition;
        }

        @Override
        public int getGroupCount() {
            // TODO Auto-generated method stub
            return mArrayList2.size();
        }

        @Override
        public long getGroupId(int groupPosition) {
            // TODO Auto-generated method stub
            return groupPosition;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            LayoutInflater mInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=mInflater.inflate(R.layout.inflate_activity, null);
            TextView mTextView=(TextView)convertView.findViewById(R.id.textView1); 
            mTextView.setText(mArrayList2.get(groupPosition));
            return convertView;
        }

        @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 false;
        }


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

}
于 2014-01-23T13:03:34.557 回答