在这篇文章中,我们将为您详细介绍android–添加数据时刷新ExpandableListView:保持状态的内容,并且讨论关于安卓刷新listview的相关问题。此外,我们还会涉及一些关于027An
在这篇文章中,我们将为您详细介绍android – 添加数据时刷新ExpandableListView:保持状态的内容,并且讨论关于安卓刷新listview的相关问题。此外,我们还会涉及一些关于027 Android 可扩展的 listview:ExpandableListView 的使用案例、Android ExpandableListView、android ExpandableListView 图标的自定义、Android ExpandableListView 开发简介的知识,以帮助您更全面地了解这个主题。
本文目录一览:- android – 添加数据时刷新ExpandableListView:保持状态(安卓刷新listview)
- 027 Android 可扩展的 listview:ExpandableListView 的使用案例
- Android ExpandableListView
- android ExpandableListView 图标的自定义
- Android ExpandableListView 开发简介
android – 添加数据时刷新ExpandableListView:保持状态(安卓刷新listview)
可以在列表中的任何位置添加新条目.我们可以添加新组或仅添加新组.
现在我正在使用:
adapter.notifyDataSetChanged();
列表刷新,但状态不一样.例如,如果我有三个组:G1扩展,G2未扩展,G3扩展,我在G2和G3之间添加一个新组G4,然后G4获得G3状态,因为它占据了它的位置.有没有办法自动保持状态或我必须在我的适配器中手动执行?
谢谢!
[编辑]
添加一些代码.
这是我的适配器:
public class StopsInnerexpandablelistadapter extends Baseexpandablelistadapter { private ArrayList<String> groups; // Dates. private ArrayList<ArrayList<HashMap<String,String>>> children; // HashMap has stop ID,image and price. private Context context; public StopsInnerexpandablelistadapter (Context ctx,ArrayList<String> groups,ArrayList<ArrayList<HashMap<String,String>>> children) { context = ctx; this.groups = groups; this.children = children; } @Override public boolean areAllItemsEnabled() { return true; } public HashMap<String,String> getChild(int groupPosition,int childPosition) { return children.get(groupPosition).get(childPosition); } public long getChildId(int groupPosition,int childPosition) { return childPosition; } public View getChildView(int groupPosition,int childPosition,boolean isLastChild,View convertView,ViewGroup parent) { if (convertView == null) { // Inflate child''s view. LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.stop_row,null); } // Get the stop data and use it to fill the child''s views. HashMap<String,String> child = children.get(groupPosition).get(childPosition); ... return convertView; } public int getChildrenCount(int groupPosition) { return children.get(groupPosition).size(); } public String getGroup(int groupPosition) { return groups.get(groupPosition); } public int getGroupCount() { return groups.size(); } public long getGroupId(int groupPosition) { return groupPosition; } public View getGroupView(int groupPosition,boolean isExpanded,ViewGroup parent) { if (convertView == null) { // Inflate group''s view. LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.stop_subgroup,null); } String date = groups.get(groupPosition); TextView dateView = (TextView) convertView.findViewById(R.id.title_date); dateView.setText(date); return convertView; } public boolean hasstableIds() { return true; } public boolean isChildSelectable(int arg0,int arg1) { return true; } public void updateData(ArrayList<String> groups,String>>> children) { this.groups = groups; this.children = children; } }
在我的活动中,我这样做:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.stop_list); mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list); model = new MyModel(this); } @Override protected void onResume() { super.onResume(); ListEntries entries = model.getEntries(); if(adapter == null){ // Sets the adapter that provides data to the list. adapter = new StopsInnerexpandablelistadapter(this,entries.getDates(),entries.getChilds()); mExpandableList.setAdapter(adapter); } else{ adapter.updateData(entries.getDates(),entries.getChilds()); adapter.notifyDataSetChanged(); } }
解决方法
虽然你通过这里启用了稳定的ID:
public boolean hasstableIds() { return true; }
您仍然需要使用getGroupId()和getChildId()方法实际返回稳定的ID.分别返回groupPosition和childPosition不符合稳定ID的条件.
要成为稳定的ID意味着返回的值在整个数据集中是唯一的.此外,无论给定项目的位置如何,返回的值都是相同的.例如,假设您正在存储人员.每个人都有一个独特的SSN.这将是一个很好的稳定ID,可以通过getChildId()返回.例如,如果约翰史密斯位于第2,4位(组,子),然后稍后移动到位置(3,1)……他的稳定ID(或SSN)仍将是相同的.仅返回位置编号不能保证这一点.
027 Android 可扩展的 listview:ExpandableListView 的使用案例
1.ExpandableListView 简介
ExpandableListView 是一种用于垂直滚动展示两级列表的视图,和 ListView 的不同之处就是它可以展示两级列表,分组可以单独展开显示子选项。这些选项的数据是通过 ExpandableListAdapter 关联的。
2.xml 页面布局
(1) 主界面布局 (CommonNumberQueryActivity 对应布局)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".CommonNumberQueryActivity">
<TextView
style="@style/TitleStyle"
android:text="常用号码查询" />
<!--可以扩展的listview:ExpandableListView-->
<ExpandableListView
android:id="@+id/elv_common_number"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ExpandableListView>
</LinearLayout>
(2)elv_child_item_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_group_name"
android:text="分组名称"
android:layout_marginLeft="40dp"
android:textSize="16sp"
android:textColor="@color/red"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
(3)elv_child_item_child.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_name"
android:text="电话名称"
android:textSize="16sp"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_number"
android:text="电话号码"
android:textSize="16sp"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
3.java 后台代码
package com.example.administrator.test62360safeguard;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import com.example.administrator.test62360safeguard.engine.CommonNumberDao;
import java.util.List;
public class CommonNumberQueryActivity extends AppCompatActivity {
ExpandableListView elv_common_number;
List<CommonNumberDao.Group> groupList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_common_number_query);
initUI();
initData();
}
/**
* 给可扩展的listview:ExpandableListView准备数据,并填充
* 首先将对应的数据库文件放入assets目录下
*/
private void initData() {
CommonNumberDao commonNumberDao=new CommonNumberDao();
//获取数据库中的数据
groupList = commonNumberDao.getGroup();
System.out.println("groupList:"+groupList);
//给ExpandableListView设置数据适配器
elv_common_number.setAdapter(new MyAdapter());
}
private void initUI() {
elv_common_number = findViewById(R.id.elv_common_number);
}
private class MyAdapter extends BaseExpandableListAdapter {
@Override
public int getGroupCount() {
return groupList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return groupList.get(groupPosition).childList.size();
}
@Override
public CommonNumberDao.Group getGroup(int groupPosition) {
return groupList.get(groupPosition);
}
@Override
public CommonNumberDao.Child getChild(int groupPosition, int childPosition) {
return groupList.get(groupPosition).childList.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
/**
* 固定写法不需要修改
*/
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = View.inflate(getApplicationContext(), R.layout.elv_child_item_group, null);
TextView tv_group_name = view.findViewById(R.id.tv_group_name);
tv_group_name.setText(getGroup(groupPosition).name);
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view = View.inflate(getApplicationContext(), R.layout.elv_child_item_child, null);
TextView tv_name = view.findViewById(R.id.tv_name);
TextView tv_number = view.findViewById(R.id.tv_number);
tv_name.setText(getChild(groupPosition, childPosition).name);
tv_number.setText(getChild(groupPosition, childPosition).number);
return view;
}
/**
* @return 孩子节点是否响应事件
*/
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
}
4. 效果图
Android ExpandableListView
1、实现展开列ExpandableListView的三种方式之SimpleExpandableListAdapter实例:
http://blog.csdn.net/x605940745/article/details/12099709 SimpleExpandableListAdapter
2、基于自定义适配器的ExpandableListView:
http://www.eoeandroid.com/thread-81657-1-1.html
android ExpandableListView 图标的自定义
在 ExpandableListView 的 xml 文件内设置
android:groupIndicator 属性
自定义一个 xml seletor
<item android:state_expanded="true"
android:drawable="@drawable/icon_ex_down"/>
<item android:state_expanded="false"
android:drawable="@drawable/icon_ex_right"/>
如果 groupview 布局背景也想自定义
请使用 RelativeLayout,使用 LinearLayout 会遮住图标
Android ExpandableListView 开发简介
Android ExpandableListView 开发简介
我之前写了一些文章是关于实现带有分组、标签的 “ListView”:
(文章 1)《类似通讯录分组的 Android PinnedSectionListView,且分组标签悬停滑入滑出》文章链接:http://blog.csdn.net/zhangphil/article/details/47144125
(文章 2)《Android 基于 PinnedSectionListView 实现联系人通讯录》文章链接:http://blog.csdn.net/zhangphil/article/details/47271741
(文章 3)《Android ListView Adapter 的 getItemViewType 和 getViewTypeCount 多种布局》文章链接:http://blog.csdn.net/zhangphil/article/details/46984367
文章 1、2 所使用的技术,通常会在一些社交类的 APP 开发中涉及。社交类 APP 中一些重要的功能模块是通讯录、联系人、好友等等这些,一般不仅要把这些联系人、好友在一个 ListView 里面显示出来,同时还要分组,这种需求,最基础的实现技术手段如文章 3 中那样可以使用 Android ListView 本身适配器的 getItemViewType 和 getViewTypeCount 实现,也可以使用文章 1、2 中那样使用开源库实现以达到更好的用户体验。不过 Android 本身的 ExpandableListView 也可以实现这样的分组功能,且 ExpandableListView 自身提供众多的接口,方便二次开发和定制使用。
现给出一个完整的例子加以说明。
测试的主 activity MainActivity.java:
package zhangphil.expandablelistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
public class MainActivity extends Activity {
private final String GROUP = "group";
private final String CHILD = "child";
private ArrayList<HashMap<String, Object>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 分组的标签
String[] g = { "A", "B", "C", "D", "E", "F" };
data = new ArrayList<HashMap<String, Object>>();
// 子数据的计数
int COUNT = 0;
Random rand = new Random();
for (int i = 0; i < g.length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(GROUP, g[i]);
ArrayList<String> child = new ArrayList<String>();
int c = rand.nextInt(10);// 为每个子List随机生成c个测试数据。
for (int j = 0; j < c; j++) {
child.add("数据" + COUNT++);
}
map.put(CHILD, child);
data.add(map);
}
ExpandableListView elv = (ExpandableListView) findViewById(R.id.expandableListView);
elv.setGroupIndicator(null);
// 这是一个参数为空或者null的ExpandableListAdapter
// 构造在子类中完成
ExpandableListAdapter mExpandableListAdapter = new MyExpandableListAdapter(this, null, 0, null, null, null, 0,
null, null);
elv.setAdapter(mExpandableListAdapter);
// 演示
// 展开0组
elv.expandGroup(0);
// 收起1组
elv.collapseGroup(1);
// 展开2组
elv.expandGroup(2);
elv.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView arg0, View arg1, int arg2, long arg3) {
// Android默认是返回false。
// 如果返回true,那么,不管是点击已展开的分组还是未展开的分组,都不会相应展开或者收缩的,也就是说这个ExpandableListView将成为一个‘死’的ListView
return false;
}
});
}
private class MyExpandableListAdapter extends SimpleExpandableListAdapter {
private LayoutInflater inflater;
public MyExpandableListAdapter(Context context, List<? extends Map<String, ?>> groupData, int groupLayout,
String[] groupFrom, int[] groupTo, List<? extends List<? extends Map<String, ?>>> childData,
int childLayout, String[] childFrom, int[] childTo) {
super(context, groupData, groupLayout, groupFrom, groupTo, childData, childLayout, childFrom, childTo);
inflater = LayoutInflater.from(context);
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
View view = inflater.inflate(android.R.layout.simple_list_item_1, null);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setText(getChild(groupPosition, childPosition) + "");
return view;
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<String> items = (ArrayList<String>) data.get(groupPosition).get(CHILD);
return items.size();
}
@Override
public int getGroupCount() {
return data.size();
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = inflater.inflate(android.R.layout.simple_list_item_1, null);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setText(getGroup(groupPosition) + "");
view.setBackgroundColor(Color.RED);
return view;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<String> items = (ArrayList<String>) data.get(groupPosition).get(CHILD);
return items.get(childPosition);
}
@Override
public Object getGroup(int groupPosition) {
return data.get(groupPosition).get(GROUP);
}
}
}
MainActivity.java 需要的布局文件 activity_main.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"
tools:context="zhangphil.expandablelistview.MainActivity" >
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
activity_main.xml 其实为 MainActivity.java 布局一个 ExpandableListView。另外说一点,在实际的开发中如果场景简单,可以考虑使用和 Android ListActivity 像类似的 Android ExpandableListActivity,这样简单到连布局文件都省去写了。
运行结果如图:
今天的关于android – 添加数据时刷新ExpandableListView:保持状态和安卓刷新listview的分享已经结束,谢谢您的关注,如果想了解更多关于027 Android 可扩展的 listview:ExpandableListView 的使用案例、Android ExpandableListView、android ExpandableListView 图标的自定义、Android ExpandableListView 开发简介的相关知识,请在本站进行查询。
本文标签: