自定义listView到达底部自动加载更多

//下拉头布局

name="grey">#FF999999

name="refresh_color_1">#ff00ddff
name="refresh_color_2">#ff99cc00
name="refresh_color_3">#ffffbb33
name="refresh_color_4">#ffff4444
//底部自动加载布局
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical">
            android:layout_width="match_parent"
        android:layout_height="48dp"
        android:gravity="center">

                    android:id="@+id/footer_progressbar"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginRight="8dp"
            android:gravity="center"
            android:indeterminateDrawable="@anim/update_loading_progressbar"
            android:visibility="visible" />

                    android:id="@+id/footer_hint_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="正在加载中"
            android:textColor="#999999"
            android:textSize="14dp" />
    


//自定义listview
public class LoadMoreListView extends ListView implements AbsListView.OnScrollListener {
    private View rooterView;
    private boolean isHaveMoreData = true;// 是否有更多数据(默认为有)
    private ProgressBar progressBar;
    private TextView tipContext;

    private RefreshAndLoadMoreView mRefreshAndLoadMoreView;
    private boolean isLoading = false;// 是否正在加载

    private OnLoadMoreListener mOnLoadMoreListener;

    public LoadMoreListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //动态载入底部布局
        rooterView = LayoutInflater.from(context).inflate(
                R.layout.pull_to_load_footer, null);
        progressBar = (ProgressBar) rooterView.findViewById(R.id.footer_progressbar);
        tipContext = (TextView) rooterView.findViewById(R.id.footer_hint_textview);
        //listView的底部添加布局(此时当给listView设置Item点击事件的时候,默认不触发这个添加的布局的点击事件)
        addFooterView(rooterView, null, false);
        setOnScrollListener(this);
    }

    public void setRefreshAndLoadMoreView(RefreshAndLoadMoreView mRefreshAndLoadMoreView) {
        this.mRefreshAndLoadMoreView = mRefreshAndLoadMoreView;
    }

    /**
     * 设置是否还有更多数据
     *
     * @param isHaveMoreData
     */
    public void setHaveMoreData(boolean isHaveMoreData) {
        this.isHaveMoreData = isHaveMoreData;
        if (!isHaveMoreData) {
            tipContext.setText("只有这么多啦");
            progressBar.setVisibility(View.GONE);
        } else {
            tipContext.setText("正在加载");
            progressBar.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 加载完成
     */
    public void onLoadComplete() {
        isLoading = false;
    }

    public boolean isLoading() {
        return isLoading;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
            if (view.getLastVisiblePosition() == view.getCount() - 1 && (mRefreshAndLoadMoreView != null &&
                    !mRefreshAndLoadMoreView.isRefreshing()) && !isLoading && mOnLoadMoreListener != null && isHaveMoreData) {
                isLoading = true;
                mOnLoadMoreListener.onLoadMore();
            }
        }
    }

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

    }

    /**
     * 加载更多的监听
     */
    public static interface OnLoadMoreListener {
        public void onLoadMore();
    }

    /**
     * 设置加载监听
     *
     * @param mOnLoadMoreListener
     */
    public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
        this.mOnLoadMoreListener = mOnLoadMoreListener;
    }
}

//自定义下拉刷新控件
public class RefreshAndLoadMoreView extends SwipeRefreshLayout {
    private LoadMoreListView mLoadMoreListView;

    /**
     * 构造方法,用于在布局文件中用到这个自定义SwipeRefreshLayout控件
     * @param context
     * @param attrs
     */
    public RefreshAndLoadMoreView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Resources res = getResources();
        //通过颜色资源文件设置进度动画的颜色资源
        setColorSchemeColors(res.getColor(R.color.refresh_color_1),
                res.getColor(R.color.refresh_color_2),
                res.getColor(R.color.refresh_color_3),
                res.getColor(R.color.refresh_color_4));
    }
    public void setLoadMoreListView(LoadMoreListView mLoadMoreListView) {
        this.mLoadMoreListView = mLoadMoreListView;
    }

    /**
     * 触屏事件,如果ListView不为空且数据还在加载中,则继续加载直至完成加载才触摸此事件
     * @param ev
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (mLoadMoreListView != null && mLoadMoreListView.isLoading()) {
            return false;
        }
        return super.onTouchEvent(ev);
    }
}

//Activity布局
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.athis.app.activity.Main22Activity">
            android:id="@+id/mRefreshAndLoadMoreView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
                    android:id="@+id/mLoadMoreListView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        
    

//Activity
public class Main22Activity extends AppCompatActivity {

    private RefreshAndLoadMoreView mRefreshAndLoadMoreView;
    private LoadMoreListView mLoadMoreListView;
    private int page =0;
    private List listdata = new ArrayList<>();
    private Main_Fragnebt_Adapter adapter;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==0){
                if (adapter==null){
                adapter = new Main_Fragnebt_Adapter(Main22Activity.this,listdata);
                mLoadMoreListView.setAdapter(adapter);
                }else {
                    adapter.notifyDataSetChanged();
                }
                mRefreshAndLoadMoreView.setRefreshing(false);
                mLoadMoreListView.onLoadComplete();
            }
            if (msg.what==1){
                mRefreshAndLoadMoreView.setRefreshing(false);
                mLoadMoreListView.onLoadComplete();
                mLoadMoreListView.setHaveMoreData(false);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main22);
        initView();
        addData(page);
    }

    private void initView() {
        mRefreshAndLoadMoreView=(RefreshAndLoadMoreView)findViewById(R.id.mRefreshAndLoadMoreView);
        mLoadMoreListView=(LoadMoreListView)findViewById(R.id.mLoadMoreListView);
        mRefreshAndLoadMoreView.setLoadMoreListView(mLoadMoreListView);
        mLoadMoreListView.setRefreshAndLoadMoreView(mRefreshAndLoadMoreView);
        //设置下拉刷新监听
        mRefreshAndLoadMoreView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mLoadMoreListView.setHaveMoreData(true);
                listdata.clear();
                page=0;
               addData(page);

            }
        });
        //设置加载监听
        mLoadMoreListView.setOnLoadMoreListener(new LoadMoreListView.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                addData(page);
            }
        });

}
    public void addData(int page){
       //获取网络数据
        }
    }

}

你可能感兴趣的:(自定义listView到达底部自动加载更多)