我尝试将水平RecylerView转换为ViewPager。
(类似于水平RecylerView的工作类似于ViewPager)
我正在处理已经在使用水平RecylerView的现有项目,并且按照要求可以完美地工作,但是我需要更改其设计,因为RecylerView是水平的,一次只能显示一项,以便用户更好地理解,我需要添加项目下方的一种线性布局,用于显示RecylerView中项目总数的指示器(就像ViewPager一样)。
指标的目的,它显示RecylerView中的当前现有项目。
我的问题:在水平recylerview中滚动时如何在指示器中获取项目的当前位置?
尝试并错误:
我不知道滚动时如何获取当前项目的位置?
(我在点击项目时获得项目的位置)
我可以使用viewpager而不是水平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。