【补充】swipeRefreshLayout布局是新版本的中支持的,所以如果是老版本,需要先升级支持库,方法如下:
打开build.gradle(app)文件夹,在dependencies{}中写上这句话:
implementation 'com.android.support:appcompat-v7:27.1.1'
一、swipeRefreshLayout中主要方法:
1.isRefreshing()
判断当前的状态是否是刷新状态。
2.setColorSchemeResources(int... colorResIds)
设置下拉进度条的颜色主题,参数为可变参数,并且是资源id,可以设置多种不同的颜色,每转一圈就显示一种颜色。
3.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener)
设置监听,需要重写onRefresh()方法,顶部下拉时会调用这个方法,在里面实现请求数据的逻辑,设置下拉进度条消失等等。
4.setProgressBackgroundColorSchemeResource(int colorRes)
设置下拉进度条的背景颜色,默认白色。
5.setRefreshing(boolean refreshing)
设置刷新状态,true表示正在刷新,false表示取消刷新。
二、如何使用
1.xml布局文件中:
xml文件中要加入swipeRefreshLayout布局,并且布局中要包含一个可以滑动的子控件,如listView
<?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">
<!-- 官方自带下拉刷新布局。其中要包含listview类的子控件,并且只能有一个-->
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.constraint.ConstraintLayout>
2.用做listview内容的子布局
listview中,每一个list都相当于一个小页面,下面的布局就是在每个小页面中的布局
<?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">
<ImageView
android:id="@+id/fruit_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/cherry_pic"/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="30dp"
android:text="example text"
/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/fruit_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="30dp"
android:text="example text"
/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/fruit_saleprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="@color/colorAccent"
android:layout_marginLeft="30dp"
android:text="example text"
/>
</TableRow>
</TableLayout>
</LinearLayout>
2.在对应的avtivity类中
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList = new ArrayList<Fruit>();
fruitAdapter adapter;
SwipeRefreshLayout refresh=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* ListView lv=findViewById(R.id.lv);
lv.setAdapter(new MylistAdapter());*/
initFruits();
adapter = new fruitAdapter(MainActivity.this, R.layout.fruit, fruitList);
ListView listView = findViewById(R.id.lv);
listView.setAdapter(adapter);
//下面写swipeRefresh
refresh=findViewById(R.id.refresh);
refresh.setProgressBackgroundColorSchemeColor(getResources().getColor(R.color.colorAccent));
refresh.setColorSchemeColors(getResources().getColor(R.color.深天蓝));
refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Toast.makeText(MainActivity.this,"正在刷新",Toast.LENGTH_SHORT).show();
myThread myThread=new myThread();
myThread.start();
}
});
}
//线程类,用于模拟刷新时需要耗时
private class myThread extends Thread{
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
Fruit apple = new Fruit("新数据-----", R.drawable.apple_pic, "$100", "$50");
fruitList.add(apple);
/*Fruit banana = new Fruit("香蕉--", R.drawable.banana_pic, "¥200", "¥150");
fruitList.add(banana);
Fruit orange = new Fruit("桔子--", R.drawable.orange_pic, "$1000.00", "$800");
fruitList.add(orange);
Fruit watermelon = new Fruit("西瓜--", R.drawable.watermelon_pic, "213", "130");
fruitList.add(watermelon);
Fruit pear = new Fruit("梨子--", R.drawable.pear_pic, "111", "67");
fruitList.add(pear);*/
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
//notifyDataSetChanged()方法是用于通知adapter有新的数据。并且必须要在UI线程中执行,所以要这样写
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
refresh.setRefreshing(false);
}
});
}
}
}
//初始化数据,造假数据的方法
private void initFruits() {
// for (int i = 0; i < 14; i++) {
for (int i = 0; i < 1; i++) {
Fruit apple = new Fruit("苹果", R.drawable.apple_pic, "$100", "$50");
fruitList.add(apple);
Fruit banana = new Fruit("香蕉", R.drawable.banana_pic, "¥200", "¥150");
fruitList.add(banana);
Fruit orange = new Fruit("桔子", R.drawable.orange_pic, "$1000.00", "$800");
fruitList.add(orange);
Fruit watermelon = new Fruit("西瓜", R.drawable.watermelon_pic, "213", "130");
fruitList.add(watermelon);
Fruit pear = new Fruit("梨子", R.drawable.pear_pic, "111", "67");
fruitList.add(pear);
Fruit grape = new Fruit("葡萄", R.drawable.grape_pic, "222", "200");
fruitList.add(grape);
Fruit pineapple = new Fruit("菠萝", R.drawable.pineapple_pic, "333", "200");
fruitList.add(pineapple);
Fruit strawberry = new Fruit("草莓", R.drawable.strawberry_pic, "55.5", "40");
fruitList.add(strawberry);
Fruit cherry = new Fruit("樱桃", R.drawable.cherry_pic, "88", "60");
fruitList.add(cherry);
Fruit mango = new Fruit("芒果", R.drawable.mango_pic, "567", "500");
fruitList.add(mango);
}
}
//自定义适配器,继承自arrayAdapter(可以单独写一个类,也可以写成像这样的内部类)---------------------------------------------------------------------------------
private class fruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
public fruitAdapter(@NonNull Context context, int textViewResourceId, @NonNull List<Fruit> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Fruit fruit = getItem(position);
View view;
ViewHolder viewHolder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
viewHolder = new ViewHolder();
viewHolder.fruitimage = view.findViewById(R.id.fruit_img);
viewHolder.fruitname = view.findViewById(R.id.fruit_name);
viewHolder.fruitprice = view.findViewById(R.id.fruit_price);
viewHolder.fruitsaleprice = view.findViewById(R.id.fruit_saleprice);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
// ImageView fruitimage=view.findViewById(R.id.fruit_img);
// TextView fruitname=view.findViewById(R.id.fruit_name);
//TextView fruitprice=view.findViewById(R.id.fruit_price);
//TextView fruitsaleprice=view.findViewById(R.id.fruit_saleprice);
viewHolder.fruitimage.setImageResource(fruit.getImgId());
viewHolder.fruitname.setText(fruit.getName());
viewHolder.fruitprice.setText(fruit.getPrice());
viewHolder.fruitprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//给文字增加删除线
viewHolder.fruitsaleprice.setText(fruit.getSaleprice());
return view;
}
class ViewHolder {
ImageView fruitimage;
TextView fruitname;
TextView fruitprice;
TextView fruitsaleprice;
}
}