Android RecyclerView使用
- 引入布局
<android.support.v7.widget.RecyclerView
android:id="@+id/msg_recycler_notify"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
- 新建Adapter
package com.actview.hilou.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
/**
* Created by xubaolun on 2018/1/31.
*/
public class TemplateRecyclerAdapter extends RecyclerView.Adapter<TemplateRecyclerAdapter.MyHolder> {
private Context context;
private List<String> list = new ArrayList<>();
public TemplateRecyclerAdapter(Context context) {
this.context = context;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.screen_recycler_item, parent, false);
MyHolder myHolder = new MyHolder(view);
return myHolder;
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
}
public int getItemCount() {
return list.size();
}
public void setListDatas(List<String> listDatas){
list=listDatas;
notifyDataSetChanged();
}
public class MyHolder extends RecyclerView.ViewHolder{
public MyHolder(View itemView) {
super(itemView);
}
}
}
- 配置recyclerView 布局管理器
ToMeRecyclerAdapter toMeRecyclerAdapter=new ToMeRecyclerAdapter(this);
LinearLayoutManager linearLayoutManagerToMe=new LinearLayoutManager(this);
linearLayoutManagerToMe.setOrientation(LinearLayoutManager.VERTICAL);
toMeRecycler.setLayoutManager(linearLayoutManagerToMe);
toMeRecycler.setAdapter(toMeRecyclerAdapter);
- 如果想要进行监听,在adapter引入
@Override
public void onBindViewHolder(MyHolder holder, int position) {
holder.name.setText("@name"+list.get(position));
holder.reply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context.startActivity(new Intent(context, ReplyActivity.class));
}
});
}
如果想实现自己的滑动效果,比如中间显示完整item
水平效果自定义监听器实现OnScrollListener
package com.actview.hilou;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by xubaolun on 2018/3/1.
*/
public class OnHorizontalRecyclerScrollListener extends OnScrollListener {
int showCount = 3;
int itemWidth;
int itemHeight;
int parentWidth;
int parentHeight;
int maxCount;
boolean isFirst = true;
/**
* 手指移动方向,右到左
*/
boolean right2Left;
int deltaX;
int delta;
int gradeVertical = 50;
int gradeHorizontal = 50;
boolean canScale = false;
boolean horizontal=true;
public OnHorizontalRecyclerScrollListener() {
super();
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
try {
switch (newState) {
case RecyclerView.SCROLL_STATE_IDLE:
//将显示的条目居中显示
if (deltaX == 0) {
//不动
break;
}
Log.i("OnRecyclerListener", "right2Left:" + right2Left);
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
int childCount = linearLayoutManager.getChildCount();
Log.i("OnRecyclerListener", "childCount:" + childCount);
int lastComplete = linearLayoutManager.findLastCompletelyVisibleItemPosition();
Log.i("OnRecyclerListener", "lastComplete:" + lastComplete);
int lastVisible = linearLayoutManager.findLastVisibleItemPosition();
Log.i("OnRecyclerListener", "lastVisible:" + lastVisible);
int firstComplete = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
Log.i("OnRecyclerListener", "firstComplete:" + firstComplete);
int firstVisible = linearLayoutManager.findFirstVisibleItemPosition();
Log.i("OnRecyclerListener", "firstVisible:" + firstVisible);
int childIndex;
int destX;
if (right2Left) {
//右-->左,后移一个
destX = parentWidth - delta - itemWidth;
if (lastComplete > -1) {
//有全部显示的
//设置最后显示的为中间的
childIndex = lastComplete;
} else {
//没有全部显示,设置最后的为中间显示
childIndex = lastVisible;
}
} else {
//左-->右,前移一个
destX = delta;
if (firstComplete > -1) {
//有第一个全部显示的
childIndex = firstComplete;
} else {
//设置第一个可视的为第一个可全部显示的
childIndex = firstVisible;
}
}
int index = childIndex - firstVisible;
View temp = linearLayoutManager.getChildAt(index);
//实际X
float x = temp.getLeft();
if (canScale) {
recyclerView.scrollBy((int) x, 0);
break;
}
if (x > destX) {
recyclerView.scrollBy((int) Math.abs(x - destX), 0);
} else {
recyclerView.scrollBy((int) -Math.abs(x - destX), 0);
}
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
deltaX = dx;
if (dx > 0) {
right2Left = true;
} else {
right2Left = false;
}
// Log.i("OnRecyclerListener", "deltaX:" + deltaX);
// Log.i("OnRecyclerListener", "right2Left:" + right2Left);
//获取recyclerView的item尺寸
if (isFirst) {
ViewGroup viewGroup = (ViewGroup) recyclerView.getParent();
parentWidth = viewGroup.getWidth();
parentHeight = viewGroup.getHeight();
Log.i("OnRecyclerListener", "parentWidth:" + parentWidth);
Log.i("OnRecyclerListener", "parentHeight:" + parentHeight);
Log.i("OnRecyclerListener", "getPaddingLeft:" + viewGroup.getPaddingLeft());
isFirst = false;
if (showCount > 1 && showCount % 2 == 1) {
itemWidth = (int) ((parentWidth + getSub(showCount / 2) * gradeHorizontal * 2) * 1.0f / showCount);
itemHeight = parentHeight;
maxCount = showCount + 1;
delta = 0;
canScale = true;
} else {
int count = recyclerView.getChildCount();
Log.i("OnRecyclerListener", "count:" + count);
if (count > 0) {
View view = recyclerView.getChildAt(0);
if (view != null) {
itemWidth = view.getWidth();
itemHeight = view.getHeight();
Log.i("OnRecyclerListener", "itemWidth:" + itemWidth);
Log.i("OnRecyclerListener", "itemHeight:" + itemHeight);
maxCount = parentWidth / itemWidth + 2;
Log.i("OnRecyclerListener", "maxCount:" + maxCount);
delta = (parentWidth - (maxCount - 2) * itemWidth) / 2;
}
}
}
}
if (canScale) {
initCurrentView(recyclerView);
}
}
public void initCurrentView(RecyclerView recyclerView) {
int count = recyclerView.getChildCount();
Log.i("OnRecyclerListener", "count:" + count);
for (int i = 0; i < count; i++) {
View view = recyclerView.getChildAt(i);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = itemWidth - gradeHorizontal * Math.abs(i - showCount / 2);
layoutParams.height = itemHeight - gradeVertical * Math.abs(i - showCount / 2);
view.setLayoutParams(layoutParams);
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
p.setMargins(0, gradeVertical * Math.abs(i - showCount / 2) / 2, 0, 0);
view.requestLayout();
}
}
public void setShowCount(int showCount) {
this.showCount = showCount;
}
public int getSub(int count) {
int result = 0;
for (int i = 1; i <= count; i++) {
result = result + i;
}
return result;
}
public void setHorizontal(boolean horizontal) {
this.horizontal = horizontal;
}
}
竖直方向效果实现OnScrollListener
package com.actview.hilou;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by xubaolun on 2018/3/1.
*/
public class OnVerticalRecyclerScrollListener extends OnScrollListener {
int showCount = 3;
int itemWidth;
int itemHeight;
int parentWidth;
int parentHeight;
int maxCount;
boolean isFirst = true;
/**
* 手指移动方向,右到左
*/
boolean bottom2top;
int deltaY;
int delta;
int gradeVertical = 50;
int gradeHorizontal = 50;
boolean canScale = false;
boolean horizontal=false;
OnVerticalRecycleListener listener;
int realIndex;
public OnVerticalRecyclerScrollListener() {
super();
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
try {
switch (newState) {
case RecyclerView.SCROLL_STATE_IDLE:
//将显示的条目居中显示
if (deltaY == 0) {
//不动
break;
}
Log.i("OnRecyclerListener", "bottom2top:" + bottom2top);
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
int childCount = linearLayoutManager.getChildCount();
Log.i("OnRecyclerListener", "childCount:" + childCount);
int lastComplete = linearLayoutManager.findLastCompletelyVisibleItemPosition();
Log.i("OnRecyclerListener", "lastComplete:" + lastComplete);
int lastVisible = linearLayoutManager.findLastVisibleItemPosition();
Log.i("OnRecyclerListener", "lastVisible:" + lastVisible);
int firstComplete = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
Log.i("OnRecyclerListener", "firstComplete:" + firstComplete);
int firstVisible = linearLayoutManager.findFirstVisibleItemPosition();
Log.i("OnRecyclerListener", "firstVisible:" + firstVisible);
int childIndex;
int destY;
if (bottom2top) {
//下-->上,上移一个
destY = parentHeight - delta - itemHeight;
if (lastComplete > -1) {
//有全部显示的
//设置最后显示的为中间的
childIndex = lastComplete;
} else {
//没有全部显示,设置最后的为中间显示
childIndex = lastVisible;
}
realIndex=childIndex-showCount/2;
} else {
//上-->下,下移一个
destY = delta;
if (firstComplete > -1) {
//有第一个全部显示的
childIndex = firstComplete;
} else {
//设置第一个可视的为第一个可全部显示的
childIndex = firstVisible;
}
realIndex=childIndex+showCount/2;
}
int index = childIndex - firstVisible;
View temp = linearLayoutManager.getChildAt(index);
//实际y
float y = temp.getTop();
if (canScale) {
recyclerView.scrollBy( 0, (int)y);
break;
}
if (y > destY) {
recyclerView.scrollBy(0, (int) Math.abs(y - destY));
} else {
recyclerView.scrollBy(0, (int) -Math.abs(y - destY));
}
if(listener!=null){
listener.onSelected(realIndex);
}
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
deltaY = dy;
if (dy > 0) {
bottom2top = false;
} else {
bottom2top = true;
}
// Log.i("OnRecyclerListener", "deltaX:" + deltaX);
// Log.i("OnRecyclerListener", "right2Left:" + right2Left);
//获取recyclerView的item尺寸
if (isFirst) {
ViewGroup viewGroup = (ViewGroup) recyclerView.getParent();
parentWidth = viewGroup.getWidth();
parentHeight = viewGroup.getHeight();
Log.i("OnRecyclerListener", "parentWidth:" + parentWidth);
Log.i("OnRecyclerListener", "parentHeight:" + parentHeight);
isFirst = false;
if (showCount > 1 && showCount % 2 == 1) {
itemWidth = parentWidth;
itemHeight = parentHeight/showCount;
maxCount = showCount + 1;
delta = 0;
// canScale = true;
} else {
int count = recyclerView.getChildCount();
Log.i("OnRecyclerListener", "count:" + count);
if (count > 0) {
View view = recyclerView.getChildAt(0);
if (view != null) {
itemWidth = view.getWidth();
itemHeight = view.getHeight();
Log.i("OnRecyclerListener", "itemWidth:" + itemWidth);
Log.i("OnRecyclerListener", "itemHeight:" + itemHeight);
maxCount = parentHeight / itemHeight + 2;
Log.i("OnRecyclerListener", "maxCount:" + maxCount);
delta = (parentHeight - (maxCount - 2) * itemHeight) / 2;
}
}
}
}
if (canScale) {
initCurrentView(recyclerView);
}
}
public void initCurrentView(RecyclerView recyclerView) {
int count = recyclerView.getChildCount();
Log.i("OnRecyclerListener", "count:" + count);
for (int i = 0; i < count; i++) {
View view = recyclerView.getChildAt(i);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = itemWidth - gradeHorizontal * Math.abs(i - showCount / 2);
layoutParams.height = itemHeight - gradeVertical * Math.abs(i - showCount / 2);
view.setLayoutParams(layoutParams);
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
p.setMargins(0, gradeVertical * Math.abs(i - showCount / 2) / 2, 0, 0);
view.requestLayout();
}
}
public void setShowCount(int showCount) {
this.showCount = showCount;
}
public int getSub(int count) {
int result = 0;
for (int i = 1; i <= count; i++) {
result = result + i;
}
return result;
}
public void setHorizontal(boolean horizontal) {
this.horizontal = horizontal;
}
public interface OnVerticalRecycleListener{
void onSelected(int position);
}
public void setListener(OnVerticalRecycleListener listener) {
this.listener = listener;
}
}
然后调用即可,可根据具体需求实现效果
recyclerView.addOnScrollListener(new OnHorizontalRecyclerScrollListener());