更多炫酷效果在官网:
一:ListView分页加载
1.手动加载
① View view=LayoutInflater.from(this).inflate(R.layout.foot,null); 加载更多控件
② addHeaderView(); 加到ListView的头,底部
addFooterView();
③ 点击按钮加载数据,适配器刷新
/**
* 手动加载
*/
View inflate = LayoutInflater.from(this).inflate(R.layout.layout_bottom, null);
lv.addFooterView(inflate);
Button btn = inflate.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
++pno;//原来访问的页数上加1
initData();
}
});
2.自动加载
① OnScrollListener() 设置滑动监听事假
② 重写onScroll(),onScrollStateChanged()
③ 在onScroll()中判断firstVisibleItem+visibleItemCount==totalItemCount&&totalItemCount&&total>0,就是最后一个item
④ 在OnScrollStateChanged()中判断是否是最后一条并且屏幕是静止状态
scrollState==SCROLL_STATE_IDLE 手指未接触屏幕,且屏幕页面保持静止
scrollState==SCROLL_STATE_TOUCH_SCROLL 手指按住屏幕滚动(未脱离屏幕)
scrollState==SCROLL_STATE_FLING 手指离开屏幕,页面依然保持滚动
/**
* 自动加载:
* ListView设置滑动监听
*/
lv.setOnScrollListener(this);
}
private boolean flag = false;//是否为最后一条数据,false 不是最后一条 true 是最后一条
/**
* 滑动状态改变
* scrollState:当前的状态
* AbsListView.OnScrollListener.SCROLL_STATE_IDLE 静止状态
*
*/
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//flag为最后一条数据 && 静止状态
if (flag && scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE){
++pno; //分页加载 pno加1
initData(); //加载更多
}
}
/**
* 滑动方法
* @param view
* @param firstVisibleItem 当前显示的第一个条目位置
* @param visibleItemCount 当前页面可显示的条目个数
* @param totalItemCount 总共条目数
*/
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int
totalItemCount) {
//判断是否为最后一条数据:第一个条目 + 可显示条目数 = 总共条目数 && 总共条目数 > 0
if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount >0){
flag = true; //符合最后一条数据
}
}
}
3.放图片错乱
原因:在于convertView的重用
解决:viewHolder.img.setTag(student.getImg()); 给img设置Tag(路径)
if(img.getTag()!=null&&img.getTag().equals(str)){img.setImageBitmap(bitmap);} 当img.setImageBitmap之前判断设置的路径和Tag的路径是否一致
XRecyclerView
mLv.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
a=0;
list.clear();
initDada();
mLv.refreshComplete();
}
@Override
public void onLoadMore() {
++a;
initDada();
mLv.loadMoreComplete();
}
});
SmartRefreshLayout上下加载时会有不同的特效
依赖
//recyclerview
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
//SmartRefreshLayout
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.4-7'
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.4-7'
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.freshdemo.MainActivity"
android:orientation="vertical">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srlAccentColor="#00000000"
app:srlPrimaryColor="#00000000"
app:srlEnablePreviewInEditMode="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
这是基本使用其中:
app:srlAccentColor="#00000000"//设置Header主题颜色
app:srlPrimaryColor="#00000000"//设置Footer主题颜色
app:srlEnablePreviewInEditMode="true"//开启和关闭预览功能
代码实现上拉刷新下拉加载
private RecyclerView mRecyclerView;
private RefreshLayout mRefreshLayout;
//初始化
mRecyclerView=findViewById(R.id.rv);
mRefreshLayout = findViewById(R.id.refreshLayout);
//刷新
mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
mData.clear();
mNameAdapter.notifyDataSetChanged();
refreshlayout.finishRefresh();
}
});
//加载更多
mRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
@Override
public void onLoadmore(RefreshLayout refreshlayout) {
for(int i=0;i<30;i++){
mData.add("小明"+i);
}
mNameAdapter.notifyDataSetChanged();
refreshlayout.finishLoadmore();
}
});
image
2.比较炫酷的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.freshdemo.MainActivity"
android:orientation="vertical">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srlAccentColor="#00000000"
app:srlPrimaryColor="#00000000"
app:srlEnablePreviewInEditMode="true">
<com.scwang.smartrefresh.header.PhoenixHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.scwang.smartrefresh.layout.footer.BallPulseFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
image
吃面条的
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
app:srlEnableFooterTranslationContent="true"
app:srlPrimaryColor="@color/colorTextContent"
app:srlEnableAutoLoadMore="false">
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srlPrimaryColor="@color/colorPrimary"
app:srlAccentColor="@android:color/white"
app:srlDrawableProgress="@drawable/animation_loading_frame"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
tools:listitem="@android:layout/simple_list_item_2"/>
<com.scwang.smartrefresh.header.MaterialHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
网友评论