UltimateRecyclerView的使用方法詳解

Selena1568 8年前發布 | 29K 次閱讀 Android開發 移動開發

來自: http://blog.csdn.net/dzc__ze/article/details/50570522


最近在用很多第三方庫的時候,發現有一些附帶的demo寫的不是很全面或者例子的代碼太多,注釋太少,要想使用還要去看下源碼什么的(。。。用第三方開源庫不就是想節省時間嘛),所以決定每周兩到三篇,寫一些比較熱門的庫的使用方法,讓使用的人能快速上手(對我而言,要是發現庫提供的功能有時不滿足需求,可能會擴展下功能什么的)。不廢話,以后都是直接貼代碼,代碼中寫滿注釋,這樣看起來的時候方便,我也省事。
(嫌麻煩的直接下拉到第5)

效果圖

效果圖
1.這是使用到的布局文件,很簡單,看成時只有一個UltimateRecyclerView就行 activity_recycler_view.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="github.dzc.apptemplate.recyclerview.RecyclerViewActivity"> <android.support.design.widget.AppBarLayout android:id="@+id/action_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" > <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="RecyclerViewActivity" android:gravity="center" android:textColor="@android:color/white"/> </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout> <com.marshalchen.ultimaterecyclerview.UltimateRecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/action_bar" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="github.dzc.apptemplate.recyclerview.RecyclerViewActivity" ></com.marshalchen.ultimaterecyclerview.UltimateRecyclerView> </RelativeLayout> 

2.頭部布局 只有一張圖片 head_view.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/img" android:scaleType="centerCrop">
</ImageView>

3.item的布局 string_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="

</TextView></pre>

4.ViewHolder

package github.dzc.apptemplate.recyclerview.viewholder;

import android.view.View; import android.widget.TextView;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;

/* Created by dzc on 16/1/22. */ public class StringViewHolder extends UltimateRecyclerviewViewHolder{ public TextView tv; public StringViewHolder(View itemView,boolean isItem) { super(itemView); if(isItem){ tv = (TextView) itemView; } } }</pre>

