在我的案例中,ExpandableListAdapter 方法无法正常工作。 ExpandableListView 的任何替代品

ExpandableListAdapter methods are not working properly in my case. Any alternate to ExpandableListView

我是第一次使用 ExpandableListAdapter。我在 groupView 中有一些文本视图,在 childView 中有一些按钮。我使用了来自互联网的以下代码,并尝试根据我的喜好对其进行转换。但是getGroupCount()方法有问题。我知道在我的代码中 _listDataHeader_listDataChild 是空的,但我应该使用什么来代替?这是我 header 视图

的照片

  and Child View layout

.

单击 header 时,此 child 视图应显示在屏幕上。我是否为此设计使用了正确的 android 视图?请帮助

 public class ExpandableListAdapter extends BaseExpandableListAdapter {

Activity context;
ArrayList<String> s_date,c_number,d_ration,s_time,download_path,a_number,a_name;
String id[];
String mUrl;

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

public ExpandableListAdapter(Activity context, ArrayList<String> start_date, ArrayList<String> caller_number, ArrayList<String> duration,ArrayList<String> start_time, ArrayList<String> download_path, ArrayList<String> agent_number, ArrayList<String> agent_name) {
    this.context=context;
    this.s_date=start_date;
    this.c_number=caller_number;
    this.d_ration=duration;
    this.s_time=start_time;
    this.download_path=download_path;
    this.a_number=agent_number;
    this.a_name=agent_name;
}

@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(final 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);
    }

    Button play_button=(Button) convertView.findViewById(R.id.play);
    play_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Log.d("play child is",childText);

        }
    });

    Button download_button=(Button) convertView.findViewById(R.id.download);
    download_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Log.d("download child is",childText);

        }
    });

}

@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.s_date.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 start_date = (TextView) convertView.findViewById(R.id.start_date);
    start_date.setText(s_date.get(groupPosition));
    TextView caller_number = (TextView) convertView.findViewById(R.id.caller_number);
    caller_number.setText(c_number.get(groupPosition));
    TextView duration = (TextView) convertView.findViewById(R.id.duration);
    duration.setText(d_ration.get(groupPosition));
    TextView start_time = (TextView) convertView.findViewById(R.id.start_time);
    start_time.setText(s_time.get(groupPosition));
    TextView agent_name = (TextView) convertView.findViewById(R.id.agent_name);
    agent_name.setText(a_name.get(groupPosition));
    TextView agent_number = (TextView) convertView.findViewById(R.id.agent_number);
    agent_number.setText(a_number.get(groupPosition));
   // start_date.setTypeface(null, Typeface.BOLD);
   // start_date.setText(headerTitle);

    return convertView;
}


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

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

这就是我设置适配器的方式

     ExpandableListAdapter a=new ExpandableListAdapter(MainScreen.this,start_date,caller_number,duration,start_time,download_path,agent_number,agent_name);
    data_list.setAdapter(a);

根据@PratikDasa 的建议,我使用了 Custom ListView,并且我使用了 Visible gone 方法。下面是解决方法。

    <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/sub_layout"
    android:background="#FFC7FFFE"
    android:visibility="gone">

我已经在此 sub_layout 下设置了所有内容(在我的例子中是按钮)并将布局可见性设置为消失。 然后单击任何列表项 (Headers),我显示了我已设置为消失的 sub_layout。

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View row = inflater.inflate(R.layout.list_item, g, false);

    row.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           // Toast.makeText(context,"i'm Clicked",Toast.LENGTH_LONG).show();

            RelativeLayout sub_list=(RelativeLayout)row.findViewById(R.id.sub_layout);
            sub_list.setVisibility(View.VISIBLE);
        }
    });

现在它像 ExpandableListView 一样工作。