前言
現(xiàn)在下拉上拉控件非常多,但是網(wǎng)上的上拉下拉控件挡闰,只有這2種功能乒融,有時(shí)候我們需要像QQ那樣滑動(dòng)item就出現(xiàn)了刪除按鈕,有時(shí)候我們需要點(diǎn)擊item可以展開更多的信息摄悯,以便我們看更多的信息赞季,有時(shí)候我們需要谷歌自帶的下拉控件,有時(shí)候需要自己定義上下拉奢驯,切換來切換去很麻煩申钩,于是我在某個(gè)大神(忘記是哪個(gè)了)的下拉上拉的控件的基礎(chǔ)上,增加了谷歌自帶下拉以及上拉樣式模仿了谷歌下拉的那種樣式瘪阁,還有滑動(dòng)item出現(xiàn)刪除跟展開更多撒遣,和城市列表邮偎,,以上的功能都只需要簡單的配置下就可以使用义黎。
首先先看個(gè)效果圖
GIF.gif
依賴:
compile'com.yanxuwen.MyRecyclerView:MyRecyclerview:1.5.7'
1.上下拉實(shí)現(xiàn):
xml:
<com.yanxuwen.MyRecyclerview.MyRecyclerView
android:id="@+id/recyclerview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
java:
mRecyclerView= (MyRecyclerView)this.findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager =newLinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
//設(shè)置刷新樣式禾进,ProgressStyle里面設(shè)置了很多種樣式,自行參考
mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
//設(shè)置加載樣式廉涕,ProgressStyle里面設(shè)置了很多種樣式泻云,自行參考
mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
//設(shè)置下拉圖標(biāo)
mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);
//設(shè)置動(dòng)畫
mRecyclerView.setItemAnimator(MyRecyclerView.ItemType.FadeInLeft);
//啟動(dòng)可以刷新
mRecyclerView.setPullRefreshEnabled(true);
//啟動(dòng)可以加載
mRecyclerView.setLoadingMoreEnabled(true);
//執(zhí)行刷新
mRecyclerView.setRefreshing(true);
//刷新跟加載監(jiān)聽
mRecyclerView.setLoadingListener(newMyRecyclerView.LoadingListener() {
public voidonRefresh() {
}
public voidonLoadMore() {
}
});
//執(zhí)行添加item操作
findViewById(R.id.add).setOnClickListener(newView.OnClickListener() {
public voidonClick(View v) {
mAdapter.add("newly added item",2);
}
});
//執(zhí)行刪除item操作
findViewById(R.id.del).setOnClickListener(newView.OnClickListener() {
public voidonClick(View v) {
mAdapter.remove(2);
}
});
//執(zhí)行點(diǎn)擊item進(jìn)行展開操作
mAdapter.setOnItemClickListener(newMyBaseAdapter.OnItemClickListener() {
public voidonItemClick(MyBaseAdapter.BaseViewHolder holder,View view, intposition) {
holder.expand();
Log.e("xxx",position +"");
}
});
刷新完后操作,記得調(diào)用
mAdapter.setFirstOnly(true);
mRecyclerView.refreshComplete();
加載完后操作:
mRecyclerView.refreshComplete();
mRecyclerView.loadMoreComplete();
如果沒有數(shù)據(jù)了狐蜕,調(diào)用
//也可以不傳遞參數(shù)宠纯,默認(rèn)會(huì)顯示空白
mRecyclerView.noMoreLoading("沒有了");
Adapter:
需要繼承MyBaseAdapter,主要介紹onCreateViewHolder的功能
publicViewHolderonCreateViewHolder(ViewGroup parent, intviewType) {
//添加滑動(dòng)item刪除按鈕馏鹤,可根據(jù)具體情況是否添加
addSwipe(R.layout.swipe_default2,SwipeLayout.ShowMode.LayDown,SwipeLayout.DragEdge.Right, true);
//添加展開詳情征椒,可根據(jù)具體情況是否添加,如何展開上面已經(jīng)有介紹了湃累。
addExpand(R.layout.expand_default);
//item布局
return newViewHolder(setLayout(R.layout.item,parent));
}
</br>
2.谷歌自帶刷新樣式:
xml:其實(shí)就是在外層嵌入SwipeRefreshLayout
<com.yanxuwen.MyRecyclerview.MySwipeRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.yanxuwen.MyRecyclerview.MyRecyclerView
android:id="@+id/recyclerview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
/>
/>
java:代碼幾乎跟上面的寫法是一樣的勃救,無非就是取消了下拉刷新用谷歌刷新替代,更改上拉加載樣式治力,所以主要介紹變動(dòng)的代碼
//設(shè)置加載樣式蒙秒,為MaterialDesign樣式,也就是類似谷歌刷新的樣式
mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.MaterialDesign);
//注意設(shè)置MaterialDesign類型的時(shí)候宵统,要單獨(dú)的在設(shè)置下顏色晕讲,最好以SwipeRefreshLayout顏色拼配
if(mRecyclerView!=null&&mRecyclerView.getMaterialProgressView()!=null){
mRecyclerView.getMaterialProgressView().setColorSchemeResources(R.color.black);
}
//【重點(diǎn),一定要調(diào)用】注意由于下來刷新用的是谷歌的马澈,瓢省,則要設(shè)置下谷歌刷新標(biāo)識(shí)
mRecyclerView.setGoogleRefresh(true,mSwipeRefreshLayout);
//啟動(dòng)可以刷新,由于啟動(dòng)了谷歌刷新痊班,該句不需要調(diào)用
//mRecyclerView.setPullRefreshEnabled(true);
//啟動(dòng)可以加載
mRecyclerView.setLoadingMoreEnabled(true);
//執(zhí)行刷新勤婚,【注意,這里的谷歌刷新我們?cè)诜庋b了一層MySwipeRefreshLayout,如果使用的是MySwipeRefreshLayout涤伐,
//mSwipeRefreshLayout.setRefreshing(true);會(huì)自動(dòng)開啟刷新馒胆,如果使用的是系統(tǒng)的MySwipeRefreshLayout,則還需要調(diào)
//用onRefresh()凝果,這些大家都指定祝迂,就不需要我多說了】
//執(zhí)行刷新
mSwipeRefreshLayout.setRefreshing(true);
</br>
3.城市列表:
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">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
</android.support.v7.widget.RecyclerView>
<com.yanxuwen.PinnedHeader.IndexBar.widget.IndexBar
android:id="@+id/indexBar"
android:layout_width="24dp"
android:layout_height="match_parent"
android:layout_marginTop="150dp"
android:layout_marginBottom="150dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
app:indexBarPressBackground="#00000000"
app:textColor="#3388FF"
app:indexBarTextSize="12sp"/>
<TextView
android:id="@+id/tvSideBarHint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="80dp"
android:minHeight="80dp"
android:layout_gravity="center"
android:background="@drawable/shape_side_bar_bg"
android:gravity="center"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
android:textSize="48sp"
android:visibility="gone"
android:padding="10dp"
tools:text="A"
tools:visibility="visible"/>
</RelativeLayout>
java:
mRv = (RecyclerView) findViewById(R.id.rv);
mRv.setLayoutManager(mManager = new LinearLayoutManager(this));
mAdapter = new CityAdapter(this, mDatas);
mHeaderAdapter = new HeaderRecyclerAndFooterWrapperAdapter(mAdapter) {
protected void onBindHeaderHolder(ViewHolder holder, int headerPos, int layoutId, Object o) {
holder.setText(R.id.tvCity, (String) o);
}
};
mHeaderAdapter.addHeaderView(R.layout.item_city, "測試頭部");
mHeaderAdapter.addHeaderView(R.layout.item_city, "測試頭部2");
mRv.setAdapter(mHeaderAdapter);
//添加頭部導(dǎo)航
mRv.addItemDecoration(mDecoration = new SuspensionDecoration(this, mDatas).setHeaderViewCount(mHeaderAdapter.getHeaderViewCount()));
mRv.addItemDecoration(new DividerItemDecoration(PinnedHeaderActivity.this, LinearLayoutManager.VERTICAL));
//使用indexBar
mTvSideBarHint = (TextView) findViewById(R.id.tvSideBarHint);//HintTextView
mIndexBar = (IndexBar) findViewById(R.id.indexBar);//IndexBar
mIndexBar.setPressedShowTextView(mTvSideBarHint)//設(shè)置HintTextView
.setNeedRealIndex(true)//設(shè)置需要真實(shí)的索引
.setLayoutManager(mManager);//設(shè)置RecyclerView的LayoutManager
initDatas(getResources().getStringArray(R.array.provinces));
initDatas:
mDatas = new ArrayList<>();
for (int i = 0; i < data.length; i++) {
CityBean cityBean = new CityBean();
cityBean.setCity(data[i]);//設(shè)置城市名稱
mDatas.add(cityBean);
}
mIndexBar
//設(shè)置懸停頭部如A,B,C,D器净,跟索引進(jìn)行關(guān)聯(lián)
.setSuspensionDecoration(mDecoration)
//是否需要根據(jù)實(shí)際的數(shù)據(jù)來生成索引數(shù)據(jù)源(例如 只有 A B C 三種tag型雳,那么索引欄就 A B C 三項(xiàng)),如果為false索引欄就會(huì)顯示從A到Z的數(shù)據(jù)
.setNeedRealIndex(true)
//設(shè)置是否排序,默認(rèn)true強(qiáng)制從A到Z進(jìn)行排序四啰,false則會(huì)按照給的數(shù)據(jù)順序進(jìn)行排序宁玫,而且會(huì)自動(dòng)隱藏索引欄
.setIsSort(true)
.setSourceDatas(mDatas)//設(shè)置數(shù)據(jù)
.setHeaderViewCount(mHeaderAdapter.getHeaderViewCount())//設(shè)置HeaderView數(shù)量
.invalidate();
mAdapter.setDatas(mDatas);
mHeaderAdapter.notifyDataSetChanged();
Adapter:跟上面的Adapter沒什么區(qū)別粗恢,這里就不需要多介紹
CityBean 重點(diǎn)柑晒,一定要繼承BaseIndexPinyinBean
public class CityBean extends BaseIndexPinyinBean {
private String city;//城市名字
public CityBean() {
}
public CityBean(String city) {
this.city = city;
}
public String getCity() {
return city;
}
public CityBean setCity(String city) {
this.city = city;
return this;
}
/**
* 是否需要被轉(zhuǎn)化成拼音,
* true的話眷射,頭部跟索引都會(huì)將getTarget值轉(zhuǎn)換為拼音
* 【這里給ab值做了特殊處理匙赞,如果是ab則不強(qiáng)制轉(zhuǎn)換為拼音】
*/
@Override
public boolean isNeedToPinyin() {
return city.equals("ab")?false:true;
}
/**
* 前提【isNeedToPinyin為false,才能顯示是全文,不然都getTarget的首拼】
* 頭部跟索引的值妖碉,isNeedToPinyin為true則會(huì)自動(dòng)轉(zhuǎn)換為拼音首字母涌庭,false的話則原文顯示
* 【這里做了特殊處理,如果是ab值欧宜,則頭部顯示"&%%"標(biāo)記坐榆,其他顯示Aab】
*/
@Override
public String getTarget() {
return city.equals("ab")?"&%%":city;
}
/**
* 前提【isNeedToPinyin為false,才能自定義,不然無線】
* 自定義索引值
*/
public String getIndexString() {
return city.equals("ab")?"&":null;
}
/**
* 是否需要顯示懸停title
*/
@Override
public boolean isShowSuspension() {
return true;
}
}
github代碼:https://github.com/yanxuwen/MyRecyclerView
微信公眾號(hào):
qrcode_for_gh_8e99f824c0d6_344.jpg