仿QQ、微信翻页查看聊天记录
主界面MainActivity.class
public class MainActivity extends Activity implements OnScrollListener{ private ProgressBar loadInfo; private ListView listView; private LinearLayout loadLayout; private ArrayList<string> items; private DatabaseService service; private int currentPage = 1; //默认在第一页 private static final int lineSize =10; //每次显示数 private int allRecorders = 0; //全部记录数 private int pageSize = 1; //默认共一页 private Aleph0 baseAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listview); service = new DatabaseService(this); //创建一个角标线性布局用来显示正在加载 loadLayout = new LinearLayout(this); loadLayout.setGravity(Gravity.CENTER); //定义一个ProgressBar表示“正在加载” loadInfo = new ProgressBar(this,null,android.R.attr.progressBarStyleSmall); //增加组件 loadLayout.addView(loadInfo, new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); //增加到listView头部 listView.addHeaderView(loadLayout); listView.setOnScrollListener(this); showAllData(); } /** * 读取全部数据 */ public void showAllData(){ allRecorders = service.getCount(); //计算总页数 pageSize = (allRecorders + lineSize -1) / lineSize; items = service.getAllItems(currentPage, lineSize); Collections.reverse(items); //倒序 baseAdapter = new Aleph0(); listView.setAdapter(baseAdapter); listView.setSelection(items.size());//直接定位到最底部 } int firstItem=-1; @Override public void onScroll(AbsListView absView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { firstItem=firstVisibleItem; } @Override public void onScrollStateChanged(AbsListView view, int scorllState) { if (firstItem==0&& currentPage < pageSize&& scorllState == OnScrollListener.SCROLL_STATE_IDLE) {// 不再滚动 Log.e(log, 滑到第一位); currentPage++; // 增加数据 appendDate(); } } /** * 增加数据 */ private void appendDate(){ final ArrayList<string> additems = service.getAllItems(currentPage, lineSize); Collections.reverse(additems); baseAdapter.setCount(baseAdapter.getCount() + additems.size()); //判断,如果到了最末尾则去掉“正在加载” if(allRecorders == baseAdapter.getCount()){ listView.removeHeaderView(loadLayout); } items.addAll(0, additems); baseAdapter.notifyDataSetChanged(); listView.setSelection(additems.size()); } class Aleph0 extends BaseAdapter { int count = lineSize; public int getCount() { return count; } public void setCount(int count){ this.count = count; } public Object getItem(int pos) { return pos; } public long getItemId(int pos) { return pos; } public View getView(int pos, View v, ViewGroup p) { TextView view = new TextView(MainActivity.this); view.setTextSize(60); if(items != null){ view.setText(items.get(pos)); }else{ view.setText(pos); } return view; } } }
/** * 刷新视图 * */ public void setList(List<ChatMode.RecentLetter> bcData) { Collections.reverse(bcData); this.listMsg = bcData; notifyDataSetInvalidated(); lv.setSelection(bcData.size()); } /** * 加载更多 * */ public void addList(final List<ChatMode.RecentLetter> newMsgData) { if (this.listMsg != null) { Collections.reverse(newMsgData); this.listMsg.addAll(0, newMsgData); notifyDataSetChanged(); lv.setSelection(newMsgData.size() + 1); } else { setList(newMsgData); } }
2,主界面布局文件activity_main.xml
<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <listview android:id="@+id/listview" android:layout_height="fill_parent" android:layout_width="fill_parent"> </listview></linearlayout>
4,效果图
版权声明:本文为niray原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。