首先放上一張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); } });復寫的第一個方法,是判斷是否可以執行下拉刷新,第二個方法:可以再這個方法中執行刷新操作;大概就這么多。
新聞熱點
疑難解答