1. 程式人生 > 其它 >ListView上拉載入,下拉重新整理功能

ListView上拉載入,下拉重新整理功能

直接上程式碼了,先上activity程式碼,下載就不用說了,直接在日誌那地方寫就行,主要是上拉,需要用到listview自己的監聽事件,依靠滑動的對應值來載入資料,然後並傳入資料介面卡中,來對尾佈局內的值進行修改。

public class MainActivity3 extends AppCompatActivity {

    private static final String TAG = MainActivity3.class.getSimpleName();
    SwipeRefreshLayout sf1;
    ListView listView1;
    MainAdapter mainAdapter;
    List
<MainBean> list; boolean isLastRow=false; int page=1; int pagesize=20; @RequiresApi(api = Build.VERSION_CODES.M) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); SwipeRefreshLayout sf1
= findViewById(R.id.sr1); listView1 = findViewById(R.id.list1); list = new ArrayList<>(); mainAdapter = new MainAdapter(this,list); listView1.setAdapter(mainAdapter); getList(); sf1.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override
public void onRefresh() { Log.i(TAG, "onRefresh: 載入"); sf1.setRefreshing(false); } }); listView1.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView absListView, int i) { //第1次:scrollState = SCROLL_STATE_TOUCH_SCROLL(1) 正在滾動 //第2次:scrollState = SCROLL_STATE_FLING(2) 手指做了拋的動作(手指離開螢幕前,用力滑了一下) //第3次:scrollState = SCROLL_STATE_IDLE(0) 停止滾動 if(isLastRow&&i==SCROLL_STATE_IDLE) { mainAdapter.changeMoreStatus(MainAdapter.LOADING_MORE); new Handler().postDelayed(new Runnable() { @Override public void run() { getList(); isLastRow = false; mainAdapter.changeMoreStatus(MainAdapter.PULLUP_LOAD_MORE); } }, 2500); } } @Override public void onScroll(AbsListView absListView, int i, int i1, int i2) { //滾動時一直回撥,直到停止滾動時才停止回撥。單擊時回撥一次。 //i:當前能看見的第一個列表項ID(從0開始) //i1:當前能看見的列表項個數(小半個也算) //i2:列表項共數 if(i+i1==i2&&i2>0){ isLastRow=true; } } }); } private void getList() { if(page<3) { int startInt = 1 + (page - 1) * pagesize; for (int i = startInt; i < startInt + pagesize; i++) { list.add(new MainBean(String.valueOf(i), "regnum:" + i)); } page++; mainAdapter.set_list(list); mainAdapter.notifyDataSetChanged(); } } }

資料介面卡程式碼

public class MainAdapter extends BaseAdapter {

    private static final  int TYPE_ITEM=0;
    private static final  int TYPE_FOOTER=1;
    //上拉載入更多
    public static final int  PULLUP_LOAD_MORE=0;
    //正在載入中
    public static final int  LOADING_MORE=1;

    private int load_more_status=0;

    private String footerText ;

    public String getFooterText() {
        return footerText;
    }

    public void setFooterText(String footerText) {
        this.footerText = footerText;
    }

    private static final String TAG = MainAdapter.class.getSimpleName();

    private LayoutInflater _mInflaster;

    public List<MainBean> get_list() {
        return _list;
    }

    public void set_list(List<MainBean> _list) {
        this._list = _list;
    }

    private List<MainBean> _list;

    public MainAdapter(Context context, List<MainBean> list) {
        _mInflaster = LayoutInflater.from(context);
        _list = list;
    }

    public void changeMoreStatus(int status){
        load_more_status=status;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return _list.size();
    }

    @Override
    public Object getItem(int i) {
        return _list.get(i);
    }

    @Override
    public int getItemViewType(int position) {
        if(position+1== _list.size()+1){
            return TYPE_FOOTER;
        }else {
            return TYPE_ITEM;
        }
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if(i+1== _list.size()){

            ViewHodler_Footer viewHodler_footer = null;
            if(viewHodler_footer==null) {
                viewHodler_footer = new ViewHodler_Footer();
                view = _mInflaster.inflate(R.layout.foot_list,null);
                viewHodler_footer.foot_text = view.findViewById(R.id.foot_text);
                view.setTag(viewHodler_footer);
            }
            else
            {
                viewHodler_footer = (ViewHodler_Footer) view.getTag();
            }
            switch (load_more_status)
            {
                case PULLUP_LOAD_MORE:
                    viewHodler_footer.foot_text.setText("上拉載入更多...");
                    break;
                case LOADING_MORE:
                    viewHodler_footer.foot_text.setText("正在載入更多資料...");
                    break;
            }
            return view;
        }else {
            ViewHolder holder=null;
            if(holder==null)
            {
                holder = new ViewHolder();
                view =  _mInflaster.inflate(R.layout.list_item,null);
                holder.t1 = view.findViewById(R.id.list_item_id);
                holder.t2 = view.findViewById(R.id.list_item_regnum);
                view.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) view.getTag();

            }
            holder.t1.setText(_list.get(i).getId());
            holder.t2.setText(_list.get(i).getRegnum());

            return view;
        }
    }

    public final class ViewHodler_Footer{
        public TextView foot_text;
    }

    public final class ViewHolder{
        public TextView t1;
        public TextView t2;

    }
}