以下是一个实现水平主动轮播效果的自定义RecyclerView的详细实例代码:

  1. 创建一个名为AutoScrollRecyclerView的自定义RecyclerView类,并继承自RecyclerView。
public class AutoScrollRecyclerView extends RecyclerView {

    private static final long DEFAULT_INTERVAL = 3000; // 默认轮播时间间隔
    private static final int SCROLL_DISTANCE = 20; // 每次滚动的距离
    private long interval = DEFAULT_INTERVAL; // 轮播时间间隔

    private Handler handler;
    private boolean isAutoScroll = false; // 是否自动滚动
    private boolean isScrolling = false; // 是否正在滚动

    public AutoScrollRecyclerView(Context context) {
        super(context);
        init();
    }

    public AutoScrollRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AutoScrollRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        handler = new Handler();
    }

    // 开始自动滚动
    public void startAutoScroll() {
        if (getAdapter() == null || getAdapter().getItemCount() == 0) {
            return;
        }
        isAutoScroll = true;
        handler.postDelayed(scrollRunnable, interval);
    }

    // 停止自动滚动
    public void stopAutoScroll() {
        isAutoScroll = false;
        handler.removeCallbacks(scrollRunnable);
    }

    // 设置轮播时间间隔
    public void setInterval(long interval) {
        this.interval = interval;
    }

    private Runnable scrollRunnable = new Runnable() {
        @Override
        public void run() {
            if (isAutoScroll) {
                smoothScrollBy(SCROLL_DISTANCE, 0);
                handler.postDelayed(this, interval);
            }
        }
    };

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                stopAutoScroll();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                startAutoScroll();
                break;
        }
        return super.onTouchEvent(e);
    }

    @Override
    public boolean fling(int velocityX, int velocityY) {
        boolean fling = super.fling(velocityX, velocityY);
        if (fling) {
            startAutoScroll();
        }
        return fling;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stopAutoScroll();
    }
}
  1. 在布局文件中使用AutoScrollRecyclerView。
<com.example.autoscrollrecyclerview.AutoScrollRecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
  1. 创建一个适配器类,并继承自RecyclerView.Adapter。
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<String> dataList;

    public MyAdapter(List<String> dataList) {
        this.dataList = dataList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.textView.setText(dataList.get(position));
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textView);
        }
    }
}
  1. 创建一个布局文件item_layout.xml,用于显示每个列表项的内容。
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:textSize="18sp" />
  1. 在Activity中使用AutoScrollRecyclerView,并设置适配器和数据源。
public class MainActivity extends AppCompatActivity {

    private AutoScrollRecyclerView recyclerView;
    private MyAdapter adapter;
    private List<String> dataList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

        dataList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            dataList.add("Item " + (i + 1));
        }

        adapter = new MyAdapter(dataList);
        recyclerView.setAdapter(adapter);
        recyclerView.startAutoScroll();
    }
}

这样,就实现了一个自定义的水平主动轮播效果的RecyclerView。通过调用startAutoScroll()方法开始自动滚动,调用stopAutoScroll()方法停止自动滚动。可以通过setInterval()方法设置轮播时间间隔

Android 自定义RecyclerView 水平主动轮播效果详细实例代码

原文地址: https://www.cveoy.top/t/topic/ikzu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录