炫酷-背景圖垂直循環(huán)滾動(dòng)登錄頁,Android RecyclerView實(shí)現(xiàn)

炫酷-背景圖上下循環(huán)滾動(dòng)登錄頁,Android RecyclerView實(shí)現(xiàn)方法

某站的登錄頁背景不停循環(huán)滾動(dòng),和街邊的廣告箱很像,感覺不錯(cuò)我也心動(dòng)了。決定高仿一下,參考了幾篇文章后就動(dòng)手了。
實(shí)現(xiàn)步驟:

1.效果圖展示
2.自定義實(shí)現(xiàn)滾動(dòng)效果RecyclerView
3.適配器Adapter實(shí)現(xiàn)
4.適配器布局文件
5.主程序調(diào)用過程
6.主布局文件
7.總結(jié)

1.效果圖展示

666.gif

2.自定義實(shí)現(xiàn)滾動(dòng)效果RecyclerView

AutoPollRecyclerView.java

public class AutoPollRecyclerView extends RecyclerView {
    private static final long TIME_AUTO_POLL = 16;
    AutoPollTask autoPollTask;
    private boolean running; //標(biāo)示是否正在自動(dòng)輪詢
    private boolean canRun;//標(biāo)示是否可以自動(dòng)輪詢,可在不需要的是否置false

    public AutoPollRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        autoPollTask = new AutoPollTask(this);
    }

    static class AutoPollTask implements Runnable {
        private final WeakReference<AutoPollRecyclerView> mReference;

        //使用弱引用持有外部類引用->防止內(nèi)存泄漏
        public AutoPollTask(AutoPollRecyclerView reference) {
            this.mReference = new WeakReference<AutoPollRecyclerView>(reference);
        }

        @Override
        public void run() {
            AutoPollRecyclerView recyclerView = mReference.get();
            if (recyclerView != null && recyclerView.running && recyclerView.canRun) {
                recyclerView.scrollBy(2, 2);
                recyclerView.postDelayed(recyclerView.autoPollTask, recyclerView.TIME_AUTO_POLL);
            }
        }
    }

    //開啟:如果正在運(yùn)行,先停止->再開啟
    public void start() {
        if (running)
            stop();
        canRun = true;
        running = true;
        postDelayed(autoPollTask, TIME_AUTO_POLL);
    }

    public void stop() {
        running = false;
        removeCallbacks(autoPollTask);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (running)
                    stop();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_OUTSIDE:
                if (canRun)
                    start();
                break;
        }
        //return  false,注釋掉onTouchEvent()方法里面的stop和start方法,則列表自動(dòng)滾動(dòng)且不可觸摸
        return super.onTouchEvent(e);}
}

3.適配器Adapter實(shí)現(xiàn)

AutoPollAdapter.java

public class AutoPollAdapter extends RecyclerView.Adapter<AutoPollAdapter.BaseViewHolder> {
    private final Context mContext;


    public AutoPollAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.auto_list_item, parent, false);
        BaseViewHolder holder = new BaseViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {

    }

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

    class BaseViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public BaseViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_login_bg);

        }
    }

}

4.適配器布局文件

auto_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/iv_login_bg"
    android:layout_width="480dp"
    android:layout_height="2065dp"
    android:scaleType="matrix"
    android:adjustViewBounds="true"
    android:background="@drawable/login_bg01"/>

5.主程序調(diào)用過程

LoginActivity.java

public class LoginActivity extends AppCompatActivity {
    private AutoPollRecyclerView recyclerView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        recyclerView = findViewById(R.id.recyclerview);

        recyclerView.setLayoutManager(
                new LinearLayoutManager(
                        this,
                        //設(shè)置LinearLayoutManager.HORIZONTAL  則水平滾動(dòng)
                        LinearLayoutManager.VERTICAL, false));

            AutoPollAdapter autoPollAdapter = new AutoPollAdapter(getApplicationContext());
            recyclerView.setAdapter(autoPollAdapter);
            //啟動(dòng)滾動(dòng)
            recyclerView.start();

    }
}

6.主布局文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <com.imooc.guessmusic.view.AutoPollRecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/login_dark" />

    <ImageView
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="120dp"
        android:src="@drawable/login_logo" />

    <TextView
        android:layout_width="400dp"
        android:layout_height="60dp"
        android:layout_marginTop="60dp"
        android:layout_gravity="center"
        android:background="@drawable/btn_c"
        android:gravity="center"
        android:textSize="22sp"
        android:textStyle="bold"
        android:textColor="@color/black"
        android:text="微信登錄" />
    <TextView
        android:layout_width="400dp"
        android:layout_height="60dp"
        android:layout_marginTop="150dp"
        android:layout_gravity="center"
        android:background="@drawable/btn_c_b"
        android:gravity="center"
        android:textSize="22sp"
        android:textColor="@color/text_white"
        android:text="手機(jī)號(hào)碼快捷登錄" />
</FrameLayout>

7.總結(jié)

Demo里面使用了沉浸式效果,如何實(shí)現(xiàn)請(qǐng)參考下面的文章。
Android-沉浸式的實(shí)現(xiàn)
Demo里面使用了今日頭條適配方案能適配97%的手機(jī),3%劉海屏與水滴屏未做適配,是如何實(shí)現(xiàn)請(qǐng)參考下面的文章。
今日頭條的屏幕適配方案,簡(jiǎn)易使用
核心代碼就是自定義的RecyclerView代碼,如果你想做其它效果的滾動(dòng)可以參考下面的文章。
Android實(shí)現(xiàn)類似中獎(jiǎng)信息自動(dòng)滾動(dòng)效果

Android RecyclerView打造自動(dòng)循環(huán)效果

原代碼獻(xiàn)上:https://pan.baidu.com/s/18sy-Gvjav8Dz2PwctrDcFw
提取碼:sr0i

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容