转载请注明原创出处,谢谢!
- GitHub: @Ricco
ps:
产品提了一个需求,放大第一个item,可以左右滑动,无限循环,分页效果
自定义View
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
/**
* 支持自动滚动的无限横滑的第一个item放大效果的RecyclerView
*/
public class CustomRecyclerView extends RecyclerView implements LifecycleObserver {
private int delayMillis = 3000;
private Handler handler = null;
private Runnable runnable = null;
public CustomRecyclerView(@NonNull Context context) {
super(context);
init();
}
public CustomRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setLayoutManager(new EnlargeLinearLayoutManager(getContext()));
new PagerSnapHelper().attachToRecyclerView(this);
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(this, delayMillis);
View view = getChildAt(0);
if (view != null) {
smoothScrollBy(view.getWidth(), 0);
}
}
};
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
private void resume() {
if (handler != null) {
handler.postDelayed(runnable, delayMillis);
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
private void pause() {
if (handler != null) {
handler.removeCallbacksAndMessages(null);
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
private void destroy() {
if (handler != null) {
handler.removeCallbacksAndMessages(null);
handler = null;
}
}
class EnlargeLinearLayoutManager extends LinearLayoutManager {
EnlargeLinearLayoutManager(Context context) {
super(context);
setOrientation(HORIZONTAL);
setReverseLayout(false);
}
@Override
public int scrollHorizontallyBy(int dx, Recycler recycler, State state) {
float d = 0f;
float b = 0f;
View view = findViewByPosition(findFirstVisibleItemPosition());
if (view != null) {
d = view.getPaddingTop() * 2f / view.getHeight();
b = Math.abs(view.getX()) / view.getWidth();
view.setScaleX(1 + d * (1 - b));
view.setScaleY(1 + d * (1 - b));
}
View view2;
if (dx > 0) {
view2 = findViewByPosition(findFirstVisibleItemPosition() + 1);
} else {
view2 = findViewByPosition(findFirstCompletelyVisibleItemPosition());
}
if (view2 != null) {
view2.setScaleX(1 + d * b);
view2.setScaleY(1 + d * b);
}
return super.scrollHorizontallyBy(dx, recycler, state);
}
}
}
item.xml padding很重要
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:clipToPadding="true"
android:orientation="vertical"
android:paddingStart="8dp"
android:paddingTop="10dp"
android:paddingEnd="8dp"
android:paddingBottom="10dp">
<ImageView
android:id="@+id/ivImg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@null"
android:src="@drawable/dou_test" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:gravity="center"
android:textColor="@color/dou_color3"
android:textSize="12sp"
tools:text="平台稳定安全" />
<TextView
android:id="@+id/tvContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:gravity="center"
android:textColor="@color/dou_color6"
android:textSize="10sp"
tools:text="智能巡查+人工审核\n双重保障拒绝断缴" />
</LinearLayout>
</FrameLayout>
adapter 主要实现无限循环
public class ItemAdapter extends BaseQuickAdapter<Item, BaseViewHolder> {
public ItemAdapter () {
super(R.layout.item);
}
@Override
protected void convert(@NonNull BaseViewHolder holder, Itemitem) {
if (item != null) {
holder.setText(R.id.tvTitle, "" + item.getTitle())
.setText(R.id.tvContent, "" + item.getContent());
Glide.with(mContext)
.load(item.getImg())
.into((ImageView) holder.getView(R.id.ivImg));
}
}
@Override
public int getItemCount() {
return Short.MAX_VALUE;
}
@Override
public HomeEntity.HomeArrBean getItem(int position) {
if (getData().size() == 0) {
return null;
}
int newPosition = position % getData().size();
return getData().get(newPosition);
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public void setNewData(@Nullable List<HomeEntity.HomeArrBean> data) {
super.setNewData(data);
if (data != null) {
if (getRecyclerView() != null) {
getRecyclerView().scrollToPosition(Short.MAX_VALUE / 2 / data.size() * data.size());
}
}
}
}
activity,记得绑定lifecycle,及时销毁handler
adapter.bindToRecyclerView(rv);
getLifecycle().addObserver(rv);