1、今天做列表时,需求上需要做一个下拉刷新,下拉加载更多的功能,就上网找了一些例子,由于我原来用的就是andbase框架,就还是用它原来写的了。其中同事给我推荐另一个框架BGARefreshLayout-Android,下载地址https://github.com/bingoogolapple/BGARefreshLayout-Android,看着感觉也挺好的,但是代码写的可能太好了,不太容易读懂,就还是用andbase自带的AbPullToRefreshView方法。
2、先上图,其中上面的滚动导航可以根据列表一起滚动,实现方式如下:原来就是在listview上加一个表头。
LayoutInflater mInflater = LayoutInflater.from(this);
headerView = mInflater.inflate(R.layout.building_top_advert,null);
main_building_listivew.addHeaderView(headerView);
4、先写一个xml文件:其中AbPullToRefreshView下的listview不能再写在Linearout中。
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_screen" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white"> <!--显示的内容--> <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <include layout="@layout/top" android:visibility="gone" android:id="@+id/top_isvisible" /> <!--滑动功能--> <com.ab.view.pullview.AbPullToRefreshView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mPullRefreshView" android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="fill_parent" android:background="@color/white"> <ListView android:id="@+id/main_building_listivew" android:layout_width="match_parent" android:layout_height="match_parent" android:dividerHeight="1dp" android:listSelector="#00000000" > </ListView> </com.ab.view.pullview.AbPullToRefreshView> </LinearLayout> <LinearLayout android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="end" android:orientation="vertical" /> </android.support.v4.widget.DrawerLayout>
5、java代码如下:
public class MainBuildingActivity extends MainActivity implements OnFooterLoadListener
private AbPullToRefreshView mPullRefreshView; //当前页 private int currentPage = 1; //总页数 private int total = 100; //每页显示的条数 private int pageSize = 15; //上方显示循环广告的view,headerView private View headerView;
mPullRefreshView=findViewById(R.id.mPullRefreshView);
/*数据刷新的方法*/ private void refreshData(){ //设置不可以进行下拉刷新 mPullRefreshView.setPullRefreshEnable(false); // 设置监听器 mPullRefreshView.setOnFooterLoadListener(this); // 设置进度条的样式 mPullRefreshView.getFooterView().setFooterProgressBarDrawable( this.getResources().getDrawable(R.drawable.progress_circular)); }
@Override public void onFooterLoad(AbPullToRefreshView abPullToRefreshView) { loadMoreTask(); } /*下拉列表时加载更多内容*/ public void loadMoreTask() { AbTaskMy mAbTask = AbTaskMy.newInstance(); final AbTaskItem item = new AbTaskItem(); item.setListener(new AbTaskListListener() { @Override //将数据加载到显示的list集合中 public void update(List<?> paramList) { List<String[]> newList = (List<String[]>) paramList; if (newList != null && newList.size() > 0) { listBuilding.addAll(newList); adapter.notifyDataSetChanged(); newList.clear(); } mPullRefreshView.onFooterLoadFinish(); } //获得新的数据 @Override public List<?> getList() { List<String[]> newList = null; try { currentPage++; Thread.sleep(1000); newList = new ArrayList<>(); Map<String, Object> map = null; for(int i=0;i<pageSize;i++){ String[] s=new String[2]; s[0]="http://61.161.221.254:1234/upload/project/20180109.jpg"; s[1]="楼盘名称__"+(currentPage*pageSize+i); newList.add(s); } } catch (Exception e) { currentPage--; newList.clear(); } return newList; }; }); mAbTask.execute(item); }
6、其中下拉加载时有一个旋转动画效果:具体含义可以去网上查找
<?xml version="1.0" encoding="utf-8"?> <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="200" android:fromDegrees="0.0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50.0%" android:pivotY="50.0%" android:repeatMode="restart" android:toDegrees="360.0" android:drawable="@drawable/progress_loading2"> </animated-rotate>duration:动画开始到结束的执行时间
fromDegrees: 设置动画开始时的角度
toDegrees:设置动画结束时的旋转角度
pivotX : 设置动画相对于控件的 x 坐标的位置
pivotY:设置动画相对于控件的 y 坐标的位置
repeatMode:
动画的进度使用 Interpolator 控制。Interpolator 定义了动画的变化速度,可以实现匀速、正加速、负加速、无规则变加速等。Interpolator 是基类,封装了所有 Interpolator 的共同方法,它只有一个方法,即 getInterpolation (float input),该方法 maps a point on the timeline to a multiplier to be applied to the transformations of an animation。Android 提供了几个 Interpolator 子类,实现了不同的速度曲线,如下:
AccelerateDecelerateInterpolator 在动画开始与介绍的地方速率改变比较慢,在中间的时侯加速
AccelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始加速
CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始减速
LinearInterpolator 在动画的以均匀的速率改变
对于 LinearInterpolator ,变化率是个常数,即 f (x) = x.
public float getInterpolation(float input) {
return input;
}