ListView下拉刷新SwipeRefreshLayout + 底部加载

1下拉刷新可以调用google官网定义的下拉控件SwipeRefreshLayout。

1.第一步 添加布局

<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/id_swipe_ly"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/two_a_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff"
            android:divider="@color/hui_f1"
            android:dividerHeight="10dp"></ListView>
    </android.support.v4.widget.SwipeRefreshLayout>

2.实现接口 与 实现方法

implements SwipeRefreshLayout.OnRefreshListener


 @Override
    public void onRefresh() {
        mHandler.sendEmptyMessageDelayed(0x11, 1000);
    }

3.声明对象

private SwipeRefreshLayout mSwipeLayout;
  1. 4.
mSwipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.id_swipe_ly);

        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeColors(Color.RED, Color.RED);

5 在listview中需要控制位在顶部刷新

list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem == 0) {//到顶部了

                    mSwipeLayout.setEnabled(true);
                    two_list_foot_view_rel_layout.setVisibility(View.GONE);
                } else if (visibleItemCount + firstVisibleItem == totalItemCount) {        //到底部了
                    mSwipeLayout.setEnabled(false);

                   mHandler.sendEmptyMessageDelayed(0x12, 1000);//handler发送消息消息  自动刷新
                } else {//在中间
                    mSwipeLayout.setEnabled(false);


                }

            }
        });
    }

2 底部加载

原理: 给listview 添加FooterView 可设置为隐藏 可以通过list.setOnScrollListener方法知道listView已在底部,显示FooterView 并执行加载,加载成功后获得新数据与原有数据进行拼接 更新adapter 隐藏FooterView

1.添加FooterView

View footView = LayoutInflater.from(getActivity()).inflate(R.layout.two_list_foot_layout,null);
        two_a_list.addFooterView(footView);

2 list.setOnScrollListener

list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem == 0) {//到顶部了

                    mSwipeLayout.setEnabled(true);
                    two_list_foot_view_rel_layout.setVisibility(View.GONE);
                } else if (visibleItemCount + firstVisibleItem == totalItemCount) {        //到底部了
                    mSwipeLayout.setEnabled(false);

                   mHandler.sendEmptyMessageDelayed(0x12, 1000);//handler发送消息消息  自动刷新
                } else {//在中间
                    mSwipeLayout.setEnabled(false);


                }

            }
        });
    }

// handler处接收消息
private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 0x11:
                    adapter.notifyDataSetChanged();
                    mSwipeLayout.setRefreshing(false);
                    ToastUtil.showToast(getActivity(),"已刷新");

                    break;
                case 0x12:
                    two_list_foot_view_rel_layout.setVisibility(View.GONE);
                    ToastUtil.showToast(getActivity(),"加载更多");

                    break;
            }
        }

3 数据拼接

原理 判断是否为第一次加载adapter 如果是这 new adapter 如果是底部加载获得的新数据这进行拼接 更新

        DongtaiBeans dongtaiBeans = new DongtaiBeans(jsonObject); // 解析json 获得javaBean
        pageNum = Integer.valueOf(dongtaiBeans.getPage());   // 获取总过页数  通过此判断是否加载

        if (mRefreshHeadFlag) {
            adapter = null;                 // adapter是否为空
        }
        if (adapter == null) {                  // adapter为空 new adapter 
            detailInfos = dongtaiBeans.getZxdtlist();  
            adapter = new DongtaiListAdapter(detailInfos);
            listView.setAdapter(adapter);    // 设置adapter 
        } else {                                 // adapter不为空 
            detailInfos.addAll(dongtaiBeans.getZxdtlist());    //  数据源拼接数据
            adapter.notifyDataSetChanged();             //  更新数据
        }

3 完整代码

<?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:background="#fff"
    android:orientation="vertical">

    <com.lpf.lpf.refresh.PullToRefreshView
        android:id="@+id/main_pull_refresh_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ListView
            android:id="@+id/two_a_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff"
            android:divider="@color/hui_f1"
            android:dividerHeight="10dp"></ListView>
    </com.lpf.lpf.refresh.PullToRefreshView>


