Horizontal RecylerView look as ViewPager

蹲街弑〆低调 提交于 2020-01-31 19:02:11

问题


I try to convert horizontal RecylerView into ViewPager.
(work like horizontal RecylerView look like ViewPager)

I'm working on existing project where horizontal RecylerView is already in use and work perfectly as per requirement, but i need to change design of it as RecylerView is horizontal only one item at time should be shown so for user better understanding i need to add one linear layout below item which shows indicator (just like ViewPager) of total items in RecylerView. purpose of indicator, it shows current existing item in RecylerView.

My question: How to get current position of item in indicator while scrolling in horizontal recylerview?

Try and Error:

I I don't know how to get position of current item while scrolling?
(i get the position of item while clicking on item )

I can use viewpager instead of horizontal recylerview, but use is multiple i need to change it in so many places(time and efforts).
(I already set indicator as per total item but i fail to get position of item)

Note: at a time only one item show

Thanks in Advance.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#f7eaf9">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fd7d7d">
    </android.support.v7.widget.RecyclerView>

    <LinearLayout
        android:id="@+id/linlay_pager"
        android:layout_margin="10dp"
        android:layout_below="@+id/recycler_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal"/>
</RelativeLayout>

MainActivity.java

package com.horizontalrecyclerview;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SnapHelper;
import android.view.Gravity;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

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

public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private Activity mActivity;
    private Button pre;
    private RelativeLayout mRelativeLayout;
    private RecyclerView mRecyclerView;
    private RecyclerView.LayoutManager mLayoutManager;
    private RecyclerView.Adapter mAdapter;
    private Random mRandom = new Random();
    private LinearLayout linlay_pager;
    private VectorDrawableCompat mVectorPagerFillCircle;
    private VectorDrawableCompat mVectorPagerCircle;

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int receivedColor = intent.getIntExtra("Color", Color.WHITE);
            mRelativeLayout.setBackgroundColor(receivedColor);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        linlay_pager= (LinearLayout) findViewById(R.id.linlay_pager);
        mVectorPagerFillCircle = VectorDrawableCompat.create(getResources(), R.drawable.ic_pager_fill_circle, null);
        mVectorPagerCircle = VectorDrawableCompat.create(getResources(), R.drawable.ic_pager_circle, null);
        mContext = getApplicationContext();
        mActivity = MainActivity.this;
        LocalBroadcastManager.getInstance(mContext).registerReceiver(
                mBroadcastReceiver,
                new IntentFilter("BROADCAST_COLOR")
        );
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        SnapHelper snapHelper = new GravitySnapHelper(Gravity.START);
        snapHelper.attachToRecyclerView(mRecyclerView);

        mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);

        List<String> colors = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            colors.add(getRandomHSVColor() + "");
        }

        LinearLayout.LayoutParams imParams = new LinearLayout.LayoutParams(ViewPager.LayoutParams.WRAP_CONTENT, ViewPager.LayoutParams.WRAP_CONTENT);
        for (int i = 0; i < 5; i++) {
            ImageView mImageCirclePager = new ImageView(this);
            if (i == 0) {
                mImageCirclePager.setImageDrawable(mVectorPagerFillCircle);
            } else {
                mImageCirclePager.setImageDrawable(mVectorPagerCircle);
            }
            linlay_pager.addView(mImageCirclePager, imParams);
        }

        mAdapter = new ColorsAdapter(mContext, colors);
        mRecyclerView.setAdapter(mAdapter);
    }

    public int getRandomHSVColor() {
        int hue = mRandom.nextInt(361);
        float saturation = 1.0f;
        float value = 1.0f;
        int alpha = 255;
        int color = Color.HSVToColor(alpha, new float[]{hue, saturation, value});
        return color;
    }
}

回答1:


I found solution these two methods make my work easy..

 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                int pos = ((LinearLayoutManager)(recyclerView.getLayoutManager())).findFirstVisibleItemPosition();
                btnAction(pos,5);
            }
        }); 




private void btnAction(int position, int bannerListSize) {
            for (int i = 0; i < bannerListSize; i++) {
                ImageView imageView = (ImageView) linlay_pager.getChildAt(i);
                if (i == position) {
                    imageView.setImageDrawable(mVectorPagerFillCircle);
                } else {
                    imageView.setImageDrawable(mVectorPagerCircle);
                }
            }
        }

Note: We can use this instead of viewpager specially when we need to show only one item.



来源:https://stackoverflow.com/questions/43109553/horizontal-recylerview-look-as-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!