1. 导入PullToRefresh
1.1 修改library的build.gradle中的sdk版本
//修改前
compileSdkVersion 16
buildToolsVersion “27.0.3”
defaultConfig {
minSdkVersion 4
targetSdkVersion 4
}
//修改后
compileSdkVersion 27
buildToolsVersion "27.0.3"
defaultConfig {
minSdkVersion 15
targetSdkVersion 27
}
1.2 Clear Project后会报如下错误,修改源代码:“PullToRefreshWebView”
错误: 找不到符号
符号: 方法 floor(float)
位置: 类 FloatMath
FloatMath.floor -> Math.floor
1.3 选中自己的工程,例如:“app”然后鼠标右键“open Moduel settings”导入库
1.4 修改layout布局文件,添加PullToRefresh控件替换ListView即可
2. 第三方控件:上拉加载、下拉刷新控件
2.1 导入第三方插件库
Android-PullToRefresh-master.zip
2.2 在布局文件中使用第三方插件
com.handmark.pulltorefresh.library.PullToRefreshListView
2.3 自定义适配器(BaseAdapter)提供数据
2.4 异步任务查询数据(AsyncTask)
2.4.1 AsyncTask定义了三种泛型类型 Params,Progress和Result。
Params 启动任务执行的输入参数,比如HTTP请求的URL。
Progress 后台任务执行的百分比。
Result 后台执行任务最终返回的结果,比如String
2.4.2 异步加载数据最少要重写以下这两个方法
doInBackground(Params…) 后台执行,比较耗时的操作都可以放在这里
onPostExecute(Result) 相当于Handler 处理UI的方式,在这里面可以使用在doInBackground 得到的结果处理操作UI
–注:此方法中再通知适配器和控件
myBaseAdapter.notifyDataSetChanged();// 通知适配器数据已改变
plv_main_plv1.onRefreshComplete();// 通知控件数据已经加载完毕
2.5 给PullToRefreshListView设置相关属性
plv_main_1.setMode(Mode.BOTH);// 设置刷新模式
Mode.BOTH:同时支持上拉下拉
Mode.PULL_FROM_START:只支持下拉Pulling Down
Mode.PULL_FROM_END:只支持上拉Pulling Up
plv_main_1.getLoadingLayoutProxy().setPullLabel("上拉刷新...");// 刚下拉时,显示的提
plv_main_1.getLoadingLayoutProxy().setRefreshingLabel("正在载入...");// 刷新时
plv_main_1.getLoadingLayoutProxy().setReleaseLabel("放开刷新...");// 下来达到一定距离时,显示的提示
github、码云
核心:
1、新建Android的project
2、将第三方工程导入到project中
3、由于现有的开发module的sdk与导入的库的sdk版本不一样,所以需要修改
4、在开发的module中引入库依赖
5、启动module报错的原因,jdk高版本移除了些东西。
6、改变listview为PullToRefreshListView
7、加载数据源
8、给PullToRefreshListView设置相关属性,给下拉组件添加监听
9、回调函数中,通知适配器数据加载完毕
在MainActivity里
package com.example.a0918_a09;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Book> data;
private MyBaseAdapter adapter;
private PullToRefreshListView lv_main_bookList;
private int page=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_main_bookList=findViewById(R.id.lv_main_bookList);
getViews();
this.data=new BookDao().list(page);
adapter=new MyBaseAdapter((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE));
lv_main_bookList.setAdapter(adapter);
lv_main_bookList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
new MyTask().execute();
}
});
}
class MyTask extends AsyncTask{
@Override
protected Object doInBackground(Object[] objects) {//往Java端请求数据
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
page++;
data.addAll(new BookDao().list(page));
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
adapter.notifyDataSetChanged();// 通知适配器数据已改变
lv_main_bookList.onRefreshComplete();// 通知控件数据已经加载完毕
}
}
private void getViews() {
lv_main_bookList.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
lv_main_bookList.getLoadingLayoutProxy().setPullLabel("上拉刷新...");// 刚下拉时,显示的提
lv_main_bookList.getLoadingLayoutProxy().setRefreshingLabel("正在载入...");// 刷新时
lv_main_bookList.getLoadingLayoutProxy().setReleaseLabel("放开刷新...");// 下来达到一定距离时,显示的提示
}
//自定义适配器先要获取到解析器才能解析资源文件
public class MyBaseAdapter extends BaseAdapter{
public class ViewHolder{
ImageView iv_listviewitem_image;
TextView tv_listviewitme_title;
TextView tv_listviewitme_author;
TextView tv_listviewitme_price;
TextView tv_listviewitme_publish;
TextView tv_listviewitme_remark;
}
//布局解析器,用来把layout布局文件解析成一个View对象,不可以直接new,需要使用系统服务获取
private LayoutInflater inflater;
public MyBaseAdapter(LayoutInflater inflater) {
this.inflater = inflater;
}
//数据的长度
@Override
public int getCount() {
return data.size();
}
//当前获取的项
@Override
public Object getItem(int i) {
return data.get(i);
}
//当前解析的第几条数据的索引
@Override
public long getItemId(int i) {
return i;
}
//用解析器把里面的数据,资源解析出来,把数据填充view里才能展示出来
//view是指屏幕遮挡的视图
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v=view;
if(v==null){
ViewHolder vh=new ViewHolder();
// Log.i("test","position:"+i);
v=inflater.inflate(R.layout.listview_item,null);
vh.iv_listviewitem_image= v.findViewById(R.id.iv_listviewitem_image);
vh.tv_listviewitme_title= v.findViewById(R.id.tv_listviewitme_title);
vh.tv_listviewitme_author= v.findViewById(R.id.tv_listviewitme_author);
vh.tv_listviewitme_price = v.findViewById(R.id.tv_listviewitme_price);
vh.tv_listviewitme_publish = v.findViewById(R.id.tv_listviewitme_publish);
vh.tv_listviewitme_remark = v.findViewById(R.id.tv_listviewitme_remark);
v.setTag(vh);//保存控件
}
ViewHolder vh = (ViewHolder) v.getTag();//传值
Book book = data.get(i);
vh.iv_listviewitem_image.setImageResource(book.getImage());
vh.tv_listviewitme_title.setText(book.getTitle());
vh.tv_listviewitme_author.setText(book.getAuthor());
vh.tv_listviewitme_price.setText(book.getPrice()+"");
vh.tv_listviewitme_publish.setText(book.getPublish());
vh.tv_listviewitme_remark.setText(book.getRemark());
return v;
}
}
}
在activity_main.xml里
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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=".MainActivity">
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/lv_main_bookList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</com.handmark.pulltorefresh.library.PullToRefreshListView>
</android.support.constraint.ConstraintLayout>
在listview_item.xml里
<?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="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_listviewitem_image"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:padding="10dp"
android:scaleType="fitXY"
android:src="@drawable/book1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/tv_listviewitme_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="狂人摄影日记"
android:textColor="@color/red"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="书本作者:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_listviewitme_author"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="阿刘" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="书本价格:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_listviewitme_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="$123"
android:textColor="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text=" 出版社:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_listviewitme_publish"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="电子出版社"
android:textColor="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="书本简介:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_listviewitme_remark"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ellipsize="end"
android:maxLines="2"
android:text="很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天"
android:textColor="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/bt_listviewitme_btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:src="@drawable/btn_shopping" />
<ImageButton
android:id="@+id/bt_listviewitme_btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:src="@drawable/btn_accounts" />
</LinearLayout>
</LinearLayout>
</LinearLayout>