1结合SwipeRefreshLayout 实现下拉刷新。
2实现上拉刷新
3更新列表中的数据
4设置条目点击事件
首先看RecycleView的布局文件配置:
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/refreshlayout_left" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview_left" android:layout_width="match_parent" android:layout_height="match_parent" android:layoutAnimation="@anim/list_anim_layout" android:cacheColorHint="@null" android:scrollbars="vertical" /> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout>list_anim.xml
<?xml version="1.0" encoding="utf-8"?> <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:delay="0.4" android:animationOrder="normal" android:animation="@anim/list_anim"/>list_anim_layout.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500"/> <translate android:fromYDelta="-10%p" android:toYDelta="0%p" android:duration="500" /> </set>
初始化 RecycleView 控件 和 下拉控件
init{
// 设置下拉 控件属性 leftPullRefresh.setColorSchemeResources(R.color.color_037AFF, R.color.color_success, R.color.text_color_ff3747, R.color.text_color_acacac);//设置加载圈圈的颜色 leftPullRefresh.setDistanceToTriggerSync(300);//设置手指下滑距离 rightPullRefresh.setColorSchemeResources(R.color.color_037AFF, R.color.color_success, R.color.text_color_ff3747, R.color.text_color_acacac);//设置加载圈圈的颜色 rightPullRefresh.setDistanceToTriggerSync(300);//设置手指下滑距离
initLeftList();
}
// RecycleView 空间的 适配器 adapter 和 下拉刷新标记
GiftMoneyAdapter leftListAdapter = null;
private int astVisibleItemLeft = 0;
private void initLeftList() { linearManagerLeft = new LinearLayoutManager(this);//设置布局管理器 linearManagerLeft.setOrientation(LinearLayoutManager.VERTICAL); leftList.setLayoutManager(linearManagerLeft); leftList.setItemAnimator(new DefaultItemAnimator());//设置Item增加、移除动画 leftPullRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { //下拉刷新 业务方法 } }); leftListAdapter = new GiftMoneyAdapter(this); leftListAdapter.setOnItemClickLitener(new GiftMoneyAdapter.OnItemClickLitener() { @Override public void onItemClick(View view, int position, Button btn) { // 条目点击事件处理 } }); leftList.setAdapter(leftListAdapter); leftList.setOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == RecyclerView.SCROLL_STATE_IDLE && astVisibleItemLeft + 1 == leftListAdapter.getItemCount()) { //上拉刷新业务方法 }if (/*newState == RecyclerView.SCROLL_STATE_DRAGGING &&*/ astVisibleItemLeft + 1 == adapter.getItemCount()) { Log.i("SCROLL_STATE_DRAGGING", "手滑到底部了"); } if (astVisibleItemtop == 0) { Log.i("SCROLL_STATE_DRAGGING", "手滑到顶部了"); }if (newState == RecyclerView.SCROLL_STATE_DRAGGING) { Log.i("SCROLL_STATE_DRAGGING", "手滑动:SCROLL_STATE_DRAGGING"); } else if (newState == RecyclerView.SCROLL_STATE_SETTLING) { Log.i("SCROLL_STATE_SETTLING", "松开惯性滑动:SCROLL_STATE_SETTLING"); } else if (newState == RecyclerView.SCROLL_STATE_IDLE) { Log.i("SCROLL_STATE_IDLE", "手松开:SCROLL_STATE_IDLE"); }}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy);if (dy < 0) { Log.i("-----", "手指向下划" + dy); } else { Log.i("-----", "手指向上划" + dy); }astVisibleItemLeft = linearManagerLeft.findLastVisibleItemPosition(); } });}astVisibleItemtop = linearManagerLeft.findFirstVisibleItemPosition();
Adapter 类的编写。。
package com.hanya.financing.main.game.more;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.hanya.financing.R;
import com.hanya.financing.global.domain.ActiveCenterMoreCardEntity;
import com.hanya.financing.global.domain.ActiveCenterMoreGiftMoneyEntity;
import java.util.ArrayList;
import java.util.List;
public class GiftMoneyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_ITEM = 0;
private static final int TYPE_HEADER = 1;
@Override//我理解是灵活设置item的显示类型,可以显示多种不同item
public int getItemViewType(int position) {
if (position == 0) {
return TYPE_HEADER;
} else {
return TYPE_ITEM;
}
}
String nextInvestMoney;
String investMoney;
public void setHeaderData(String nextInvestMoney,String investMoney){
this.nextInvestMoney = nextInvestMoney;
this.investMoney = investMoney;
}
public List<ActiveCenterMoreGiftMoneyEntity.GiftInfo> getData() {
return this.data;
}
/**
* 设置数据
* @param newData list上的数据
* @param isAddMore true:如果是下拉刷新,则加载最新数据, false:否则 (上拉刷新)追加数据
*/
public void setData(List<ActiveCenterMoreGiftMoneyEntity.GiftInfo> newData,boolean isAddMore) {
if(isAddMore){
this.data.clear();
this.data.addAll(newData);
}else{
this.data.addAll(newData);
}
}
List<ActiveCenterMoreGiftMoneyEntity.GiftInfo> data;
Context context;
public GiftMoneyAdapter(Context context){
this.context = context;
this.data = data;
data = new ArrayList<ActiveCenterMoreGiftMoneyEntity.GiftInfo>();
}
/**
* 初始化view
* @param parent
* @param viewType
* @return
*/
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
View itemView = LayoutInflater.from(context).inflate(R.layout.item_activitycenter_more_giftmoney, null);
ItemViewHolder itemViewHolder = new ItemViewHolder(itemView);
return itemViewHolder;
}else if(viewType==TYPE_HEADER){
View header = LayoutInflater.from(context).inflate(R.layout.item_activitycenter_more_giftmoney_header, null);
header.findViewById(R.id.line_to_hide).setVisibility(View.GONE);
header.findViewById(R.id.btn_use).setVisibility(View.GONE);
ItemViewHolder headerHolder = new ItemViewHolder(header);
return headerHolder;
}
return null;
}
/**
* 绑定数据
* @param holder
* @param position
*/
@Override
public void onBindViewHolder( RecyclerView.ViewHolder holder, final int position) {
final ItemViewHolder h = (ItemViewHolder) holder;
if(h.getItemViewType()==TYPE_HEADER){
h.控件 set value
}else if(h.getItemViewType()==TYPE_ITEM){
ActiveCenterMoreGiftMoneyEntity.GiftInfo itemdata = data.get(position);
String value= itemdata.getValue();
h.控件 .setValue(value);
h.btn_use.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickLitener.onItemClick(h.itemView, position, h.btn_use);
}
});
}
}
//自定义的ViewHolder,持有每个Item的的所有界面元素
class ItemViewHolder extends RecyclerView.ViewHolder {
View line_to_hide;
TextView tv_giftmoney;
TextView tv_giftmoney_amount;
Button btn_use;
public ItemViewHolder(View view) {
super(view);
tv_giftmoney = (TextView) view.findViewById(R.id.tv_giftmoney);
tv_giftmoney_amount = (TextView) view.findViewById(R.id.tv_giftmoney_amount);
btn_use = (Button) view.findViewById(R.id.btn_use);
line_to_hide = view.findViewById(R.id.line_to_hide);
}
}
@Override
public int getItemCount() {
return this.data.size() ;
}
/**
* 自定义ItemClick的回调接口
*/
private OnItemClickLitener mOnItemClickLitener;
public interface OnItemClickLitener {
void onItemClick(View view, int position, Button btn_usercard);
}
public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
this.mOnItemClickLitener = mOnItemClickLitener;
}
}
设置数据。。
ActiveCenterMoreCardEntity entity = new ActiveCenterMoreCardEntity(ActiveCenterMoreCardEntity.CARD_INDEX_INFO);
boolean isPullRefresh = rightPullRefresh.isRefreshing();
if(isPullRefresh){
rightPullRefresh.setRefreshing(false);
}
try {
entity.unPackData(data);
this.currentCardentity = entity;
showCardPageData(entity,isPullRefresh);
} catch (Exception e) {
e.printStackTrace();
}
showCardPageData(bean,boolean isAddMore){
//更新列表
List<ActiveCenterMoreCardEntity.CardInfo> d = entity.getCardList();
if (d != null && d.size() > 0) {
// boolean isAddMore = (entity.getOffset() < entity.getPagesize()) ? false : true;
rightListAdapter.setData(d, isAddMore);
rightListAdapter.notifyDataSetChanged();
// 偏移量 成倍加10
currentOffsetRight = currentOffsetRight + entity.getPagesize();
}
}