我想制作一个简单的应用程序,其中我有一个这种格式的数据库。
分类 第1项 第2项 项目n
第2类 item1项目n
我想在列表视图中加载所有这些数据,并将类别作为分隔符,并在其上添加按钮 以及它们下面的所有项目就像一个典型的带有字母分隔符的联系人应用程序(但每个分隔符上都有按钮)。需求:
答案 0 :(得分:4)
这些东西可以帮助你,我在我的应用程序中做了类似的事情。
首先创建一些xml来保存您的数据 这是我的activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
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"
tools:context=".MainActivity" >
<ExpandableListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/listbg"
>
</ExpandableListView>
Xml 2; listrow_details
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="40dp"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:text="@string/hello_world"
android:textSize="14sp"
android:textStyle="bold" >
</TextView>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
Xml 3:listview_group
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_marginLeft="8dp"
android:gravity="left"
android:paddingLeft="32dp"
android:paddingTop="8dp"
android:text="Test"
android:textSize="14sp"
android:textAlignment="textEnd"
android:textStyle="bold" />
创建一个组类:
public class Group {
public String string;
public final List<String> children = new ArrayList<String>();
public Group(String string) {
this.string = string;
}
}
可扩展列表视图的适配器类:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private final SparseArray<Group> groups;
public LayoutInflater inflater;
public Activity activity;
public MyExpandableListAdapter(Activity act, SparseArray<Group> groups) {
activity = act;
this.groups = groups;
inflater = act.getLayoutInflater();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).children.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String children = (String) getChild(groupPosition, childPosition);
TextView text = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_details, null);
}
text = (TextView) convertView.findViewById(R.id.textView1);
text.setText(children);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(activity, children,
Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return groups.get(groupPosition).children.size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ExpandableListView eLV = (ExpandableListView) parent;
eLV.expandGroup(groupPosition);
if (convertView == null) {
convertView = inflater.inflate(R.layout.listview_group, null);
}
Group group = (Group) getGroup(groupPosition);
((TextView) convertView).setText(group.string);
// ((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
最后在您的主要活动中执行此操作
public class MainActivity extends Activity {
// More efficient than HashMap for mapping integers to objects
SparseArray<Group> groups = new SparseArray<Group>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createData();
ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
listView.setGroupIndicator(getResources().getDrawable(R.drawable.listbg));
MyExpandableListAdapter adapter = new MyExpandableListAdapter(this,
groups);
listView.setAdapter(adapter);
}
public void createData() {
for (int j = 0; j < 5; j++) {
Group group = new Group("Test " + j);
for (int i = 0; i < 5; i++) {
group.children.add("Sub Item" + i);
}
groups.append(j, group);
}
}
}
上面的代码将解决您的问题,我使用了一个可绘制的xml来自定义我的expeablelistview的外观
这里是listbg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_empty="true" android:drawable="@android:color/transparent"/>
<item android:state_expanded="true" android:drawable="@android:color/transparent" />
<item android:drawable="@android:color/transparent" />
</selector>
您可以根据需要使用按钮或任何内容进行自定义。 这个代码将解决你的问题,我想,PEACE:)
<强>截图强>
答案 1 :(得分:0)
好的所以我发现了一些有用的here,这就是我要找的东西 所以答案是: