Android开发中实现分页效果的简单步骤
来源:爱站网时间:2020-10-27编辑:网友分享
分页加载在程序开发中是必备的,但是我们实现这个功能并不仅仅为了美观,用户体验也是很重要的,爱站技术频道下面就带大家了解Android开发中实现分页效果的简单步骤,感兴趣的小伙伴们参考看看吧!
分页加载在程序开发中是必备的,但是我们实现这个功能并不仅仅为了美观,用户体验也是很重要的,爱站技术频道下面就带大家了解Android开发中实现分页效果的简单步骤,感兴趣的小伙伴们参考看看吧!
具体内容如下
1.实现分页最主要的就是封装分页代码,然后在按钮里实现相关的操作
/** * 分页工具 * * @Project App_Page * @Package com.android.dividepage * @author chenlin * @version 1.0 * @Date 2012年6月2日 * @Note TODO * @param*/ public class PageHelper { private List allData; // 所有数据 private int perPage = 10; // 每页条目 private int currentPage = 1;// 当前页 private int pageNum = 1; // 页码 private List childData;// 子数据 private int allNum;// 总共条目 public PageHelper(List datas, int perPage) { this.allData = datas; if (perPage > 0) this.perPage = perPage; // 如果数据大于10条 if (allData != null && allData.size() > perPage) { childData = allData.subList(0, perPage - 1); } allNum = allData.size(); // 如果总数能除断perPage,页数就是余数,否则+1 pageNum = allNum % perPage == 0 ? (allNum / perPage) : (allNum / perPage + 1); } public int getCount() { return this.allNum; } public int getCurrentPage() { return this.currentPage; } public int getPageNum() { return this.pageNum; } public int getPerPage() { return this.perPage; } public void gotoPage(int n) { // 页面跳转 currentPage = n > pageNum ? pageNum : (n 1; } public void headPage() {// 第一页 currentPage = 1; } public void lastPage() {// 最后一页 currentPage = pageNum; } public void nextPage() {// 下一页 currentPage = hasNextPage() ? currentPage + 1 : pageNum; } public void prePage() {// 前一页 currentPage = hasPrePage() ? currentPage - 1 : 1; } public void setPerPage(int perPage) {// 设置上一页面 this.perPage = perPage; } /** * 获得当前数据 * @return */ public List currentList() { if (currentPage == 1) { childData = allData.subList(0, perPage); } else if (currentPage == pageNum) { childData = allData.subList(perPage * (pageNum - 1), allNum); } else { childData = allData.subList(perPage * (currentPage - 1), perPage * currentPage); } return childData; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } }
2、主页里我根据tab标签把按钮设置进去,然后在click里方法读取,最后根据tag标签就可判断是哪个按钮点击了
/** * 分页主页 * @Project App_Page * @Package com.android.dividepage * @author chenlin * @version 1.0 * @Date 2012年6月2日 * @Note TODO */ public class MainActivity extends Activity implements OnClickListener { private ListView mListView; //分页按钮 private Button mBtnPrePage, mBtnNextPage, mBtnPreItem, mBtnNextItem; //显示分页信息 private TextView mTvPageNo; //数据实现 private PageHelpermPageDaoImpl; private DataAdapter mAdapter; private List mDatas; //被选着的索引 private int selectIndex = 0; private static final int PREPAGE = 0; private static final int NEXTPAGE = 1; private static final int PREITEM = 2; private static final int NEXTITEM = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); initDatas(); } private void initDatas() { //从资源文件里读数据 mDatas = Arrays.asList(getResources().getStringArray(R.array.channellist)); //每次读8条数据 mPageDaoImpl = new PageHelper (mDatas, 8); mAdapter = new DataAdapter(this, mPageDaoImpl.currentList()); mListView.setAdapter(mAdapter); //设置当前页码与总页码 mTvPageNo.setText(mPageDaoImpl.getCurrentPage() + " / " + mPageDaoImpl.getPageNum()); } private void initViews() { mListView = (ListView) findViewById(R.id.page_list); mBtnPrePage = (Button) findViewById(R.id.pre_page); mBtnPrePage.setTag(PREPAGE); mBtnPrePage.setOnClickListener(this); mBtnNextPage = (Button) findViewById(R.id.next_page); mBtnNextPage.setTag(NEXTPAGE); mBtnNextPage.setOnClickListener(this); mBtnPreItem = (Button) findViewById(R.id.pre_item); mBtnPreItem.setTag(PREITEM); mBtnPreItem.setOnClickListener(this); mBtnNextItem = (Button) findViewById(R.id.next_item); mBtnNextItem.setTag(NEXTITEM); mBtnNextItem.setOnClickListener(this); mTvPageNo = (TextView) findViewById(R.id.pagenum); } @Override public void onClick(View v) { final int flag = (Integer) v.getTag(); switch (flag) { case PREPAGE:// 首页 headPage(); break; case NEXTPAGE:// 尾页 lastPage(); break; case PREITEM:// 上一条 prePage(); break; case NEXTITEM:// 下一条 nextPage(); break; } } private void prePage() { if (selectIndex == 0) { if (mPageDaoImpl.getCurrentPage() >= 1) { mPageDaoImpl.prePage(); } mAdapter.setData(mPageDaoImpl.currentList()); mListView.setSelection(mAdapter.getCount() - 1); mTvPageNo.setText(mPageDaoImpl.getCurrentPage() + " / " + mPageDaoImpl.getPageNum()); } else { return; } } private void nextPage() { if (mPageDaoImpl.getCurrentPage()
3、适配器代码,太简单了,我就不说了
public class DataAdapter extends BaseAdapter { private Context mContext; private ListmDatas; public DataAdapter(Context context, List datas) { this.mContext = context; this.mDatas = datas; } @Override public int getCount() { return mDatas == null ? 0 : mDatas.size(); } @Override public Object getItem(int position) { return mDatas == null ? null : mDatas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder mHolder; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.channel_item, parent, false); mHolder = new ViewHolder(); mHolder.nameView = (TextView) convertView.findViewById(R.id.channel_name); convertView.setTag(mHolder); } mHolder = (ViewHolder) convertView.getTag(); mHolder.nameView.setText(mDatas.get(position)); return convertView; } static class ViewHolder { public TextView nameView; } public void setData(List datas) { mDatas = datas; notifyDataSetChanged(); } }
以上就是爱站技术频道小编介绍的Android开发中实现分页效果的简单步骤,接下来还是要在项目中操作,不管是什么功能的开发也是值得程序员重视的。