ListView 上拉加载更多
ListView 上拉加载更多
首先来个效果图
界面布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_list_view_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.it.controlstudy.ListViewRefreshActivity">
<ListView
android:id="@+id/listviewrefresh_lv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
ListView Item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/listviewrefrsh_item_iv"
android:background="@drawable/img2"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:id="@+id/listviewrefrsh_item_tv"
android:text="This is a computer!"
android:layout_marginTop="30dp"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Load More 视图
<?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="horizontal">
<ProgressBar
android:layout_marginLeft="60dp"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="Load more ..."
android:textSize="20dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Activity
public class ListViewRefreshActivity extends AppCompatActivity {
public ListView listView;
public View loadmoreView;
public int last_index; //当前页面最下方ListView Item在adapter中的位置
public int total_index; //所有ListView的Item总个数
public List<String> list = new ArrayList<>();// 数据集合
public MyBaseAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_refresh);
listView = (ListView) findViewById(R.id.listviewrefresh_lv);
// 获得自定义的刷新视图
loadmoreView= View.inflate(ListViewRefreshActivity.this,R.layout.listviewrefresh_loadmore_layout,null);
// 设置刷新视图默认情况下是不可见的,并且不占空间
loadmoreView.setVisibility(View.GONE);
// 添加刷新视图到ListView中
listView.addFooterView(loadmoreView,null,false);
//初始化集合数据
for (int i = 1; i <= 10; i++) {
list.add("I am the "+i+" computer!");
}
//为ListView设置adapter
adapter = new MyBaseAdapter(this, list);
listView.setAdapter(adapter);
//设置ListView的监听事件
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
/**
* 监听着ListView的滑动状态改变。
* 一共有三种状态
* SCROLL_STATE_TOUCH_SCROLL:手指正拖着ListView滑动
* SCROLL_STATE_FLING:ListView自由滑动
* SCROLL_STATE_IDLE:ListView滑动后静止
* */
@Override
public void onScrollStateChanged(AbsListView absListView, int scrollState) {
//ListView 处于最下方,并且滑动停止
if(last_index == total_index && (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE))
{
//设置刷新界面可见
loadmoreView.setVisibility(View.VISIBLE);
//加载更多数据
onLoad();
}
}
/**
* firstVisibleItem: 屏幕中显示的第一个Item数据在adapter中的位置
* visibleItemCount:屏幕中显示Item的个数
* totalItemCount: 在adapter中的总Item数
* */
@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//计算最后一个Item位置,最后根据这个值判断是否到达ListView最下方
last_index = firstVisibleItem+visibleItemCount;
total_index = totalItemCount;
System.out.println("last: "+last_index);
System.out.println("total: "+total_index);
}
});
}
/**
* 加载更多数据
*/
public void onLoad()
{
new Thread(new Runnable() {
@Override
public void run() {
try {
//模拟耗时操作
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//向数据集中添加数据
for (int i = 0; i < 10; i++) {
list.add("I am the "+(list.size()+1)+" computer!");
}
runOnUiThread(new Runnable() {
@Override
public void run() {
//刷新ListView
adapter.updateView(list);
//加载数据结束后使刷新界面不可见
loadmoreView.setVisibility(View.GONE);
}
});
}
}).start();
}
class MyBaseAdapter extends BaseAdapter{
public List<String> list;
public LayoutInflater inflater;
public MyBaseAdapter(Context context, List<String> list) {
this.list=list;
this.inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
public void updateView(List<String> nowList){
this.list=nowList;
this.notifyDataSetChanged(); //强制动态刷新数据进而调用getView方法
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHoder;
if(view ==null){
viewHoder=new ViewHolder();
view=inflater.inflate(R.layout.listviewrefresh_item_layout,null);
viewHoder.imageView=(ImageView) view.findViewById(R.id.listviewrefrsh_item_iv);
viewHoder.textView=(TextView) view.findViewById(R.id.listviewrefrsh_item_tv);
view.setTag(viewHoder);
}else {
viewHoder=(ViewHolder)view.getTag();
}
viewHoder.textView.setText(list.get(i));
return view;
}
class ViewHolder{
ImageView imageView;
TextView textView;
}
}
}
文章部分内容摘自网络和图书,如有侵权,请联系我。