前几天在博客看到别人实现这个功能,觉得挺有用的,于是自己也试着做了一下。
中间在获取recyclerview scroll距离的时候遇到点问题,后来通过recyclerview.getChildAt(0).getLeft() 来达成,图如下:
具体代码如下:
Activity
package com.example.com.myapplication;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Administrator on 2015/12/3.
*/
public class MyCheckNumberActivity extends AppCompatActivity {
private RecyclerView recycler;
private MyCheckRecyclerAdapter adapter;
private TextView t;
private int maxNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check_number_layout);
recycler = (RecyclerView) findViewById(R.id.recycler);
t = (TextView) findViewById(R.id.text);
recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
recycler.setItemAnimator(new DefaultItemAnimator());
Point p = new Point();
Display display = getWindowManager().getDefaultDisplay();
display.getSize(p);
maxNumber = 7;
ArrayList<String> strings = new ArrayList<String>();
for(int i = 0 ; i < 33 ; i++){
strings.add(""+i);
}
strings.add(0,"");
strings.add(0,"");
strings.add(0,"");
strings.add(strings.size(),"");
strings.add(strings.size(),"");
strings.add(strings.size(),"");
adapter = new MyCheckRecyclerAdapter(this,strings,p.x,maxNumber);
recycler.setAdapter(adapter);
MyCheckScrollListener listener = new MyCheckScrollListener();
listener.setItemWidth(p.x / maxNumber);
recycler.setOnScrollListener(listener);
}
class MyCheckScrollListener extends RecyclerView.OnScrollListener{
private int selectPosition = 0;
private int itemWidth = 0;
private int halfItemWidth = 0;
private int moveX = 0;
private boolean isScrolling = false;
private TextView light;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
//滑动
if(newState == RecyclerView.SCROLL_STATE_DRAGGING){
isScrolling = true;
}
//停止
else if(newState == RecyclerView.SCROLL_STATE_IDLE && isScrolling){
View v = recyclerView.getChildAt(0);
int left = v.getLeft();
int cha = 0;
if(Math.abs(left) < halfItemWidth){
cha = left;
}
else{
cha = itemWidth + left ;
}
recyclerView.smoothScrollBy(cha,0);
moveX += cha;
isScrolling = false;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(!isScrolling){
return;
}
moveX += dx;
int position = getPosition();
if(selectPosition != position){
selectPosition = position;
setHightLight(recyclerView,dx);
t.setText(adapter.getValueByPosition(selectPosition + maxNumber/2));
}
}
private void setItemWidth(int width){
this.itemWidth = width;
halfItemWidth = itemWidth / 2;
}
private void setHightLight(RecyclerView recycler, int dx){
if(light != null){
light.setTextSize(20);
light.setTextColor(MyCheckNumberActivity.this.getResources().getColor(R.color.drak));
}
TextView t;
//如果向右滑动,则高亮部分应为最大item数量/2 +1
if(dx > 0){
t = (TextView) recycler.getChildAt(maxNumber/2 +1).findViewById(R.id.text);
}
//如果向左滑动,则高亮部分应为最大item数量/2
else{
t = (TextView) recycler.getChildAt(maxNumber/2).findViewById(R.id.text);
}
t.setTextSize(25);
t.setTextColor(MyCheckNumberActivity.this.getResources().getColor(R.color.red));
light = t;
}
private int getPosition(){
return ((moveX + halfItemWidth)/itemWidth);
}
}
}
package com.example.com.myapplication;
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.ArrayList;
/**
* Created by Administrator on 2015/12/3.
*/
public class MyCheckRecyclerAdapter extends RecyclerView.Adapter {
private int itemWidth = 0;
private ArrayList<String> ages;
private Context context;
public MyCheckRecyclerAdapter(Context context,ArrayList<String> ages,int screenWidth ,int maxNumber) {
if(maxNumber % 2 == 0){
throw new IllegalArgumentException("maxNumber 必须为奇数");
}
this.context = context;
this.ages = ages;
itemWidth = screenWidth / maxNumber;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.check_number_item,parent,false);
return new CheckViewHolder(v);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
CheckViewHolder ho = (CheckViewHolder)holder;
ViewGroup.LayoutParams params = ho.text.getLayoutParams();
params.width = itemWidth;
ho.text.setLayoutParams(params);
ho.text.setText(ages.get(position));
}
@Override
public int getItemCount() {
return ages.size();
}
public String getValueByPosition(int position){
return ages.get(position);
}
public class CheckViewHolder extends RecyclerView.ViewHolder{
private TextView text;
public CheckViewHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.text);
}
}
}