无标题文章

public class CustomListView extends ListView implements NestedScrollingChild, AbsListView.OnScrollListener {

    /**
     * 滚动到底部回掉接口
     */
    public interface OnBottonListener {

        /**
         * 加载更多
         */
        public void loadMore();
    }

    //footer XML布局
    private View footerView;
    private ImageView footerImg;
    private TextView footerText;

    private int firstItemIndex;
    private int currentScrollState;

    private OnBottonListener onBottonListener;

    private View placeholder_listview_headerview;

    /**
     * 每页总数
     */
    private int pageSize = 10;
    /**
     * 满足查询条件的总数
     */
    private long total = 0;

    private NestedScrollingChildHelper mNestedScrollingChildHelper;

    public CustomListView(final Context context) {
        super(context);
        initHelper();
        init(context);
    }

    public CustomListView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        initHelper();
        init(context);
    }

    public CustomListView(final Context context, final AttributeSet attrs, final int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initHelper();
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomListView(final Context context, final AttributeSet attrs, final int defStyleAttr, final int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initHelper();
        init(context);
    }

    private void initHelper() {
        mNestedScrollingChildHelper = new NestedScrollingChildHelper(this);
        setNestedScrollingEnabled(true);
    }

    @Override
    public void setNestedScrollingEnabled(final boolean enabled) {
        mNestedScrollingChildHelper.setNestedScrollingEnabled(enabled);
    }

    @Override
    public boolean isNestedScrollingEnabled() {
        return mNestedScrollingChildHelper.isNestedScrollingEnabled();
    }

    @Override
    public boolean startNestedScroll(final int axes) {
        return mNestedScrollingChildHelper.startNestedScroll(axes);
    }

    @Override
    public void stopNestedScroll() {
        mNestedScrollingChildHelper.stopNestedScroll();
    }

    @Override
    public boolean hasNestedScrollingParent() {
        return mNestedScrollingChildHelper.hasNestedScrollingParent();
    }

    @Override
    public boolean dispatchNestedScroll(final int dxConsumed, final int dyConsumed, final int dxUnconsumed, final int dyUnconsumed, final int[] offsetInWindow) {
        return mNestedScrollingChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedPreScroll(final int dx, final int dy, final int[] consumed, final int[] offsetInWindow) {
        return mNestedScrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedFling(final float velocityX, final float velocityY, final boolean consumed) {
        return mNestedScrollingChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
    }

    @Override
    public boolean dispatchNestedPreFling(final float velocityX, final float velocityY) {
        return mNestedScrollingChildHelper.dispatchNestedPreFling(velocityX, velocityY);
    }

    /**
     * 初始化
     */
    private void init(Context context) {
        // footerView
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        footerView = inflater.inflate(R.layout.addmore_listview_footer, null);
        footerImg = (ImageView) footerView.findViewById(R.id.loadMoreImg);
        footerText = (TextView) footerView.findViewById(R.id.loadMoreHintTv);
        footerView.setTag(true);

        //占位,让可以在setAdapter之后调用addFooterView有效果
        placeholder_listview_headerview = inflater.inflate(R.layout.placeholder_listview_headerview, null);
        addFooterView(placeholder_listview_headerview, null, false);

        setOnScrollListener(this);
    }

    /**
     * 对比每页总数和满足条件的所有条数。应该在onScrollStateChanged设置,
     * 当total > pageSize时滚动条触底才能出现加载更多的footerView。
     * 注意:必须在具体的应用中调用。
     */
    public void setIsAddFooterView(int pageSize, Long total) {
        this.pageSize = pageSize;
        this.total = total;
    }

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

        int lastIndex = view.getLastVisiblePosition();// 适配器数据集在屏幕中显示的最后一项
        int viewCount = view.getCount() - 1; // 适配器中包含的view的总条目数

        // 列表为空
        if (viewCount <= 0) {
            return;
        }

        switch (scrollState) {
            case SCROLL_STATE_IDLE: // 停止滚动
                boolean hasAddFooterView = (Boolean) footerView.getTag();
                if (lastIndex == viewCount && hasAddFooterView && pageSize < total) { // 滚动到最后一项目
                    addListFooterView();
                    onBottonListener.loadMore();
                }
                break;
            default:
                break;
        }
    }

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

    /**
     * 添加列表页脚
     */
    public View addListFooterView() {
        footerView.setVisibility(VISIBLE);
        footerView.setTag(false);

        footerImg.setVisibility(View.VISIBLE);
        footerText.setText("努力加载中……");

        addFooterView(footerView, null, false);

        //选定当前项
        setSelection(getLastVisiblePosition());

        // 设置图标动画
        Animation operatingAnim = AnimationUtils.loadAnimation(getContext(),
                R.anim.loading_more_anim);
        footerImg.startAnimation(operatingAnim);

        return footerView;
    }

    /**
     * 根据各种加载状态设置页脚文字
     *
     * @param status 加载更多失败的状态 “1”:已经加载完毕,没有更多数据 “2”:加载更多失败,可能是网络不好等
     * @param text   对应状态下的文字提示
     */
    public void setFooterViewText(int status, String text) {
        if (!(Boolean) footerView.getTag()) {
            footerText.setText(text);
            footerImg.clearAnimation();
            footerImg.setVisibility(View.GONE);

            if (status == 2) {
                footerView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        onClickFooterToReloadMore();
                    }
                });
            }
        }
    }

    /**
     * 点击页脚重新加载更多
     */
    private void onClickFooterToReloadMore() {
        removeListFooterView();
        addListFooterView();
        onBottonListener.loadMore();
    }

    /**
     * 加载更多后,移除页脚
     */
    public void removeListFooterView() {
        footerView.setTag(true);
        footerImg.clearAnimation(); // 清除动画
        removeFooterView(footerView);
    }

    public OnBottonListener getOnBottonListener() {
        return onBottonListener;
    }

    public void setOnBottonListener(OnBottonListener onBottonListener) {
        this.onBottonListener = onBottonListener;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<com.edate.eui.widget.NltiSwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tl="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:addStatesFromChildren="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.design.widget.CoordinatorLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/main.collapsing"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
                <!--app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">-->
                <!--app:layout_scrollFlags="scroll|enterAlways"-->
                <!---->
                <RelativeLayout
                    android:id="@+id/rel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#dedede"
                    android:fitsSystemWindows="true"
                    app:layout_collapseMode="parallax">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <com.allure.lbanners.LMBanners
                            android:id="@+id/banners"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <com.edate.appointment.common.view.MyGridView
                            android:id="@+id/gridView"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="@dimen/dimen_7"
                            android:background="@color/white"
                            android:numColumns="4"
                            android:paddingBottom="8dp"
                            android:paddingTop="7dp"
                            android:scrollbars="none" />
                    </LinearLayout>
                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/layout_search"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dimen_28"
                    android:layout_margin="@dimen/dimen_10"
                    android:background="@drawable/edit_text_input_grey_bg"
                    app:layout_collapseMode="pin">

                    <com.edate.appointment.common.view.font.MyFontTextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:drawableLeft="@drawable/bt_search"
                        android:drawablePadding="3dp"
                        android:gravity="center"
                        android:text="易约"
                        android:textColor="@color/grey21"
                        android:textSize="13sp" />
                </RelativeLayout>
            </android.support.design.widget.CollapsingToolbarLayout>

            <com.hyphenate.easeui.tablayout.SlidingTabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dimen_40"
                android:background="@color/white"
                tl:tl_indicator_color="@color/text_red_color"
                tl:tl_indicator_height="1dp"
                tl:tl_indicator_margin_left="40dp"
                tl:tl_indicator_margin_right="40dp"
                tl:tl_tab_padding="0dp"
                tl:tl_tab_space_equal="true"
                tl:tl_textSelectColor="@color/text_red_color"
                tl:tl_textUnselectColor="@color/grey11"
                tl:tl_textsize="@dimen/size_14"
                tl:tl_underline_color="@color/grey91"
                tl:tl_underline_gravity="BOTTOM"
                tl:tl_underline_height="0.5dp" />
        </android.support.design.widget.AppBarLayout>

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </android.support.v4.widget.NestedScrollView>
    </android.support.design.widget.CoordinatorLayout>
</com.edate.eui.widget.NltiSwipeRefreshLayout>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,826评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,968评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,234评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,562评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,611评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,482评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,271评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,166评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,608评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,814评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,926评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,644评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,249评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,866评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,991评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,063评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,871评论 2 354

推荐阅读更多精彩内容