SoFunction
Updated on 2025-03-11

Android_RecyclerView implements up and down scrolling ad bar instance (with pictures)

Preface

There is a scrolling advertising bar on the company's new project homepage similar to JD/Taobao. I checked it and there are probably two ways to implement it. One is textview. If there is only text, it is feasible, but we have a small picture on it, so pass; the other is two viewGroups, which can be realized using animations to scroll alternately, but it seems very troublesome, so I was lazy and thought of using recyclerView to solve this small problem!

Ideas

The height of this scrolling ad bar is usually fixed. Use a fixed height viewGroup to wrap a recyclerView. The item layout of the recyclerView sets a minHeight to the height of the viewGroup, so that you can see a complete item, and then use the method smoothScrollBy() provided by the recyclerView to scroll the recyclerView; it requires two parameters, the scroll distance of the x-axis and the scroll distance of the y-axis. We scroll up and down, so just pass 1 in the x-axis! The y-axis distance is passed to your item height, and then write a loop task using handler to achieve continuous scrolling!

/**
   * 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);
  }

Problems encountered

After writing, I found that this control cannot be touched and swiped, but I also need to click events. After thinking about it, it would be troublesome to deal with it in methods such as onTouchEvent. It is not guaranteed that it will not be slipped at all, so I thought of a lazy way. Add a transparent mask to the recyclerView, completely disable the touch event of the recyclerView, and set a click event for the mask... The following is the code

layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
       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="Car Maintenance\nTreasure Book"
    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"/>

  <!--DisabledrecyclerViewTouch events,His click event is implemented by a transparent mask-->
  <RelativeLayout
    android:layout_marginLeft="6dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <
      android:
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

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

  </RelativeLayout>

</LinearLayout>

Adapter:

public class MaintainInfoAdapter extends <> {

  List<String> list;
  public MaintainInfoAdapter(List<String> list) {
     = list;
  }

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

    return new MyViewHolder(view);
  }

  @Override
  public void onBindViewHolder(MyViewHolder holder, int position) {
    ((position % 4));
  }

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

  public static class MyViewHolder extends  {

    public TextView tv;

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

  }
}

Setting the recyclerView:

/**
    * Rolling car maintenance certificate
    */
  private void initMaintainData() {
    mList = new ArrayList&lt;&gt;();
    ("How to maintain tires in team cars 0");
    ("How to maintain tires in team cars 1");
    ("How to do a good job in tire maintenance for team cars 2");
    ("How to do a good job in tire maintenance for team cars 3");
    (new LinearLayoutManager(mActivity));
    mAdapter = new MaintainInfoAdapter(mList);
    (mAdapter);
    Message msg = new Message();
     = MAINTAIN_INFO;
    (msg, 3000);
    //Set click events with a transparent mask    (new () {
      @Override
      public void onClick(View v) {
        (mActivity, "pos % 4:" + (pos % 4), Toast.LENGTH_SHORT).show();
      }
    });
  }
//The currently displayed itemprivate int pos = 0;
private Handler sHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      (msg);
      switch () {
        case MAINTAIN_INFO:
          (0, SizeUtils.dp2px(60));
          pos++;
          Message message = new Message();
           = MAINTAIN_INFO;
          (MAINTAIN_INFO);
          (message, 3000);
          break;
      }
    }
  };

It's just a small demo, and I didn't consider many details too much... I hope the big guys will point out any questions. I am very grateful and I hope everyone supports me.