开篇废话
产品让我们将“我的”页面改版,上面加了一个广告条,非常类似淘宝“我的”页面的广告条,然后就自己写了一个,方法比较一般,如果大家有更好的办法请留言给我,谢谢。 滚动图文条之GitHub地址 ,帮我点个Star。
滚动图文条
大概效果就是下图这样。
滚动图文条
思路
- 写一个不可用手滑动的RecyclerView
- 使用Handler定时RecyclerView自动滑动到下一个Item
- 使用smoothScrollToPosition使其平滑地滑动
开始工作
做一些基本工作
写一个AdModel类。
public class AdModel { public String title; public String content; public AdModel(String title, String content) { this.title = title; this.content = content; } }
写一些item_ad布局。
写AdAdapter类。
public class AdAdapter extends RecyclerView.Adapter{ private Context mContext; private OnItemClickListener onItemClickListener; private LayoutInflater mInflater; private List mDataList; public AdAdapter(Context context, List datas) { this.mContext = context; mDataList = datas; mInflater = LayoutInflater.from(context); } @Override public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) { View itemView = mInflater.inflate(R.layout.item_ad, null); return new ViewHolder(itemView); } @Override public void onBindViewHolder(ViewHolder holder, final int p) { if (mDataList == null || mDataList.size() ==0){ return; } if (holder != null) { final int position = p % mDataList.size(); holder.mTvTitle.setText(mDataList.get(position).title); holder.mTvContent.setText(mDataList.get(position).content); holder.mIvIcon.setImageResource(R.drawable.icon); holder.viewRoot.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (onItemClickListener != null) { onItemClickListener.onItemClick(v, position); } } }); } } public void setOnItemClickListener(OnItemClickListener clickListener) { this.onItemClickListener = clickListener; } @Override public int getItemCount() { return mDataList == null ? 0 : Integer.MAX_VALUE; } public class ViewHolder extends RecyclerView.ViewHolder { public View viewRoot; public TextView mTvTitle; public TextView mTvContent; public ImageView mIvIcon; public ViewHolder(View itemView) { super(itemView); viewRoot = itemView.findViewById(R.id.layout); mTvTitle = itemView.findViewById(R.id.tv_title); mTvContent = itemView.findViewById(R.id.tv_content); mIvIcon = itemView.findViewById(R.id.iv_icon); } } /** * RecyclerView的item点击监听接口 */ public interface OnItemClickListener { void onItemClick(View v, int position); } }
页面activity_main布局。
重点代码
在MainActivity中写如下代码。
package com.cc.scrolladbar; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearSmoothScroller; import android.support.v7.widget.RecyclerView; import android.util.DisplayMetrics; import android.view.View; import java.util.ArrayList; import java.util.List; /** * Created by guoshichao on 2018/8/16 * QQ:1169380200 */ public class MainActivity extends AppCompatActivity { private static final int SCROLL_AD = 0;//会员轮播广告 public static final int DEFAULT_SCROLL_INTERVAL = 3000;//会员轮播广告间隔时间 public static final int DEFAULT_SCROLL_ANIMATION_TIME = 500;//会员轮播广告动画时长 private RecyclerView mRvAd; private AdAdapter mAdapter; private ListmAdList; private AdHandler mHandler; private int nowScrollPosition = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initList(); } @Override public void onStart() { super.onStart(); scrollVipAdOnce(nowScrollPosition);//防止滑动一半切到别的页面使滑动完成 if (mHandler != null) { sendScrollMessage(DEFAULT_SCROLL_INTERVAL); } } @Override public void onStop() { super.onStop(); if (mHandler != null) { mHandler.removeMessages(SCROLL_AD); } } private void initList() { mAdList = new ArrayList<>(); mAdList.add(new AdModel("第一条广告标题", "我是第一条广告的内容哦~")); mAdList.add(new AdModel("第二条广告标题", "我是第二条广告的内容哦~")); mAdList.add(new AdModel("第三条广告标题", "我是第三条广告的内容哦~")); LinearLayoutManager manager = new LinearLayoutManager(this) { @Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) { LinearSmoothScroller smoothScroller = new LinearSmoothScroller(recyclerView.getContext()) { // 为了平滑滑动返回:滑过1px时经历的时间(ms)。 @Override protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { return (float) (DEFAULT_SCROLL_ANIMATION_TIME / displayMetrics.densityDpi); } }; smoothScroller.setTargetPosition(position); startSmoothScroll(smoothScroller); } }; mRvAd = (RecyclerView) findViewById(R.id.rv_ad); mRvAd.setLayoutManager(manager); mAdapter = new AdAdapter(this, mAdList); mRvAd.setAdapter(mAdapter); mAdapter.setOnItemClickListener(new AdAdapter.OnItemClickListener() { @Override public void onItemClick(View v, int position) { //点击跳转到指定广告页 } }); mHandler = new AdHandler(); sendScrollMessage(DEFAULT_SCROLL_INTERVAL); } private void scrollVipAdOnce(int position) { if (mAdList != null && mAdList.size() > 1) { //平滑滑动到指定位置 mRvAd.smoothScrollToPosition(position); } } private void sendScrollMessage(long delayMillis) { mHandler.removeMessages(SCROLL_AD); mHandler.sendEmptyMessageDelayed(SCROLL_AD, delayMillis); } private class AdHandler extends Handler { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case SCROLL_AD: nowScrollPosition++; scrollVipAdOnce(nowScrollPosition); sendScrollMessage(DEFAULT_SCROLL_INTERVAL); break; default: break; } } } }
重点分析
其中有一段代码比较重要。
LinearLayoutManager manager = new LinearLayoutManager(this) { @Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) { LinearSmoothScroller smoothScroller = new LinearSmoothScroller(recyclerView.getContext()) { // 为了平滑滑动返回:滑过1px时经历的时间(ms)。 @Override protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { return (float) (DEFAULT_SCROLL_ANIMATION_TIME / displayMetrics.densityDpi); } }; smoothScroller.setTargetPosition(position); startSmoothScroll(smoothScroller); } };
这里是为了平滑滑动。因为距离比较短小或者别的原因,mRvAd.smoothScrollToPosition(position)无法使其平滑地滑动。故加以上代码。
我的完成图如下。
滚动图文条
写在后面
这个Demo比较简单,没什么技术难点,如果还是有些不懂的,可以留言,我在文中可以做更多的解释。如果有大佬有更好的解决方案,望指教。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。