如果是使用ListView的话我们知道,可以很简单的实现加载的动画,但是使用android.support.v7.widget.RecyclerView 的话,暂时还不是很多,所以自己就实现了一个效果,先看图吧(第一次上传动态图片,不喜勿喷哦)
图片有点模糊,但是能看到效果,那就是下面进入会有一个延迟的效果,下面看看代码吧;
这是一个自定义的,能够上拉加载更多的RecyclerView
package com.socks.jiandan.view;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import com.meyhuan.mytest.LoadFinishCallBack;
import com.nostra13.universalimageloader.core.ImageLoader;
/**
* Created by meyhuan 15/4/9.
*/
public class AutoLoadRecyclerView extends RecyclerView implements LoadFinishCallBack {
private onLoadMoreListener loadMoreListener;
private boolean isLoadingMore;
public AutoLoadRecyclerView(Context context) {
this(context, null);
}
public AutoLoadRecyclerView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AutoLoadRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
isLoadingMore = false;
setOnScrollListener(new AutoLoadScrollListener(null, true, true));
}
/**
* 如果需要显示图片,需要设置这几个参数,快速滑动时,暂停图片加载
*
* @param imageLoader
* @param pauseOnScroll
* @param pauseOnFling
*/
public void setOnPauseListenerParams(ImageLoader imageLoader, boolean pauseOnScroll, boolean pauseOnFling) {
setOnScrollListener(new AutoLoadScrollListener(imageLoader, pauseOnScroll, pauseOnFling));
}
public void setLoadMoreListener(onLoadMoreListener loadMoreListener) {
this.loadMoreListener = loadMoreListener;
}
@Override
public void loadFinish(Object obj) {
isLoadingMore = false;
}
public interface onLoadMoreListener {
void loadMore();
}
/**
* 滑动自动加载监听器
*/
private class AutoLoadScrollListener extends OnScrollListener {
private ImageLoader imageLoader;
private final boolean pauseOnScroll;
private final boolean pauseOnFling;
public AutoLoadScrollListener(ImageLoader imageLoader, boolean pauseOnScroll, boolean pauseOnFling) {
super();
this.pauseOnScroll = pauseOnScroll;
this.pauseOnFling = pauseOnFling;
this.imageLoader = imageLoader;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//由于GridLayoutManager是LinearLayoutManager子类,所以也适用
if (getLayoutManager() instanceof LinearLayoutManager) {
int lastVisibleItem = ((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
int totalItemCount = AutoLoadRecyclerView.this.getAdapter().getItemCount();
//有回调接口,并且不是加载状态,并且剩下2个item,并且向下滑动,则自动加载
if (loadMoreListener != null && !isLoadingMore && lastVisibleItem >= totalItemCount -
2 && dy > 0) {
loadMoreListener.loadMore();
isLoadingMore = true;
}
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (imageLoader != null) {
switch (newState) {
case 0:
imageLoader.resume();
break;
case 1:
if (pauseOnScroll) {
imageLoader.pause();
} else {
imageLoader.resume();
}
break;
case 2:
if (pauseOnFling) {
imageLoader.pause();
} else {
imageLoader.resume();
}
break;
}
}
}
}
}
package com.meyhuan.mytest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.preference.PreferenceManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
com.socks.jiandan.view.AutoLoadRecyclerView mRecyclerView;
SwipeRefreshLayout mSwipeRefreshLayout;
private FreshNewsAdapter mAdapter;
private LoadFinishCallBack mLoadFinisCallBack;
private ImageLoader imageLoader;
private DisplayImageOptions options;
HttpUtils httpUtils = new HttpUtils();
BitmapUtils bitmapUtils ;
//是否是大图模式
private static boolean isLargeMode = false;
private int index =1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bitmapUtils = new BitmapUtils(this);
mRecyclerView = (com.socks.jiandan.view.AutoLoadRecyclerView) findViewById(R.id.recycler_view);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
mRecyclerView.setHasFixedSize(false);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mLoadFinisCallBack = mRecyclerView;
mRecyclerView.setLoadMoreListener(new com.socks.jiandan.view.AutoLoadRecyclerView.onLoadMoreListener() {
@Override
public void loadMore() {
mAdapter.loadNextPage();
}
});
mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mAdapter.loadFirst();
}
});
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
imageLoader = ImageLoader.getInstance();
// mRecyclerView.setOnPauseListenerParams(imageLoader, false, true);
// SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
// isLargeMode = sp.getBoolean(SettingFragment.ENABLE_FRESH_BIG, true);
int loadingResource;
//大图模式
if (isLargeMode) {
loadingResource = R.drawable.ic_loading_large;
} else {
loadingResource = R.drawable.ic_loading_small;
}
options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.resetViewBeforeLoading(true)
.showImageOnLoading(loadingResource)
.build();
mAdapter = new FreshNewsAdapter();
mRecyclerView.setAdapter(mAdapter);
mAdapter.loadFirst();
}
/**
* 新鲜事适配器
*/
public class FreshNewsAdapter extends RecyclerView.Adapter {
// private int page;
private ArrayList freshNewses;
private int lastPosition = -1;
public FreshNewsAdapter(ArrayList freshNewses) {
this.freshNewses = freshNewses;
}
public FreshNewsAdapter() {
freshNewses = new ArrayList();
}
private void setAnimation(View viewToAnimate, int position) {
if (position > lastPosition) {
Animation animation = AnimationUtils.loadAnimation(viewToAnimate.getContext(), R
.anim.item_bottom_in);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
@Override
public void onViewDetachedFromWindow(ViewHolder holder) {
super.onViewDetachedFromWindow(holder);
holder.ll_content.clearAnimation();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
int layoutId;
Log.e("Tag", "onCreateViewHolder");
layoutId = R.layout.main_fragment_list_item;
View v = LayoutInflater.from(parent.getContext())
.inflate(layoutId, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
if(freshNewses != null && freshNewses.size() > 0){
Goods freshNews = freshNewses.get(position);
// imageLoader.displayImage(freshNews.getImageUrl(), holder.img, options);
bitmapUtils.display( holder.img,freshNews.getImageUrl());
holder.tv_title.setText(freshNews.getAddress());
holder.tv_info.setText(freshNews.getPrice() + "@" );
setAnimation(holder.ll_content, position);
}
}
@Override
public int getItemCount() {
return freshNewses.size();
}
public void loadFirst() {
index = 1;
loadData();
}
public void loadNextPage() {
index++;
loadData();
}
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView tv_title;
private TextView tv_info;
private TextView tv_views;
private TextView tv_share;
private ImageView img;
private LinearLayout ll_content;
public ViewHolder(View contentView) {
super(contentView);
tv_title = (TextView) contentView.findViewById(R.id.main_fragment_item_text1);
tv_info = (TextView) contentView.findViewById(R.id.main_fragment_item_text2);
tv_views = (TextView) contentView.findViewById(R.id.main_fragment_item_text3);
img = (ImageView) contentView.findViewById(R.id.main_fragment_item_imageView);
ll_content = (LinearLayout) contentView.findViewById(R.id.main_fragment_item_layout);
}
}
private void loadData() {
httpUtils.send(HttpRequest.HttpMethod.GET, Goods.URL_FRESH_NEWS + index, new RequestCallBack() {
@Override
public void onSuccess(ResponseInfo responseInfo) {
ArrayList freshNewses = Goods.parse(responseInfo.result);
if (1 == index) {
mAdapter.freshNewses.clear();
mAdapter.freshNewses.addAll(freshNewses);
} else {
mAdapter.freshNewses.addAll(freshNewses);
}
mAdapter.notifyDataSetChanged();
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
mLoadFinisCallBack.loadFinish(null);
}
@Override
public void onFailure(HttpException e, String s) {
}
});
}
}
代码使用到了xUtils开源框架,关键是在
FreshNewsAdapter,的
@Override
public void onViewDetachedFromWindow(ViewHolder holder) {
super.onViewDetachedFromWindow(holder);
holder.ll_content.clearAnimation();
}
方法里面要做这样的一步操作,也就是当View滑出屏幕的时候,要取消它的动画
holder.ll_content.clearAnimation();
这里不多说了,想深入的了解,那就下载代码去吧。
点击下载源码