【问题标题】:ExpandableListAdapter methods are not working properly in my case. Any alternate to ExpandableListViewExpandableListAdapter 方法在我的情况下无法正常工作。 ExpandableListView 的任何替代品
【发布时间】:2016-12-10 07:00:22
【问题描述】:

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

  and Child View layout

.

单击标题时,此子视图应显示在屏幕上。我是否为此设计使用了正确的 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);

【问题讨论】:

  • 你在哪里设置标题和子项?
  • header 必须是单个项目,子视图可能或多个不超过一个项目。您必须将标题 id 用于您的子项目键。就这些
  • 我有一个用于标题的布局和另一个用于子 @Ahamed 的布局,在这段代码中,我知道 _listDataHeader 和 _listDataChild 为空,但根据我的代码,我应该设置什么来代替它们?跨度>
  • 您必须清除在构造函数中传递的不需要的参数。将此网址用于可扩展列表视图。 androidhive.info/2013/07/android-expandable-list-view-tutorial
  • @Ahamed 我使用了来自同一个 url 的代码并根据我的喜好对其进行了转换.. 让我附上关于我想要实现的目标的图片

标签: java android expandablelistview


【解决方案1】:

正如@PratikDasa 所建议的,我使用了自定义 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 下设置了所有内容(在我的情况下为按钮)并将布局可见性设置为消失。 然后单击任何列表项(标题),我已经显示了我设置为消失的 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 一样工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 2020-10-12
    • 2020-02-25
    • 1970-01-01
    • 2021-11-24
    相关资源
    最近更新 更多