1. 程式人生 > >Android 實現簡單的分頁

Android 實現簡單的分頁

1.實現分頁最主要的就是封裝分頁程式碼,然後在按鈕裡實現相關的操作

/**
 * 分頁工具
 * 
 * @Project App_Page
 * @Package com.android.dividepage
 * @author chenlin
 * @version 1.0
 * @Date 2012年6月2日
 * @Note TODO
 * @param <T>
 */
public class PageHelper<T> {

    private List<T> allData; // 所有資料
    private int perPage = 10
; // 每頁條目 private int currentPage = 1;// 當前頁 private int pageNum = 1; // 頁碼 private List<T> childData;// 子資料 private int allNum;// 總共條目 public PageHelper(List<T> 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 ? 1 : n); } public boolean hasNextPage() {// 是否有下一頁 return currentPage < pageNum; } public boolean hasPrePage() {// 是否有前一頁 return currentPage > 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<T> 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 PageHelper<String> mPageDaoImpl;
    private DataAdapter mAdapter;
    private List<String> 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<String>(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() <= mPageDaoImpl.getPageNum()) {
            mPageDaoImpl.nextPage();
        } 
        mAdapter.setData(mPageDaoImpl.currentList());
        mListView.setSelection(0);
        mTvPageNo.setText(mPageDaoImpl.getCurrentPage() + " / " + mPageDaoImpl.getPageNum());
    }



    private void lastPage() {
        if (mPageDaoImpl.getCurrentPage() != mPageDaoImpl.getPageNum()) {
            mPageDaoImpl.lastPage();
        } 
        mAdapter.setData(mPageDaoImpl.currentList());
        mTvPageNo.setText(mPageDaoImpl.getCurrentPage() + " / " + mPageDaoImpl.getPageNum());
    }

    private void headPage() {
        if (mPageDaoImpl.getCurrentPage() != 1) {
            mPageDaoImpl.headPage();
        } 
        mAdapter.setData(mPageDaoImpl.currentList());
        mTvPageNo.setText(mPageDaoImpl.getCurrentPage() + " / " + mPageDaoImpl.getPageNum());
    }

}

3、介面卡程式碼,太簡單了,我就不說了

public class DataAdapter extends BaseAdapter {

    private Context mContext;
    private List<String> mDatas;

    public DataAdapter(Context context, List<String> 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<String> datas) {
        mDatas = datas;
        notifyDataSetChanged();
    }
}

———————————————————————
有需求者請加qq:136137465,非誠勿擾
(java 架構師全套教程,共760G, 讓你從零到架構師,每月輕鬆拿3萬)
01.高階架構師四十二個階段高
02.Java高階系統培訓架構課程148課時
03.Java高階網際網路架構師課程
04.Java網際網路架構Netty、Nio、Mina等-視訊教程
05.Java高階架構設計2016整理-視訊教程
06.架構師基礎、高階片
07.Java架構師必修linux運維繫列課程
08.Java高階系統培訓架構課程116課時
(送:hadoop系列教程,java設計模式與資料結構, Spring Cloud微服務, SpringBoot入門)
——————————————————————–