首先需要明白的是,平常用的ListView做不到类似于小说章节阅读的浏览效果,去实现刷新加载的功能。虽然GitHub上有一些开源库可以使用,但是这种东西自己如果有时间能够亲历一遍了解的更加透彻,毕竟在很多Android的APP中都有这方面的功能。话不多说,下面我们就开始上代码。
首先把头部尾部的布局弄好
headview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/head"
android:orientation="horizontal"
android:gravity="center"
android:padding="5dp">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleInverse"
android:id="@+id/progressbar"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在刷新。。。。"
android:id="@+id/update_info"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/update_time"
/>
</LinearLayout>
</LinearLayout>
footview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/foot_load"
android:padding="5dp"
android:gravity="center">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyle"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加载中。。。。"
/>
</LinearLayout>
</LinearLayout>
根据这2个界面布局我们就可以去搭建一个自己的自定义ListView,以此来实现我们想要的效果
下面我们就可以着手自定义的ListView的编写了
package com.yakir.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.example.t210_06.R;
import java.text.SimpleDateFormat;
/**
* 自定义listview实现上拉刷新下拉加载
*/
public class MyListView extends ListView implements AbsListView.OnScrollListener{
private View footview;
private View headview;
private int totaItemCounts;
private int lasstVisible;
private int fistVisiable;
private LoadListener loadListener;
private int footViewHeight;
private int headViewHeight;
private int yload;
boolean isLoading;
private TextView updateInfo;
private TextView updateTime;
private ProgressBar progressBar;
public MyListView(Context context) {
super(context);
initView(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(Context context) {
//拿到头布局xml
headview=LayoutInflater.from(context).inflate(R.layout.headview,null);
updateInfo=(TextView)headview.findViewById(R.id.update_info);
updateTime=(TextView)headview.findViewById(R.id.update_time);
progressBar=(ProgressBar)headview.findViewById(R.id.progressbar);
updateTime.setText("更新于:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
//拿到尾布局xml
footview=LayoutInflater.from(context).inflate(R.layout.footview,null);
//测量footview的高度
footview.measure(0,0);
//拿到高度
footViewHeight=footview.getMeasuredHeight();
//隐藏view
footview.setPadding(0,-footViewHeight,0,0);
headview.measure(0,0);
headViewHeight=headview.getMeasuredHeight();
headview.setPadding(0,-headViewHeight,0,0);
//设置不可见
// footview.findViewById(R.id.foot_load).setVisibility(View.GONE);
// headview.findViewById(R.id.head).setVisibility(View.GONE);
//添加到listview底部
this.addFooterView(footview);
//添加到listview头部
this.addHeaderView(headview);
//设置拉动监听
this.setOnScrollListener(this);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
yload=(int)ev.getY();
break;
case MotionEvent.ACTION_MOVE:
int moveY=(int)ev.getY();
int paddingY=-headViewHeight+(moveY-yload)/2;
if (paddingY<0){
updateInfo.setText("下拉刷新。。。");
progressBar.setVisibility(View.GONE);
}
if (paddingY>0){
updateInfo.setText("松开刷新。。。");
progressBar.setVisibility(View.GONE);
}
headview.setPadding(0,paddingY,0,0);
break;
}
return super.onTouchEvent(ev);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (totaItemCounts==lasstVisible&&scrollState==SCROLL_STATE_IDLE) {
if (!isLoading) {
isLoading=true;
footview.setPadding(0,0,0,0);
//加载数据
loadListener.onLoad();
}
}
if (fistVisiable==0){
headview.setPadding(0,0,0,0);
updateInfo.setText("正在刷新。。。");
progressBar.setVisibility(View.VISIBLE);
loadListener.pullLoad();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
this.fistVisiable=firstVisibleItem;
this.lasstVisible=firstVisibleItem+visibleItemCount;
this.totaItemCounts=totalItemCount;
}
//加载完成
public void loadComplete(){
isLoading=false;
footview.setPadding(0,-footViewHeight,0,0);
headview.setPadding(0,-headViewHeight,0,0);
}
public void setInterface(LoadListener loadListener){
this.loadListener=loadListener;
}
//接口回调
public interface LoadListener{
void onLoad();
void pullLoad();
}
}
然后在外面的主界面布局里调用这个我们自己写的插件就行了
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.yakir.view.MyListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_view">
</com.yakir.view.MyListView>
</RelativeLayout>
MainActivity.java
package com.example.t210_06;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import com.yakir.view.MyListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements MyListView.LoadListener{
private MyListView listview;
private List<Integer>list=new ArrayList<>();
private ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i=1;i<20;i++){
list.add(i);
}
initView();
adapter=new ArrayAdapter<>(this,android.R.layout.simple_expandable_list_item_1,list);
listview.setAdapter(adapter);
}
private void initView() {
listview=(MyListView) findViewById(R.id.list_view);
listview.setInterface(this);
}
@Override
public void onLoad() {
//设置三秒延迟模仿延时获取数据
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//加载数据
for (int j=1;j<11;j++){
list.add(j);
}
//更新 数据
adapter.notifyDataSetChanged();
//加载完毕
listview.loadComplete();
}
},3000);
}
@Override
public void pullLoad() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
list.clear();
for (int i=1;i<20;i++){
list.add(i+1);
}
adapter.notifyDataSetChanged();
listview.loadComplete();
}
},2000);
}
}
这1点必须注意:
在initView(context)方法中通过LayoutInfalter.from(context).from(你的xml,null);拿到xml。返回一个View对象,ListView中有一个setfootView()方法,使用他可以设置View到ListView的底部,当然做了这两步是完全不够的,我们还需要对这个View做一个交互的操作,当用户滑到底部时这个View显示,数据加载完毕之后这个View消失。这里就能通过设置底部视图的焦点位置去,使其隐藏,当下拉的时候触发事件就可以达到我们的效果。