Android仿制淘寶滾動圖文條的示例代碼
開篇廢話
產品讓我們將“我的”頁面改版,上面加了一個廣告條,非常類似淘寶“我的”頁面的廣告條,然后就自己寫了一個,方法比較一般,如果大家有更好的辦法請留言給我,謝謝。 滾動圖文條之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布局。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="50dp" android:minHeight="50dp" android:gravity="center_vertical" android:orientation="horizontal" android:background="@null" tools:background="@color/black"> <LinearLayout android:id="@+id/ll_ad" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:orientation="vertical"> <TextView android:id="@+id/tv_title" tools:text="會員身份0元搶" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:lines="1" android:textColor="@color/white" android:textSize="12sp" /> <TextView android:id="@+id/tv_content" tools:text="送你體驗會員" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:lines="1" android:textColor="@color/white" android:textSize="10sp" /> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/> <ImageView android:id="@+id/iv_icon" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginRight="15dp" android:src="@drawable/icon"/> </LinearLayout>
寫AdAdapter類。
public class AdAdapter extends RecyclerView.Adapter<AdAdapter.ViewHolder> { private Context mContext; private OnItemClickListener onItemClickListener; private LayoutInflater mInflater; private List<AdModel> mDataList; public AdAdapter(Context context, List<AdModel> 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點擊監(jiān)聽接口 */ public interface OnItemClickListener { void onItemClick(View v, int position); } }
頁面activity_main布局。
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.cc.scrolladbar.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_ad" android:layout_width="0dp" android:layout_height="50dp" android:layout_marginEnd="20dp" android:layout_marginStart="20dp" android:layout_marginTop="60dp" android:nestedScrollingEnabled="false" android:background="@color/black" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
重點代碼
在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 List<AdModel> mAdList; 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時經(jīng)歷的時間(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時經(jīng)歷的時間(ms)。 @Override protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { return (float) (DEFAULT_SCROLL_ANIMATION_TIME / displayMetrics.densityDpi); } }; smoothScroller.setTargetPosition(position); startSmoothScroll(smoothScroller); } };
這里是為了平滑滑動。因為距離比較短小或者別的原因,mRvAd.smoothScrollToPosition(position)無法使其平滑地滑動。故加以上代碼。
我的完成圖如下。
滾動圖文條
寫在后面
這個Demo比較簡單,沒什么技術難點,如果還是有些不懂的,可以留言,我在文中可以做更多的解釋。如果有大佬有更好的解決方案,望指教。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android?Flutter實現(xiàn)創(chuàng)意時鐘的示例代碼
時鐘這個東西很奇妙,總能當做創(chuàng)意實現(xiàn)的入口。這篇文章主要介紹了如何通過Android?Flutter實現(xiàn)一個創(chuàng)意時鐘,感興趣的小伙伴可以了解一下2023-03-03解決Android啟動APP的一瞬間系統(tǒng)欄會變成藍色問題
這篇文章主要介紹了解決Android啟動APP的一瞬間系統(tǒng)欄會變成藍色問題,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06Android判斷用戶2G/3G/4G移動數(shù)據(jù)網(wǎng)絡
這篇文章主要介紹了Android判斷用戶2G/3G/4G移動數(shù)據(jù)網(wǎng)絡的方法,感興趣的小伙伴們可以參考一下2015-12-12詳解Android中的ActivityThread和APP啟動過程
ActivityThread就是我們常說的主線程或UI線程,ActivityThread的main方法是整個APP的入口,本篇深入學習下ActivityThread,順便了解下APP和Activity的啟動過程。2021-06-06