自定义SwipeRefreshLayout 嵌套listview 并解决item点击事件焦点冲突问题

最近项目需要实现上拉加载更多,为了不引入第三方库,可以使用自定义SwipeRefreshLayout 来实现,
作品摘自 简书:http://www.jianshu.com/p/d23b42b6360b
但是在使用的时候发现有个bug,就是当页面显示最后一条数据的时候 ,listview 的点击事件不响应了,但是响应的是正在加载。所以我就在原来的基础上稍加改动:
一下是自定义的全部代码:


import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.ListView;

import .R;

/**
 * @author: qiaohao
 * time: 2017/11/14 15:57
 * desc:PulltoRefreshView
 */
public class PulltoRefreshView extends SwipeRefreshLayout implements AbsListView.OnScrollListener {


    /**
     * 滑动到最下面时的上拉操作
     */

    private int mTouchSlop;
    /**
     * listview实例
     */
    private ListView mListView;

    /**
     * 上拉监听器, 到了最底部的上拉加载操作
     */
    private OnLoadListener mOnLoadListener;

    /**
     * ListView的加载中footer
     */
    private View mListViewFooter;

    /**
     * 按下时的y坐标
     */
    private int mYDown;
    /**
     * 抬起时的y坐标, 与mYDown一起用于滑动到底部时判断是上拉还是下拉
     */
    private int mLastY;
    /**
     * 是否在加载中 ( 上拉加载更多 )
     */
    private boolean isLoading = false;

    private boolean isActionDown;



    public PulltoRefreshView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        mListViewFooter = LayoutInflater.from(context).inflate(R.layout.listview_footer, null,
                false);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        // 初始化ListView对象
        if (mListView == null) {
            getListView();
        }
    }

    /**
     * 获取ListView对象
     */
    private void getListView() {
        int childs = getChildCount();
        if (childs > 0) {
            View childView = getChildAt(1);
            if (childView instanceof ListView) {
                mListView = (ListView) childView;
                // 设置滚动监听器给ListView, 使得滚动的情况下也可以自动加载
                mListView.setOnScrollListener(this);
            }
        }
    }

    /*
     * (non-Javadoc)
     * @see android.view.ViewGroup#dispatchTouchEvent(android.view.MotionEvent)
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                // 按下
                mYDown = (int) event.getRawY();
                isActionDown=true;
                break;
		//注意这里的代码跟原文的代码有点区别是为了上拉的时候显示Loadding布局
            case MotionEvent.ACTION_MOVE:
                // 移动
                if (canLoadMore()) {
                    loadData();
                }
                break;

            case MotionEvent.ACTION_UP:
                // 抬起
                isActionDown=false;
                mLastY = (int) event.getRawY();
                break;
            default:
                break;
        }

        return super.dispatchTouchEvent(event);
    }

   /**
     * 判断是否满足加载更多条件
     *
     * @return
     */
    private boolean canLoadMore() {
        // 1. 是上拉状态
        boolean condition1 = (mYDown - mLastY) >= mTouchSlop;
        if (condition1) {
            System.out.println("是上拉状态");
        }
        // 2. 当前页面可见的item是最后一个条目
        boolean condition2 = false;
        if (mListView != null && mListView.getAdapter() != null) {
            condition2 = mListView.getLastVisiblePosition() == (mListView.getAdapter().getCount() - 1);
        }
        if (condition2) {
            System.out.println("是最后一个条目");
        }
        // 3. 正在加载状态
        boolean condition3 = !isLoading;
        if (condition3) {
            System.out.println("不是正在加载状态");
        }
        return condition1 && condition2 && condition3;
    }

    /**
     * 处理加载数据的逻辑
     */
    private void loadData() {
        System.out.println("加载数据...");
        if (mOnLoadListener != null) {
            // 设置加载状态,让布局显示出来
            setLoading(true);
            mOnLoadListener.onLoad();
        }

    }



    /**
     * @param loading
     */
    public void setLoading(boolean loading) {
        isLoading = loading;
        if (isLoading) {
            mListView.addFooterView(mListViewFooter);
        } else {
            mListView.removeFooterView(mListViewFooter);
            mYDown = 0;
            mLastY = 0;
        }
    }

    /**
     * @param loadListener
     */
    public void setOnLoadListener(OnLoadListener loadListener) {
        mOnLoadListener = loadListener;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                         int totalItemCount) {
        // 滚动时到了最底部也可以加载更多
//        if (canLoadMore()) {
//            loadData();
//        }
    }

    /**
     * 加载更多的监听器
     *
     * @author mrsimple
     */
    public static interface OnLoadListener {
        public void onLoad();
    }
}


使用方法:

xml

 <com.view.custom.PulltoRefreshView
        android:id="@+id/refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:background="@color/white"
            android:id="@+id/rv_record_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            ></ListView>

    </com.view.custom.PulltoRefreshView>


java:

refreshLayout = (PulltoRefreshView) findViewById(R.id.refresh_layout);
        //设置刷新时动画的颜色,可以设置4个
        refreshLayout.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);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadListener(this);


分别去实现

SwipeRefreshLayout.OnRefreshListener,PulltoRefreshView.OnLoadListener
这两个接口,然后重写onRefresh(){}
和  onLoad(){}
加载更多布局 R.layout.listview_footer xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:gravity="center"
    android:paddingBottom="8dip"
    android:paddingTop="5dip"
    android:focusable="false">

    <ProgressBar
        android:id="@+id/pull_to_refresh_load_progress"
        style="@android:style/Widget.ProgressBar.Small.Inverse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:paddingRight="100dp"
        android:indeterminate="true" />

    <TextView
        android:layout_marginLeft="17dp"
        android:id="@+id/pull_to_refresh_loadmore_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:paddingTop="5dip"
        android:text="正在加载更多..."
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/darker_gray"
        android:textSize="14sp"
        android:textStyle="bold" />

</RelativeLayout>

解决item点击事件焦点冲突的方法:item 布局的根布局中加入android:descendantFocusability="blocksDescendants"




          
展开阅读全文

没有更多推荐了,返回首页