我正在尝试实现可扩展的列表视图,但是当我单击父列表项时,我想显示另一个xml作为子列表项,它有两个具有一些功能的按钮。它应该显示child.xml的内容是扩展列表视图。是否有可能做到这一点?我已经浏览了与可扩展列表视图相关的各种帖子,但我还没有得到解决方案。 我实施的守则如下:
MainActivity.class:
public class MainActivity extends Activity {
private ExpandableListView mEventList;
private String[] eventMenuTitles = {Item1, Item2, Item3, Item4, Item5};
Integer[] imgid={R.drawable.logo,R.drawable.logo,R.drawable.logo,R.drawable.logo,R.drawable.logo};
private String[][] children;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load slide menu items
eventMenuTitles = getResources().getStringArray(R.array.event_drawer_items);
// eventMenuIcons = getResources().getInteger(R.array.event_drawer_icons);
children = new String [][] {
{ "Content1" },
{ "Content2" },
{ "Content3" },
{ "Content4" },
{"Content5"}
};
mEventList=(ExpandableListView)findViewById(R.id.expandableListView1);
mEventList.setAdapter(new ExpandableListAdapter(eventMenuTitles, children));
mEventList.setGroupIndicator(null);
}
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private final LayoutInflater inf;
private String[] groups;
private String[][] children;
public ExpandableListAdapter(String[] groups, String[][] children) {
this.groups = groups;
this.children = children;
inf = LayoutInflater.from(getApplicationContext());
}
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inf.inflate(R.layout.activity_contents, parent, false);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.textView1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(getChild(groupPosition, childPosition).toString());
return convertView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inf.inflate(R.layout.activity_list, parent, false);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(getGroup(groupPosition).toString());
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private class ViewHolder {
TextView text;
}
}
}
activity_contents.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Large Text"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3"
android:layout_marginTop="25dp"
android:layout_toLeftOf="@+id/textView3"
android:src="@drawable/logo" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageButton1"
android:layout_toLeftOf="@+id/textView1"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignParentRight="true"
android:onClick="add"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_toLeftOf="@+id/button1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</RelativeLayout>
Activity_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="1" >
<ImageView
android:id="@+id/icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0.2"
android:layout_gravity="center"
android:src="@drawable/logo" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:layout_weight="0.8"
android:gravity="center|left" />
</LinearLayout>
如图所示,activity_contents.xml有一个imageButton和一个按钮,每个onClick我想要执行一些功能。但是,我能得到的最大值是点击父项时显示的xml。如何在activity_contents.xml中单击按钮添加一些功能。
答案 0 :(得分:0)
我终于找到了上述问题的解决方案。它如下:
MainActivity.class:
public class MainActivity extends Activity {
private ExpandableListView mEventList;
private String[] eventMenuTitles = {Item1, Item2, Item3, Item4, Item5};
Integer[] imgid={R.drawable.logo,R.drawable.logo,R.drawable.logo,R.drawable.logo,R.drawable.logo};
private String[][] children;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load slide menu items
eventMenuTitles = getResources().getStringArray(R.array.event_drawer_items);
// eventMenuIcons = getResources().getInteger(R.array.event_drawer_icons);
children = new String [][] {
{ "Content1" },
{ "Content2" },
{ "Content3" },
{ "Content4" },
{"Content5"}
};
mEventList=(ExpandableListView)findViewById(R.id.expandableListView1);
mEventList.setAdapter(new ExpandableListAdapter(eventMenuTitles, children));
mEventList.setGroupIndicator(null);
}
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private final LayoutInflater inf;
private String[] groups;
private String[][] children;
public ExpandableListAdapter(String[] groups, String[][] children) {
this.groups = groups;
this.children = children;
inf = LayoutInflater.from(getApplicationContext());
}
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inf.inflate(R.layout.activity_contents, parent, false);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.textView1);
holder.btn = (Button)convertView.findViewById(R.id.button1);
holder.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Your Code
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(getChild(groupPosition, childPosition).toString());
return convertView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inf.inflate(R.layout.activity_list, parent, false);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(getGroup(groupPosition).toString());
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private class ViewHolder {
TextView text;
Button btn;
}
}
}
其余的都是一样的。 :)