使recyclerview滚动到下一张卡片(水平)并实现自动滚动

时间:2018-04-05 05:28:14

标签: android android-recyclerview

我有应用程序,我想实现Dineout应用程序的滚动。基本上Dineout在垂直回收器视图中有一个cardview,当用户滑动时它会滚动到下一张卡,甚至在2秒后以无限方式自动发生。

我能够使用一些随机数据制作垂直回收者视图,但是当用户滑动时我无法进入第二张卡片,而且我甚至想要添加自动功能。

我附上了源代码。

MainActivity.java

package com.example.root.recyclertutorial;

import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {



    private RecyclerView mRecyclerView;
    private SubActivityAdapter mAdapter;
    final int duration = 50;
    final int pixelsToMove = 200;
    private final Handler mHandler = new Handler(Looper.getMainLooper());
    private RecyclerView.LayoutManager mLayoutManager;
    private final Runnable SCROLLING_RUNNABLE = new Runnable() {

        @Override
        public void run() {
            mRecyclerView.smoothScrollBy(pixelsToMove, 0);
            mHandler.postDelayed(this, duration);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecyclerView = (RecyclerView) findViewById(R.id.hrlist_recycler_view);

        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setHasFixedSize(true);
        mAdapter = new SubActivityAdapter(this, getData());
        mRecyclerView.setAdapter(mAdapter);
//        RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(this) {
//            @Override protected int getVerticalSnapPreference() {
//                return LinearSmoothScroller.SNAP_TO_START;
//            }
//        };
//        smoothScroller.setTargetPosition(1);
//        layoutManager.startSmoothScroll(smoothScroller);
//        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, 200, dy);
//                int lastItem = layoutManager.findLastCompletelyVisibleItemPosition();
//                if(lastItem == layoutManager
//                        .getItemCount()-1){
//                    mHandler.removeCallbacks(SCROLLING_RUNNABLE);
//                    Handler postHandler = new Handler();
//                    postHandler.postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            mRecyclerView.setAdapter(null);
//                            mRecyclerView.setAdapter(mAdapter);
//                            mHandler.postDelayed(SCROLLING_RUNNABLE, 50000);
//                        }
//                    }, 50000);
//                }
//            }
//        });
//        mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);

        //Layout manager for the Recycler View
//        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
//        mRecyclerView.setLayoutManager(mLayoutManager);
    }

    public static List<SubActivityData> getData() {
        List<SubActivityData> subActivityData = new ArrayList<>();
        String[] cardTitle = {
                "Card 1",
                "Card 2",
                "Card 3",
                "Card 4",
                "Card 5",
                "Card 6",
        };
        for (int i = 0; i < cardTitle.length; i++) {
            SubActivityData current = new SubActivityData();
            current.cardTitle = cardTitle[i];
            subActivityData.add(current);
        }

        return subActivityData;
    }
}

SubActivityAdapter.java

package com.example.root.recyclertutorial;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

/**
 * Created by Abhishek on 20-03-2015.
 */
public class SubActivityAdapter extends RecyclerView.Adapter<SubActivityAdapter.SubActivityViewHolder> {

    private final LayoutInflater inflater;
    List<SubActivityData> subActivityData = Collections.EMPTY_LIST;

    public SubActivityAdapter(Context context, List<SubActivityData> subActivityData) {
        inflater = LayoutInflater.from(context);
        this.subActivityData = subActivityData;
    }


    @Override
    public SubActivityViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.custom_card, parent, false);
        SubActivityViewHolder subActivityViewHolder = new SubActivityViewHolder(view);
        return subActivityViewHolder;
    }

    @Override
    public void onBindViewHolder(SubActivityViewHolder holder, int position) {
        SubActivityData currentCard = subActivityData.get(position);
        holder.title.setText(currentCard.cardTitle);

    }

    @Override
    public int getItemCount() {
        return subActivityData.size();
    }

    class SubActivityViewHolder extends RecyclerView.ViewHolder {

        TextView title;

        public SubActivityViewHolder(View itemView) {
            super(itemView);

            title = (TextView) itemView.findViewById(R.id.card_text);
        }
    }
}

SubActivityData.java

package com.example.root.recyclertutorial;

/**
 * Created by root on 4/4/18.
 */

public class SubActivityData {

    public String cardTitle;

    void SubActivityData(){
        this.cardTitle = cardTitle;
    }


}

CustomCard.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp"
    android:tag="contains Cards main Container">

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="10dp"
        app:cardBackgroundColor="#B6B6B6">

        <TextView
            android:id="@+id/card_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:padding="20dp"
            android:text="Card 1"
            android:textSize="30sp" />

    </android.support.v7.widget.CardView>
</LinearLayout>

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Horizontal Card List"
        android:textSize="16sp"
        android:padding="3dp"
        android:background="#CCCCCC" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/hrlist_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="horizontal" />

</LinearLayout>

如果有人能回答,那将是非常有帮助的。基本要求是我想在用户滚动时滚动到下一张卡片。 2秒后应该有一个自动滚动到下一张卡加上无限循环。

0 个答案:

没有答案