android 分页加载

两个前提

1 滑动布局尽量不要嵌套滑动布局

2加载更多数据的view是个脚布局


需求

整个布局是gridview + listview

 

上面gridview 下面 listview


处理方案 listview的添加的头布局是这个gridview

view = View.inflate(this, R.layout.item_more_load_more, null);
MyGridView gv_more_load_more_fenye = (MyGridView) view.findViewById(R.id.gv_more_load_more_fenye);

lv.addHeaderView(view);

在回调接口中加载更多数据

lv_more_audio.setOnRefreshListener(new RefreshListView.OnRefreshListener() {
    @Override
    public void onLoadMore() {
        if(audiosList.size() ==count){
         Toast.makeText(MoreAudioActivity2.this,"没有更多数据",Toast.LENGTH_SHORT).show();
            mHandler.sendEmptyMessage(ConfigUtil.NO_MORE_DATA);
        }else {
            getData();
        }
    }
});

自定义listview的代码



public class RefreshListView extends ListView implements AbsListView.OnScrollListener {


    private View mFooterView;
    private int mFooterHeight;

    private ImageView ivArrow;
    private TextView tvState;
    private TextView tvTime;
    private ProgressBar pbLoading;

    int startY;




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

    public RefreshListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initFooterView();
    }

    public RefreshListView(Context context) {
        super(context);
        initFooterView();
    }


    // 初始化脚布局
    private void initFooterView() {
        mFooterView = View.inflate(getContext(),
                R.layout.pull_to_refresh_footer, null);
        this.addFooterView(mFooterView);

        mFooterView.measure(0, 0);
        mFooterHeight = mFooterView.getMeasuredHeight();
        mFooterView.setPadding(0, -mFooterHeight, 0, 0);

        // 设置滑动监听
        this.setOnScrollListener(this);
    }



    private OnRefreshListener mListener;

    public void setOnRefreshListener(OnRefreshListener listener) {
        mListener = listener;
    }

    // 回调接口
    public interface OnRefreshListener {

        // 加载更多监听
        public void onLoadMore();
    }

    // 刷新结束
    public void onRefreshComplete(boolean success) {
            // 隐藏加载更多的布局
            mFooterView.setPadding(0, -mFooterHeight, 0, 0);
            isLoadMore = false;
    }


    private boolean isLoadMore = false;// 标记当前是否正在加载更多

    // 滑动状态变化
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && !isLoadMore) {
            int lastVisiblePosition = getLastVisiblePosition();
            if (lastVisiblePosition == getCount() - 1) {
                isLoadMore = true;
                // 显示脚布局
                mFooterView.setPadding(0, 0, 0, 0);
                // 将listview定位到最后一个item, 让脚布局显示出来
                this.setSelection(getCount() - 1);
                // 开始加载下一页数据
                if (mListener != null) {
                    mListener.onLoadMore();
                }
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
    }
}



pull_to_refresh_footer    的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal">

    <ProgressBar
        android:id="@+id/pb_loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:indeterminateDrawable="@drawable/custom_progress" />

    <ImageView
        android:id="@+id/pull_to_load_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="加载中..."
        android:textColor="@color/blue"
        android:textSize="18sp" />

</LinearLayout>


custom_progress   的代码

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360" >

    <shape
        android:innerRadius="16dp"
        android:shape="ring"
        android:thickness="3dp"
        android:useLevel="false" >
        <gradient
            android:centerColor="@color/blue_new"
            android:endColor="#fff"
            android:startColor="@color/blue_new"
            android:type="sweep" />
    </shape>

</rotate>


如果需要下拉加载 后期 我会加上

但是如果用Google 自带的  可能更加方便

转到 我的另外一个博文



http://blog.csdn.net/itworkermk/article/details/50932646



有什么不懂的可以留言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值