首先放上一张gif图片
本身使用的是一个网上很出名的开源框架,地址是https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh,在此声明,我只是为新手做一个简单的案例,帮助大家有效,快速的掌握这个框架,自定义下拉刷新动态图片。
首先是xml文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="yianke.example_06.MainActivity"> <in.srain.cube.views.ptr.PtrFrameLayout android:id="@+id/main_frame" android:layout_width="match_parent" android:layout_height="match_parent"> <!--头部刷新--> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/main_top_animation" android:layout_width="60dp" android:layout_height="60dp" android:layout_gravity="center"/> </FrameLayout> <android.support.v7.widget.RecyclerView android:id="@+id/main_recycler" android:layout_width="match_parent" android:layout_height="match_parent"/> </in.srain.cube.views.ptr.PtrFrameLayout></LinearLayout>其次是java代码:
import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.view.View;import android.widget.ImageView;import com.github.library.BaseRecyclerAdapter;import com.github.library.BaseViewHolder;import java.util.ArrayList;import java.util.List;import butterknife.Bind;import butterknife.ButterKnife;import in.srain.cube.views.ptr.PtrDefaultHandler;import in.srain.cube.views.ptr.PtrFrameLayout;import in.srain.cube.views.ptr.PtrHandler;public class MainActivity extends AppCompatActivity{ @Bind(R.id.main_top_animation) ImageView mMainTopAnimation; @Bind(R.id.main_recycler) RecyclerView mMainRecycler; @Bind(R.id.main_frame) PtrFrameLayout mMainFrame; PRivate LinearLayoutManager mManager; private AnimationDrawable mAnimation; private List<String> mList; private BaseRecyclerAdapter<String> mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); initViews(); } private void initViews() { initDatas(); mMainRecycler.setLayoutManager(mManager); mMainRecycler.setAdapter(mAdapter = new BaseRecyclerAdapter<String>(this, mList, R.layout.main_item) { @Override protected void convert(BaseViewHolder helper, String item) { helper.setImageResource(R.id.item_img, R.mipmap.ic_launcher); helper.setText(R.id.item_titles, item); } }); //下拉刷新 mMainFrame.setPtrHandler(new PtrHandler() { @Override public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) { return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header); } @Override public void onRefreshBegin(PtrFrameLayout frame) { mMainFrame.postDelayed(new Runnable() { @Override public void run() { mMainFrame.refreshComplete();//刷新完毕 refreshDatas(); } }, 2000); } }); } /** * 初始化数据 */ private void initDatas() { mList = new ArrayList<>(); setDatas(); mManager = new LinearLayoutManager(this); mMainTopAnimation.setImageResource(R.drawable.main_top_animation); mAnimation = (AnimationDrawable) mMainTopAnimation.getDrawable(); } /** * 设置数据 */ private void setDatas() { if (mList.size() == 0) { for (int i = 1; i < 3; i++) { mList.add("...豌豆..." + i); } } } /** * 刷新数据 */ private void refreshDatas() { int itemCount = mManager.getItemCount(); mList.clear(); if (mList.size() == 0) { for (int i = 0; i < itemCount; i++) { mList.add("我是刷新后的数据..."); } mAdapter.notifyDataSetChanged(); } }}这些都相当的基础和简单,其中需要说明的是:BaseRecyclerAdapter这个类,其实就是网上的开源库,适配adapter的使用,没什么难度。然后就是动画的制作了,不明白帧动画的实现的,可以去网上搜索一下,理解一下。我这里就不bb了。。。直接上代码
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/tgp_lol_refreshing_0" android:duration="100"/> <item android:drawable="@drawable/tgp_lol_refreshing_1" android:duration="100"/> <item android:drawable="@drawable/tgp_lol_refreshing_2" android:duration="100"/> <item android:drawable="@drawable/tgp_lol_refreshing_3" android:duration="100"/> <item android:drawable="@drawable/tgp_lol_refreshing_4" android:duration="100"/> <item android:drawable="@drawable/tgp_lol_refreshing_5" android:duration="100"/> <item android:drawable="@drawable/tgp_lol_refreshing_6" android:duration="100"/> <item android:drawable="@drawable/tgp_lol_refreshing_7" android:duration="100"/> <item android:drawable="@drawable/tgp_lol_refreshing_8" android:duration="100"/> <item android:drawable="@drawable/tgp_lol_refreshing_9" android:duration="100"/> <item android:drawable="@drawable/tgp_lol_refreshing_10" android:duration="100"/> <item android:drawable="@drawable/tgp_lol_refreshing_11" android:duration="100"/> <item android:drawable="@drawable/tgp_lol_refreshing_12" android:duration="100"/> <item android:drawable="@drawable/tgp_lol_refreshing_13" android:duration="100"/> <item android:drawable="@drawable/tgp_lol_refreshing_14" android:duration="100"/></animation-list>其中需要说明的是:android:oneshot="false"设置为false:代表动画执行完一次了,循环执行播放,设置为true,就只执行一次。需要说明的是下拉刷新的开源库中的一个接口:
//下拉刷新 mMainFrame.setPtrHandler(new PtrHandler() { @Override public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) { return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header); } @Override public void onRefreshBegin(PtrFrameLayout frame) { mMainFrame.postDelayed(new Runnable() { @Override public void run() { mMainFrame.refreshComplete();//刷新完毕 refreshDatas(); } }, 2000); } });复写的第一个方法,是判断是否可以执行下拉刷新,第二个方法:可以再这个方法中执行刷新操作;大概就这么多。
新闻热点
疑难解答