无标题文章
2017-09-28 本文已影响0人
闹闹也会有脾气
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>