**我想在我的android代码片段中添加一个可扩展的列表视图,但我无法做到。我也在这个网站上看到了所有与这个主题相关的帖子,但我找不到一个好的答案。有人能帮忙吗??**
HMCfragment.java
public class HMCfragment extends Fragment {
View rootView;
ExpandableListView lv;
private String[] groups;
private String[][] children;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
groups = new String[] { "Test Header 1", "Test Header 2", "Test Header 3", "Test Header 4" };
children = new String [][] {{"Debashish Dutta"},{"Deepok Baruah"},{"Rajan Kumar"},{"Aditya Patel"},{"Piyush dwivedi"}};
rootView = inflater.inflate(R.layout.hmc_layout, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
lv = (ExpandableListView) view.findViewById(R.id.expandableListView1);
lv.setAdapter(new ExpandableListAdapter(groups, children));
lv.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(getActivity());
}
@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.child_layout, 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.parent_layout, parent, false);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.parent_txt);
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;
}
}}
这是我创建的项目;
MyActivity.java
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
View rootView;
ExpandableListView lv;
private String[] groups;
private String[][] children;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
groups = new String[]{"Test Header 1", "Test Header 2", "Test Header 3", "Test Header 4"};
children = new String[][]{{"Debashish Dutta"}, {"Deepok Baruah"}, {"Rajan Kumar"}, {"Aditya Patel"}, {"Piyush dwivedi"}};
rootView = inflater.inflate(R.layout.fragment_my, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
lv = (ExpandableListView) view.findViewById(R.id.exp);
lv.setAdapter(new ExpandableListAdapter(groups, children));
lv.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(getActivity());
}
@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.child_layout, parent, false);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.textView);
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.parent_layout, parent, false);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.textView);
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;
}
}
}
}
fragment_my.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity$PlaceholderFragment">
<ExpandableListView
android:id="@+id/exp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
child_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
parent_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />
</LinearLayout>