其他分享
首页 > 其他分享> > 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;

    }
}

 

标签:int,view,list,上拉,Override,ListView,holder,public,加载
来源: https://www.cnblogs.com/youyuan1980/p/16149971.html