Android_RecyclerView实现上下滚动广告条(带图片)

前言

公司新项目首页有个类似京东/淘宝滚动广告条,查了一下大概都是两种实现方式,一是textview,如果只有文字的话是可行的,但我们这个上面还有个小图片,所以pass;二是两个viewGroup,使用动画交替滚动,可以实现,就是显得很麻烦,于是偷懒的我就想着用recyclerView来解决这个小问题!

思路

这个滚动广告条高度通常是固定的,用一个固定高度的viewGroup来包裹一个recyclerView,recylerView的item布局设置一个minHeight为viewGroup的高度,这样刚好能看到一个完整的item,然后使用recyclerView自带的方法 smoothScrollBy()来滚动recyclerView;他需要两个参数,x轴的滚动距离和y轴的滚动距离,我们是上下滚动,所以x轴传入1就好啦!y轴距离传入你的item高度,然后使用handler写一个循环任务就可以实现一直滚动啦!

/**
     * Animate a scroll by the given amount of pixels along either axis.
     *
     * @param dx Pixels to scroll horizontally
     * @param dy Pixels to scroll vertically
     */
    public void smoothScrollBy(int dx, int dy) {
        smoothScrollBy(dx, dy, null);
    }

遇到的问题

写好之后发现这个控件是不能够触摸滑动的,但是又需要点击事件。想了想如果在onTouchEvent之类的方法中处理的话很麻烦,还不能保证完全禁止一点点都不能滑,所以就又想了个偷懒的办法。给recyclerView上加一层透明的蒙板,彻底禁用掉recyclerView的touch事件,给蒙板设置点击事件……下面是代码

布局:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="60dp"
              android:background="@color/colorWhite"
              android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="18dp"
        android:gravity="center"
        android:text="养车\n宝典"
        android:textColor="@color/colorTitle"
        android:textSize="12sp"/>

    <View
        android:layout_width="0.5dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="12dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="12dp"
        android:background="@color/colorTitle"/>

    
    <RelativeLayout
        android:layout_marginLeft="6dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <com.xinshiwi.mycar.view.AutoScrollRecyclerView
            android:id="@+id/rv_home_maintain"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <View
            android:id="@+id/view_home_maintain"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"/>

    RelativeLayout>

LinearLayout>

Adapter:

public class MaintainInfoAdapter extends RecyclerView.Adapter<MaintainInfoAdapter.MyViewHolder> {

    List list;
    public MaintainInfoAdapter(List list) {
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_maintain, null);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv.setText(list.get(position % 4));
    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv_maintain);
        }

    }
}

设置recyclerView:

/**
     * 滚动养车宝典
     */
    private void initMaintainData() {
        mList = new ArrayList<>();
        mList.add("如何做好队汽车的轮胎养护0");
        mList.add("如何做好队汽车的轮胎养护1");
        mList.add("如何做好队汽车的轮胎养护2");
        mList.add("如何做好队汽车的轮胎养护3");
        mRvHomeMaintain.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new MaintainInfoAdapter(mList);
        mRvHomeMaintain.setAdapter(mAdapter);
        Message msg = new Message();
        msg.what = MAINTAIN_INFO;
        sHandler.sendMessageDelayed(msg, 3000);
        //通过一个透明的蒙板来设置点击事件
        mViewHomeMaintain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mActivity, "pos % 4:" + (pos % 4), Toast.LENGTH_SHORT).show();
            }
        });
    }
//当前显示的item
private int pos = 0;
private Handler sHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MAINTAIN_INFO:
                    mRvHomeMaintain.smoothScrollBy(0, SizeUtils.dp2px(60));
                    pos++;
                    Message message = new Message();
                    message.what = MAINTAIN_INFO;
                    sHandler.removeMessages(MAINTAIN_INFO);
                    sHandler.sendMessageDelayed(message, 3000);
                    break;
            }
        }
    };

只是一个小demo,很多细节没太考虑……有什么问题还望大佬们指出,不胜感激!

给大佬递茶.jpg

你可能感兴趣的:(Android学习之路)