</LinearLayout>


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:orientation="vertical">


    <RelativeLayout
        android:id="@+id/two_list_foot_view_rel_layout"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#f1f1f1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="正在加载"
            android:textColor="#696969"
            android:textSize="16sp" />

    </RelativeLayout>

</LinearLayout>

package com.taisheng.xiaofang.com.taisheng.xiaofang.two;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lpf.lpf.net.AsyncTaskJsonUtil;
import com.lpf.lpf.net.ThreadJsonUtils;
import com.lpf.lpf.refresh.PullToRefreshView;
import com.lpf.lpf.utils.ToastUtil;
import com.taisheng.xiaofang.R;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class AFrag extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    private View view;
    private int mWidth;

    private ListView two_a_list;
    private List<String> list;


    private boolean mRefreshHeadFlag = true;// 判断是否刷新的是头部
    private boolean mRefreshFlag = false;// 判断是否刷新
    private int pageNum;
    private List<String> detailInfos;

    private SwipeRefreshLayout mSwipeLayout;

    private  MyShouyeListAdapter adapter;
    private RelativeLayout two_list_foot_view_rel_layout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.two_a, container, false);
        mWidth = getResources().getDisplayMetrics().widthPixels;
        initView();
        initData();

        return view;
    }

    private void initView() {
        mSwipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.id_swipe_ly);

        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeColors(Color.RED, Color.RED);


        two_a_list = (ListView) view.findViewById(R.id.two_a_list);
        two_a_list.setFocusable(false);

        View footView = LayoutInflater.from(getActivity()).inflate(R.layout.two_list_foot_layout,null);
        two_a_list.addFooterView(footView);
        two_list_foot_view_rel_layout = (RelativeLayout) footView.findViewById(R.id.two_list_foot_view_rel_layout);



        two_a_list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem == 0) {//到顶部了

                    mSwipeLayout.setEnabled(true);
                    two_list_foot_view_rel_layout.setVisibility(View.GONE);
                } else if (visibleItemCount + firstVisibleItem == totalItemCount) {        //到底部了
                    mSwipeLayout.setEnabled(false);
                    two_list_foot_view_rel_layout.setVisibility(View.VISIBLE);
                    mHandler.sendEmptyMessageDelayed(0x12, 1000);
                } else {//在中间
                    mSwipeLayout.setEnabled(false);
                    two_list_foot_view_rel_layout.setVisibility(View.VISIBLE);

                }

            }
        });
    }

    private void initData() {

        list = new ArrayList<>();


        String path = URLson.最新动态+"?pagenum=1";

        AsyncTaskJsonUtil jsonUtil = new AsyncTaskJsonUtil(getActivity(), null,
                null, false, null, true, "请稍等......", new AsyncTaskJsonUtil.ResultCallBack() {

            @Override
            public void JSONResult(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                DongtaiBeans dongtaiBeans = new DongtaiBeans(jsonObject);
                pageNum = Integer.valueOf(dongtaiBeans.getPage());

                if (mRefreshHeadFlag) {
                    adapter = null;
                }
                if (adapter == null) {
                    detailInfos = dongtaiBeans.getZxdtlist();
                    adapter =  new MyShouyeListAdapter(list);
                    two_a_list.setAdapter(adapter);
                } else {
                    detailInfos.addAll(dongtaiBeans.getZxdtlist());
                    adapter.notifyDataSetChanged();
                }


            }
        });
        jsonUtil.execute(path);


    }


    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 0x11:
                    adapter.notifyDataSetChanged();
                    mSwipeLayout.setRefreshing(false);
                    ToastUtil.showToast(getActivity(),"已刷新");

                    break;
                case 0x12:

                    two_list_foot_view_rel_layout.setVisibility(View.GONE);
                    ToastUtil.showToast(getActivity(),"加载更多");

                    break;
            }
        }

        ;
    };

    @Override
    public void onRefresh() {
        mHandler.sendEmptyMessageDelayed(0x11, 1000);
    }


    class MyShouyeListAdapter extends BaseAdapter {


        List<String> list;

        public MyShouyeListAdapter(List<String> list) {
            this.list = list;
        }


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

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            ViewHolder holder;

            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.two_a_list_item_layout, null);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }


            return convertView;
        }

        class ViewHolder {

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值