RecycleView实现 viewFlipper 广告条垂直滚动效果(仿叮咚买菜)

编程入门 行业动态 更新时间:2024-10-07 04:24:05

RecycleView实现 viewFlipper 广告条垂直滚动效果(仿叮咚<a href=https://www.elefans.com/category/jswz/34/1765717.html style=买菜)"/>

RecycleView实现 viewFlipper 广告条垂直滚动效果(仿叮咚买菜)


demo码云代码仓库

实现效果

viewFlipper 的使用这里就不介绍了,想通过viewFlipper实现广告条滚动效果的可以去看 git上的这个案例

因为不是什么太难的功能就不对细节做太多的讲解了,代码里有我写好的注释,直接上代码

view

/*** @author whl* Created on: 3/1/22 3:07 PM* description*/
public class UpRollRecyclerFragment extends Fragment {private Thread thread;private final DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator();private TestAdapter testAdapter;private final List<TestBean> mDataList=new ArrayList<>();public UpRollRecyclerFragment() {// Required empty public constructor}private View view ;private RecyclerView rv;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {view = inflater.inflate(R.layout.fragment_up_roll_recycler, container, false);rv = view.findViewById(R.id.rv);init();return view;}@SuppressLint("ClickableViewAccessibility")//禁用 Lint 检查 去代码警告private void init() {// 不处理手势rv.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {return true;}});//数据填充for (int i = 0; i < 18; i++) {TestBean dataList=new TestBean("哈哈哈"+i,".png","=http%3A%2F%2Ffile02.16sucai%2Fd%2Ffile%2F2014%2F0827%2Fc0c92bd51bb72e6d12d5b877dce338e8.jpg&refer=http%3A%2F%2Ffile02.16sucai&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648713402&t=33e7b1550ce710d69923b27867318334");mDataList.add(dataList);}//根据需求更换布局管理器GridLayoutManager manager=new GridLayoutManager(getActivity(),2);rv.setLayoutManager(manager);new PagerSnapHelper().attachToRecyclerView(rv);testAdapter = new TestAdapter(mDataList, getActivity());rv.setAdapter(testAdapter);startRoll();initListener();}//开启线程调用recycleView的 smoothScrollByprivate void startRoll(){thread = new Thread(new Runnable() {@Overridepublic void run() {SystemClock.sleep(3000);if (!thread.isInterrupted()) {rv.smoothScrollBy(0,dip2px(getActivity(),120), decelerateInterpolator);this.run();}}});thread.start();}private void initListener(){testAdapter.buttonSetOnclick(new TestAdapter.ButtonInterface() {@Overridepublic void onclick(View view, int position) {//进行你的跳转操作 需要拿值可以在接口中添加参数传出Toast.makeText(getActivity(),"点击",Toast.LENGTH_SHORT).show();}});}public static int dip2px(Context context, int dip) {float scale = context.getResources().getDisplayMetrics().density;return (int) ((float) dip * scale + 0.5F);}//页面销毁对线程的处理@Overridepublic void onDestroy() {super.onDestroy();if (thread != null)thread.interrupt();}}

adapter

/*** @author whl* Created on: 3/1/22 3:09 PM* description*/
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.VH>{private final List<TestBean> beanList;private final Context context;private ButtonInterface buttonInterface;public TestAdapter(List<TestBean> beanList, Context context) {this.beanList = beanList;this.context = context;}public void buttonSetOnclick(ButtonInterface buttonInterface){this.buttonInterface=buttonInterface;}public interface ButtonInterface{public void onclick( View view,int position);}@NonNull@Overridepublic VH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {return new VH(LayoutInflater.from(context).inflate(R.layout.item_rv, viewGroup, false));}@Override@SuppressLint("RecyclerView")//禁用 Lint 检查 去代码警告public void onBindViewHolder(@NonNull final VH vh, final int i) {vh.tv.setText(beanList.get(i % beanList.size()).getMsg());Glide.with(context).load(beanList.get(i%beanList.size()).getImgAddress()).into(vh.iv_picture);vh.ly_view.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (buttonInterface!=null){buttonInterface.onclick(v,i);}}});}@Overridepublic int getItemCount() {return Integer.MAX_VALUE;}static class VH extends RecyclerView.ViewHolder{private final TextView tv;private final ImageView iv_picture;private final LinearLayout ly_view;public VH(@NonNull View itemView) {super(itemView);tv = itemView.findViewById(R.id.tv);iv_picture=itemView.findViewById(R.id.iv_picture);ly_view=itemView.findViewById(R.id.ly_view);}}
}

xml

<LinearLayout xmlns:android=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="120dp"android:orientation="horizontal"android:weightSum="2"tools:context="com.yhhcb.ui.UpRollRecyclerFragment"><LinearLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:padding="10dp"><android.support.v7.widget.RecyclerViewandroid:id="@+id/rv"android:layout_width="match_parent"android:layout_height="wrap_content"/>
</LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"><android.support.v7.widget.RecyclerViewandroid:id="@+id/rv_right"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>
</LinearLayout>

recycleView 的 item xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""android:id="@+id/ly_view"android:layout_width="wrap_content"android:orientation="vertical"android:gravity="center"android:layout_height="120dp"><ImageViewandroid:id="@+id/iv_picture"android:layout_width="60dp"android:layout_height="60dp"android:src="@mipmap/ic_launcher"/><TextViewandroid:id="@+id/tv"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="item"android:gravity="center"android:textColor="@color/text"android:textSize="12sp"/>
</LinearLayout>

javaBean


/*** @author whl* Created on: 3/1/22 3:20 PM* description*/
public class TestBean {private String msg;private String imgAddress;private String titleImageAddress;public TestBean(String msg, String imgAddress,String titleImageAddress) {this.msg = msg;this.imgAddress = imgAddress;this.titleImageAddress=titleImageAddress;}public String getMsg() {return msg == null ? "" : msg;}public void setMsg(String msg) {this.msg = msg;}public String getImgAddress() {return imgAddress == null ? "" : imgAddress;}public void setImgAddress(String imgAddress) {this.imgAddress = imgAddress;}public String getTitleImageAddress() {return titleImageAddress == null ? "" : titleImageAddress;}public void setTitleImageAddress(String titleImageAddress) {this.titleImageAddress = titleImageAddress;}
}

更多推荐

RecycleView实现 viewFlipper 广告条垂直滚动效果(仿叮咚买菜)

本文发布于:2024-03-07 19:28:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1718707.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:买菜   效果   广告   RecycleView   viewFlipper

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!