我在网上找了很久也没看到类似百度贴吧评论以及再次评论的实例,但因为工作需要不得不写,经过研究自己写了一个例子供大家参考
自定义ExpandableListPageView实现分页效果
public class ExpandableListPageView extends ExpandableListView implements
OnScrollListener {
// private static String Tag = "ListPageView";
public Boolean canLoad = false; // 是否能够加载数据
public int currentPageIndex = 0; // 记录页索引
public int pageSize = 0; // 每页显示项目数
public String loadMessage = "正在加载...."; // 进度条提示消息
public LinearLayout footerLayout;// 页脚
public OnPageLoadListener listener;// 分布侦听事件
public Context context;
public ExpandableListPageView(Context context) {
super(context);
}
public ExpandableListPageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 设置第页显示项目数
*
* @param pageSize
* 第页显示项目数
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* 进度条显示的消息文本
*
* @param msg
*/
public void setLoadMessage(String msg) {
this.loadMessage = msg;
}
/*
* 设置数据源
*
* @see android.widget.ListView#setAdapter(android.widget.ListAdapter)
*/
@Override
public void setAdapter(ExpandableListAdapter adapter) {
this.setOnScrollListener(this);
// 必须在setAdapter()方法之前构建进度条并添加到页脚
this.BuildProgressBar();
super.setAdapter(adapter);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (this.canLoad && (scrollState == OnScrollListener.SCROLL_STATE_IDLE)) {
// 加载数据
if (listener != null) {
this.currentPageIndex++;
listener.onPageChanging(this.pageSize, currentPageIndex);
// System.out.println("----####------this.pageSize-------"+this.pageSize);
// System.out.println("----####------this.currentPageIndex-------"+currentPageIndex);
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// Log.i("####", "listener = " + listener + " firstVisibleItem = " +
// firstVisibleItem + " visibleItemCount = "
// + visibleItemCount + " totalItemCount = " + totalItemCount);
this.canLoad = false;
if (this.listener == null) {
return;
}
if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
this.canLoad = this.listener.canLoadData();
}
// Log.i("####", "判断canLoad = " + canLoad);
}
/**
* 是否显示进度条
*
* @param isVisible
* true:显示,false:不显示
*/
public void setProggressBarVisible(Boolean isVisible) {
if (this.footerLayout == null) {
return;
}
int visibility = View.VISIBLE;
if (!isVisible) {
visibility = View.GONE;
}
// 定位到最后一行,必须设置,要不然进度条看不到
this.setSelection(this.getAdapter().getCount());
this.footerLayout.setVisibility(visibility);
// 设置页脚中组件的显示状态
for (int i = 0; i < this.footerLayout.getChildCount(); i++) {
View v = this.footerLayout.getChildAt(i);
v.setVisibility(visibility);
}
}
/**
* 创建页脚显示进度条,必须在setAdapter()方法之前调用.
*
* @return
*/
private void BuildProgressBar() {
if (this.getFooterViewsCount() != 0) {
return;
}
footerLayout = new LinearLayout(this.getContext());
footerLayout.setGravity(Gravity.CENTER);
footerLayout.setPadding(0, 0, 0, 0);
footerLayout.setOrientation(LinearLayout.HORIZONTAL);
ProgressBar bar = new ProgressBar(this.getContext());
footerLayout.addView(bar);
footerLayout.setBackgroundResource(R.color.white);
TextView txt = new TextView(this.getContext());
txt.setText(this.loadMessage);
footerLayout.addView(txt);
footerLayout.setVisibility(View.GONE);
this.addFooterView(footerLayout);
}
/**
* 设置页加载侦听事件
*
* @param listener
*/
public void setOnPageLoadListener(OnPageLoadListener listener) {
this.listener = listener;
}
public interface OnPageLoadListener {
/**
* 触发分页事件
*
* @param pageSize
* @param pageIndex
*/
public void onPageChanging(int pageSize, int pageIndex);
/**
* 是否能够加载数据 此方法返回结果为true时触发OnPageChanging事件,否则不做任何处理
*
* @return
*/
public boolean canLoadData();
}
}
/***
* 实例类
*
*/
public class PostActivity extends Activity implements
OnPageLoadListener, OnClickListener {
PostAdapter adapter;
private ExpandableListPageView listView;
int page = 1;
private int pageSize = 5;// 分页每页显示数据
long messageSize = 0;
int lastItem = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_liaoyiliaopost);
into();
}
public void into() {
listView = (ExpandableListPageView) this
.findViewById(R.id.post_list_view);
// 构建空的适配器
registerForContextMenu(listView);
this.adapter = new PostAdapter(this);
listView.setOnPageLoadListener(this);
// 设置进度条提示消息
listView.setLoadMessage("load");
listView.setAdapter(adapter);
// 设置每次加载显示数量
listView.setPageSize(pageSize);
// 设置监听事件
// 默认加载第1页数据
LoadBindData load = new LoadBindData(5, 0);
load.execute();
}
@Override
public void onPageChanging(final int pageSize, final int pageIndex) {
// 加载数据
LoadBindData load = new LoadBindData(pageSize, pageIndex);
load.execute();
}
@Override
public boolean canLoadData() {
// return (this.adapter.getCount() >= 5) ? false : true;
return (adapter.getGroupCount() >= messageSize) ? false : true;
}
/**
* @author RUJC 异步读取数据
*/
public class LoadBindData extends AsyncTask<String, String, String> {
private List<PostGroup> list;
private int pageSize;
private int pageIndex;
/**
* 构造函数
*
* @param pageSize
* 每次加载的项目数
* @param pageIndex
* 页索引
*/
public LoadBindData(int pageSize, int pageIndex) {
this.pageSize = pageSize;
this.pageIndex = pageIndex;
if (pageIndex == 0) {
page = 1;
} else {
page++;
}
}
@Override
protected void onPreExecute() {
// 在doInBackground执行之前触发.
// 显示进度条
listView.setProggressBarVisible(true);
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
// doInBackground执行完后触发,result是doInBackground执行后的返回值.
// 更新数据并关闭进度条显示
if (list != null) {
adapter.add(list);
}
// 更新界面数据显示
adapter.notifyDataSetChanged();
// 关闭进度条
listView.setProggressBarVisible(false);
super.onPostExecute(result);
}
@Override
protected String doInBackground(String... params) {
// 读取数据
list = getData(pageSize, pageIndex);
return null;
}
}
/**
* 数据源
*
* @param pageSize
* 每次加载显示的项目数
* @param pageIndex
* 页索引
* @return
*/
private List<PostGroup> getData(int pageSize, int pageIndex) {
List<PostInfo> childList = null;
List<PostGroup> groupList = null;
PostInfo childInfo;
PostGroup groupInfo;
try {
groupList = new ArrayList<PostGroup>();
for (int i = 0; i < nArray.length(); i++) {//nArray是获取网络数据的一个List集合
JSONObject myObj = nArray.getJSONObject(i);
String userAlias = "userAlias "
String username = "username";
childList = new ArrayList<Info>();
groupInfo = new PostInfo ();
groupInfo.setUserAlias(userAlias);
groupInfo.setUname(username);
JSONArray childs = myObj.getJSONArray("childs");
for (int j = 0; j < childs.length(); j++) {
childInfo = new PostInfo();
String userAliass = childsObj.getString("userAlias");
String usernames = childsObj.getString("username");
childInfo.setUserAlias(userAliass);
childList.add(childInfo);
childInfo = null;
}
groupInfo.setPostInfo(childList);
groupList.add(groupInfo);
groupInfo = null;
}
} catch (Exception e) {
// TODO: handle exception
}
return groupList;
}
}
适配器
public class PostAdapter extends BaseExpandableListAdapter {
private LayoutInflater mChildInflater;
Context context;
private List<PostGroup> groupList = new ArrayList<PostGroup>();
public LiaoYiLiaoPostAdapter(Context context) {
mChildInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void add(List<PostGroup> messages) {
groupList.addAll(messages);
}
@Override
public int getGroupCount() {
return groupList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
int s = 0;
if (null != groupList.get(groupPosition).getPostInfo()) {
s = groupList.get(groupPosition).getPostInfo().size();
} else {
s = 0;
}
return s;
}
@Override
public LiaoYiLiaoPostGroup getGroup(int groupPosition) {
return groupList.get(groupPosition);
}
@Override
public PostInfo getChild(int groupPosition, int childPosition) {
return groupList.get(groupPosition).getPostInfo()
.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
/**
* 设置父组样式
*
* **/
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
FriendHolder holder;
if (convertView == null) {
holder = new FriendHolder();
convertView = mChildInflater
.inflate(R.layout.list_post_group, null);
holder.userheadimg = (ImageView) convertView
.findViewById(R.id.userheadimg);
holder.but_post = (ImageView) convertView
.findViewById(R.id.but_post);
holder.username = (TextView) convertView
.findViewById(R.id.username);
holder.time = (TextView) convertView.findViewById(R.id.time);
holder.floor = (TextView) convertView.findViewById(R.id.floor);
holder.info = (TextView) convertView.findViewById(R.id.info);
convertView.setTag(holder);
} else {
holder = (FriendHolder) convertView.getTag();
}
String headimg = "http://www.homefoot.com"
+ groupList.get(groupPosition).getUsrImg();// 头像
String name = groupList.get(groupPosition).getUserAlias();// 昵称
String times = groupList.get(groupPosition).getDate();
CharSequence content = groupList.get(groupPosition).getInfoSequence();
holder.info.setText(content);
holder.time.setText(times);
holder.username.setText(name);
int floor = groupPosition + 1;
String flor = " " + floor + "楼";
holder.floor.setText(flor);
ImageLoader iLoader = new ImageLoader(context);
iLoader.DisplayImage(headimg, holder.userheadimg, false);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
FriendHolder holder;
if (convertView == null) {
holder = new FriendHolder();
convertView = mChildInflater
.inflate(R.layout.list_post_child, null);
holder.uname = (TextView) convertView.findViewById(R.id.uname);
convertView.setTag(holder);
} else {
holder = (FriendHolder) convertView.getTag();
}
LiaoYiLiaoPostInfo groupname = groupList.get(groupPosition)
.getPostInfo().get(childPosition);
holder.uname.setText(groupname.getUserAlias());// 好友名称
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
class FriendHolder {
// 父类
ImageView userheadimg;// 头像
ImageView but_post;// 回复按钮
TextView username;// 用户名
// 子类
TextView uname;// 用户名
TextView coinfo;// 内容
}
}
创建一个main.xml文件
< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contact_list_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gray_back"
android:orientation="vertical" >
<com.demo.util.ExpandableListPageView<!--com.demo.util 所在的包名 -->
android:id="@+id/liao_post_list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:cacheColorHint="#00000000"
android:childDivider="@drawable/interval_line"
android:divider="@drawable/interval_line"
android:dividerHeight="1dp"
android:fadingEdge="none"
android:groupIndicator="@null"
android:listSelector="@drawable/list_item_bg" />
</LinearLayout>
我只提供了一个思路,如果要运行需要改代码 ,不清楚的地方可以相互交流下