我第一次使用ExpandableListAdapter
。我在groupView中有一些textview,在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;
}
}
这就是我设置adpater的方式
ExpandableListAdapter a=new ExpandableListAdapter(MainScreen.this,start_date,caller_number,duration,start_time,download_path,agent_number,agent_name);
data_list.setAdapter(a);
答案 0 :(得分:0)
由@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下设置了所有内容(在我的情况下为按钮),并将布局visibilty设置为已消失。 然后点击任何列表项(标题),我已经显示了我设置为已经消失的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。