最新消息:文章中包含代码时,请遵守代码高亮规范!

【原创】ScrollView嵌套ListView,ListView分页加载数据问题

Android 王 伟 129浏览 0评论

在开发中,遇到一个问题。ScrollView里嵌套一个ListView。大多数的做法是重写一个MyListView来解决。


public class MyListView extends ListView{

      public MyListView(Context context){
          super(context);
      }

      public MyListView(Context context,AttributeSet attrs,int defStyle){
          super(context,attrs,defStyle);
      }

      public MyListView(Context context,AttributeSet attrs)
          super(context,attrs);
      }

      @Override
      protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec){
           int expandSpec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
           super.onMeasure(widthMeasureSpec,expandSpec);
      }
}

这种方式,其实就是重写onMeasure方法,但是这样做分页的效果就不行了。因为在ListView的onMeasure重写之后,通常所用的setOnScrollListener失去作用,无法监听ListView的滚动事件。 网上目前的方式是通过监听ScrollView滚动到底部,来实现分页加载。我这里说另一种方式。我们可以将ListView上面的所有的布局作为一个View,然后作为ListView的头,添加进去。那么就是相当于布局就是一个ListView,Listview本身就是带滑动的。那么就没有ScrollView的什么事了。看代码吧。


public class MainActivity extends AppCompatActivity {
    private SwipeRefreshLayout mRefreshLayout;
    private ListView mLvContent;
    private int mPage = 1;
    private int mCount = 10;
    private List<String> mListData = new ArrayList<>();
    private List<String> mPageData = new ArrayList<>();
    private MyAdapter mAdapter;
    private boolean mHasData;
    private int lastVisibleItem;
    private int totalItemAllCount;
    private int remainCount;

    private SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mPage = 1;
            remainCount = mListData.size();
            mPageData.clear();
            mAdapter.notifyDataSetChanged();
            getData();
        }
    };

    private void getData() {
        mPageData.clear();
        if (remainCount < mCount) {
            for (int i = 0; i < mListData.size(); i++) {
                mPageData.add(mListData.get(i));
            }
            mAdapter.notifyDataSetChanged();
        } else {
            for (int i = 0; i < mPage * mCount; i++) {
                mPageData.add(mListData.get(i));
            }
            mAdapter.notifyDataSetChanged();
            remainCount = mListData.size() - mPage * mCount;
            mPage++;
            mHasData = true;
        }
        mRefreshLayout.setRefreshing(false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
    }

    private void initData() {
        for (int i = 0; i < 54; i++) {
            mListData.add("这是第" + (i + 1) + "个数据");
        }
        remainCount = mListData.size();
    }

    private void initHeader() {
        View view = LayoutInflater.from(this).inflate(R.layout.view_header, null);
        mLvContent.addHeaderView(view);
    }

    private void initView() {
        mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refreshLayout);
        mRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        mRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
        mRefreshLayout.setDistanceToTriggerSync(100);
        mRefreshLayout.setProgressBackgroundColor(R.color.white);
        mRefreshLayout.setOnRefreshListener(mOnRefreshListener);

        mLvContent = (ListView) findViewById(R.id.Lv_content);
        initHeader();
        mAdapter = new MyAdapter();
        mLvContent.setAdapter(mAdapter);
        mLvContent.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (mHasData && totalItemAllCount == lastVisibleItem && scrollState == SCROLL_STATE_IDLE) {
                    mHasData = false;
                    getData();
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                lastVisibleItem = firstVisibleItem + visibleItemCount;
                totalItemAllCount = totalItemCount;
            }
        });
    }

    class MyAdapter extends BaseAdapter {

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

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

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

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (view == null) {
                view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_content, null);
                holder = new ViewHolder();
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            holder.tvContent = (TextView) view.findViewById(R.id.tv_content);
            holder.tvContent.setText(mPageData.get(i));
            return view;
        }

        class ViewHolder {
            TextView tvContent;
        }
    }
}

转载时请注明出处及相应链接,本文永久地址:http://blog.it985.com/22406.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者Bob的打赏,我们会更加努力!    如果您想成为作者,请点我

您必须 登录 才能发表评论!