我尝试将水平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;
}
}
答案 0 :(得分:1)
我找到了解决方案,这两种方法让我的工作变得轻松..
<a>
注意:当我们只需要显示一个项目时,我们可以使用此代替viewpager。