5.重點來了,所有注釋都在這里

 package github.dzc.apptemplate.recyclerview; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.support.design.widget.FloatingActionButton; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.support.v7.widget.helper.ItemTouchHelper; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.SimpleAdapter; import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView; import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter; import com.marshalchen.ultimaterecyclerview.itemTouchHelper.SimpleItemTouchHelperCallback; import com.marshalchen.ultimaterecyclerview.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration; import java.util.List; import butterknife.Bind; import butterknife.ButterKnife; import github.dzc.apptemplate.BaseActivity; import github.dzc.apptemplate.R; import github.dzc.apptemplate.recyclerview.viewholder.StringViewHolder; public class RecyclerViewActivity extends BaseActivity { @Bind(R.id.recycler_view) UltimateRecyclerView recyclerView; View headerView; private StringAdapter adapter; private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycler_view); ButterKnife.bind(this); handler = new Handler(); recyclerView.setLayoutManager(new LinearLayoutManager(this)); headerView = LayoutInflater.from(this).inflate(R.layout.head_view,null); adapter = new StringAdapter(data); recyclerView.setAdapter(adapter); //為每個item添加頭部的布局 這里運用到的其實就是RecyclerView.ItemDecoration //沒錯 可能一般都是用這個來實現item之間的分隔線的 但是線也是一個view,但這個view夠大的時候,就是一個頭部了 StickyRecyclerHeadersDecoration stickyRecyclerHeadersDecoration = new StickyRecyclerHeadersDecoration(adapter); recyclerView.addItemDecoration(stickyRecyclerHeadersDecoration); ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter){ //這個方法還有別的方法可以重載 可以控制如滑動刪除等功能 @Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;//控制拖動的方向 這里設置了智能上下拖動交換位置 final int swipeFlags = ItemTouchHelper.LEFT ;//控制滑動刪除的方向 這里設置了只能左滑刪除 // final int swipeFlags = ItemTouchHelper.LEFT |ItemTouchHelper.RIGHT;//左右滑刪除 return makeMovementFlags(dragFlags, swipeFlags); } @Override public boolean isItemViewSwipeEnabled() { return super.isItemViewSwipeEnabled();//這里控制開啟或關閉item是否可以滑動刪除的功能 } @Override public boolean isLongPressDragEnabled() { return super.isLongPressDragEnabled();//控制長按拖動功能 } }; final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback); itemTouchHelper.attachToRecyclerView(recyclerView.mRecyclerView); //設置頭部一定要在setAdapter后面,因為這個操作會調用adapter的方法來顯示頭部,如果adapter為null,則出錯 recyclerView.setParallaxHeader(headerView); recyclerView.enableDefaultSwipeRefresh(true);//開啟下拉刷新 recyclerView.enableLoadmore();//開啟上拉加載更多 recyclerView.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { handler.postDelayed(new Runnable() { @Override public void run() { recyclerView.setRefreshing(false); } },2000); } }); } class StringAdapter extends UltimateViewAdapter<StringViewHolder>{ private List<String> stringList; public StringAdapter(List<String> stringList) { this.stringList = stringList; } @Override public StringViewHolder getViewHolder(View view) { return new StringViewHolder(view,false); //這個getViewHolder方法在內部實現中只有在獲取頭部、加載更多、下拉刷新的時候會調用 //直接設置itemView為GONE,所以不需要初始化什么的,只要返回的是個ViewHolder就行 } @Override public StringViewHolder onCreateViewHolder(ViewGroup parent) { View view = LayoutInflater.from(RecyclerViewActivity.this).inflate(R.layout.string_item,null); return new StringViewHolder(view,true); } @Override public int getAdapterItemCount() { return stringList==null?0:stringList.size(); //這里返回的是你的item的個數 不包括頭部和加載view } @Override public long generateHeaderId(int position) { // if (getItem(position).length() > 0) // return getItem(position).charAt(0); // else return -1; if(customHeaderView!=null){ position-=1; } String s = position+""; return s.charAt(0); //為每一項item生成頭部的View,如果返回-1,則不生成,假如多個連續的item返回同一個id, //則只會生成一個頭部View //這里提取position的第一個數作為id //1 10 11 12 14等返回的id是一樣的 為1 } @Override public void onBindViewHolder(StringViewHolder holder, int position) { //一定要加這個判斷 因為UltimateRecyclerView本身有加了頭部和尾部 這個方法返回的是包括頭部和尾部在內的 if (position < getItemCount() && (customHeaderView != null ? position <= stringList.size() : position < stringList.size()) && (customHeaderView != null ? position > 0 : true)) { position -= customHeaderView==null?0:1; holder.tv.setText(stringList.get(position)); } } @Override public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) { View view = LayoutInflater.from(RecyclerViewActivity.this).inflate(R.layout.string_item,null); return new StringViewHolder(view,true); //初始化item的頭部布局 這里為了方便 就直接用StringViewHolder,實際使用可以使用不同于item的布局 } @Override public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) { if(customHeaderView!=null){ position-=1; } ((StringViewHolder)holder).tv.setText("header "+(position+"").charAt(0)); ((StringViewHolder)holder).tv.setTextColor(getResources().getColor(android.R.color.holo_red_dark)); //綁定item頭部view的數據,這里提取每個view的position的第一個數來作為頭部顯示數據 //即10 11 12 13 14這些 返回的是1 //20 21 22等是2 } @Override public void onItemMove(int fromPosition, int toPosition) { swapPositions(data,fromPosition,toPosition); //如果開啟的拖動移動位置的功能 //要重寫這個方法 因為如果不重寫 交換的只是view的位置,數據的位置沒有交換 一拖動,就會變成原來的樣子 super.onItemMove(fromPosition, toPosition); } @Override public void onItemDismiss(int position) { remove(data,position);//控制刪除的 super.onItemDismiss(position); } } } 

代碼我放在了github
https://github.com/duzechao/AppTemplate

</div>

 本文由用戶 Selena1568 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!