首先放上一張gif圖片
本身使用的是一個(gè)網(wǎng)上很出名的開源框架,地址是https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh,在此聲明,我只是為新手做一個(gè)簡(jiǎn)單的案例,幫助大家有效,快速的掌握這個(gè)框架,自定義下拉刷新動(dòng)態(tài)圖片。
首先是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); } }); } /** * 初始化數(shù)據(jù) */ private void initDatas() { mList = new ArrayList<>(); setDatas(); mManager = new LinearLayoutManager(this); mMainTopAnimation.setImageResource(R.drawable.main_top_animation); mAnimation = (AnimationDrawable) mMainTopAnimation.getDrawable(); } /** * 設(shè)置數(shù)據(jù) */ private void setDatas() { if (mList.size() == 0) { for (int i = 1; i < 3; i++) { mList.add("...豌豆..." + i); } } } /** * 刷新數(shù)據(jù) */ private void refreshDatas() { int itemCount = mManager.getItemCount(); mList.clear(); if (mList.size() == 0) { for (int i = 0; i < itemCount; i++) { mList.add("我是刷新后的數(shù)據(jù)..."); } mAdapter.notifyDataSetChanged(); } }}這些都相當(dāng)?shù)幕A(chǔ)和簡(jiǎn)單,其中需要說(shuō)明的是:BaseRecyclerAdapter這個(gè)類,其實(shí)就是網(wǎng)上的開源庫(kù),適配adapter的使用,沒什么難度。然后就是動(dòng)畫的制作了,不明白幀動(dòng)畫的實(shí)現(xiàn)的,可以去網(wǎng)上搜索一下,理解一下。我這里就不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>其中需要說(shuō)明的是:android:oneshot="false"設(shè)置為false:代表動(dòng)畫執(zhí)行完一次了,循環(huán)執(zhí)行播放,設(shè)置為true,就只執(zhí)行一次。需要說(shuō)明的是下拉刷新的開源庫(kù)中的一個(gè)接口:
//下拉刷新 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); } });復(fù)寫的第一個(gè)方法,是判斷是否可以執(zhí)行下拉刷新,第二個(gè)方法:可以再這個(gè)方法中執(zhí)行刷新操作;大概就這么多。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注