水平 RecyclerView 看起来像 ViewPager
Horizontal RecylerView look as ViewPager
我尝试将水平 RecylerView 转换为 ViewPager。
(像水平 RecylerView 一样工作看起来像 ViewPager)
我正在处理现有项目,其中水平 RecylerView 已经在使用中并且根据要求完美运行,但我需要更改它的设计,因为 RecylerView 是水平的,一次应该只显示一个项目,以便用户更好理解我需要在项目下方添加一个线性布局,它显示 RecylerView 中总项目的指示器(就像 ViewPager)。
指示器的用途,它显示 RecylerView 中当前存在的项目。
我的问题:如何在水平回收视图中滚动时获取指示器中项目的当前位置?
试错:
我不知道如何在滚动时获取当前项目的位置?
(我在点击项目时得到了项目的位置)
我可以使用 viewpager 而不是 horizontal recylerview,但用途是多种我需要在很多地方(时间和精力)进行更改。
(我已经根据总项目设置了指标,但我无法获得项目的位置)
注意: 一次只显示一项
提前致谢。
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;
}
}
我找到了解决方案,这两种方法让我的工作变得轻松..
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);
}
}
}
注意:当我们只需要显示一个项目时,我们可以用它代替viewpager。
我尝试将水平 RecylerView 转换为 ViewPager。
(像水平 RecylerView 一样工作看起来像 ViewPager)
我正在处理现有项目,其中水平 RecylerView 已经在使用中并且根据要求完美运行,但我需要更改它的设计,因为 RecylerView 是水平的,一次应该只显示一个项目,以便用户更好理解我需要在项目下方添加一个线性布局,它显示 RecylerView 中总项目的指示器(就像 ViewPager)。 指示器的用途,它显示 RecylerView 中当前存在的项目。
我的问题:如何在水平回收视图中滚动时获取指示器中项目的当前位置?
试错:
我不知道如何在滚动时获取当前项目的位置?
(我在点击项目时得到了项目的位置)
我可以使用 viewpager 而不是 horizontal recylerview,但用途是多种我需要在很多地方(时间和精力)进行更改。
(我已经根据总项目设置了指标,但我无法获得项目的位置)
注意: 一次只显示一项
提前致谢。
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;
}
}
我找到了解决方案,这两种方法让我的工作变得轻松..
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);
}
}
}
注意:当我们只需要显示一个项目时,我们可以用它代替viewpager。