为了账号安全,请及时绑定邮箱和手机立即绑定

滚动时获取RecycleView的中心可见项

滚动时获取RecycleView的中心可见项

繁星coding 2019-11-25 13:14:31
我想在上绘制一条中心线RecycleView,然后在滚动时(以及向左或向右移动)获取中心项,这是我尝试绘制水平线的尝试RecycleView:    HorizontalAdapter adapter = new HorizontalAdapter(data);    LinearLayoutManager layoutManager            = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);    recycleView.setLayoutManager(layoutManager);    recycleView.setAdapter(adapter);有什么办法知道哪个项目移动到中心RecycleView?以及如何RecycleView只向左或向右滚动一个位置?
查看完整描述

3 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

我做了这样的事情。我完全可以满足您的需求。首先,这是我的算法的工作方式 

这是我的recyclerView Adapter


public class DateAdapter extends RecyclerView.Adapter<DateAdapter.DateViewHolder> {

private ArrayList<LabelerDate> dateDataList;



private static final int VIEW_TYPE_PADDING = 1;

private static final int VIEW_TYPE_ITEM = 2;

private int paddingWidthDate = 0;


private int selectedItem = -1;


public DateAdapter(ArrayList<LabelerDate> dateData, int paddingWidthDate) {

    this.dateDataList = dateData;

    this.paddingWidthDate = paddingWidthDate;


}



@Override

public DateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    if (viewType == VIEW_TYPE_ITEM) {

        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_date,

                parent, false);

        return new DateViewHolder(view);

    } else {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_padding,

                parent, false);


        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view.getLayoutParams();

        layoutParams.width = paddingWidthDate;

        view.setLayoutParams(layoutParams);

        return new DateViewHolder(view);

    }

}


@Override

public void onBindViewHolder(DateViewHolder holder, int position) {

    LabelerDate labelerDate = dateDataList.get(position);

    if (getItemViewType(position) == VIEW_TYPE_ITEM) {

        if(labelerDate.dateType.equals(BirthDayActivity.DateType.C31))

                holder.tvDate.setText(String.valueOf(labelerDate.valueDate));

                holder.tvDate.setVisibility(View.VISIBLE);

                holder.imgSmall.setVisibility(View.VISIBLE);


        if (position == selectedItem) {

            holder.tvDate.setTextColor(Color.parseColor("#094673"));

            holder.tvDate.setTextSize(35);

            holder.imgSmall.setBackgroundResource(R.color.textviewbold);


        } else {

            holder.tvDate.setTextColor(Color.GRAY);

            holder.tvDate.setTextSize(35);

            holder.imgSmall.setBackgroundResource(R.color.gray);

        }

    }

}


public void setSelecteditem(int selecteditem) {

    this.selectedItem = selecteditem;

    notifyDataSetChanged();

}


@Override

public int getItemCount() {

    return dateDataList.size();

}


@Override

public int getItemViewType(int position) {

    LabelerDate labelerDate = dateDataList.get(position);

    if (labelerDate.dateType.equals(BirthDayActivity.DateType.NONE)) {

        return VIEW_TYPE_PADDING;

    }

    return VIEW_TYPE_ITEM;

}



public class DateViewHolder extends RecyclerView.ViewHolder {

    public TextView tvDate;

    public ImageView imgSmall;


    public DateViewHolder(View itemView) {

        super(itemView);

        tvDate = (TextView) itemView.findViewById(R.id.tvNumberDate);

        imgSmall = (ImageView) itemView.findViewById(R.id.small_marked_dob);

    }

}}

这是最重要的算法:


public void getRecyclerviewDate() {

    recyclerViewDate = (RecyclerView) findViewById(R.id.recyclerViewDay);

    ViewTreeObserver vtoDate = recyclerViewDate.getViewTreeObserver();

    vtoDate.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override

        public boolean onPreDraw() {

            recyclerViewDate.getViewTreeObserver().removeOnPreDrawListener(this);

            finalWidthDate = recyclerViewDate.getMeasuredWidth();

            itemWidthDate = getResources().getDimension(R.dimen.item_dob_width);

            paddingDate = (finalWidthDate - itemWidthDate) / 2;

            firstItemWidthDate = paddingDate ;

            allPixelsDate = 0;


            final LinearLayoutManager dateLayoutManager = new LinearLayoutManager(getApplicationContext());

            dateLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

            recyclerViewDate.setLayoutManager(dateLayoutManager);

            recyclerViewDate.addOnScrollListener(new RecyclerView.OnScrollListener() {

                @Override

                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

                    super.onScrollStateChanged(recyclerView, newState);

                    synchronized (this) {

                         if(newState == RecyclerView.SCROLL_STATE_IDLE){           

                            calculatePositionAndScrollDate(recyclerView);

                        }

                    }


                }


                @Override

                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                    super.onScrolled(recyclerView, dx, dy);

                    allPixelsDate += dx;

                }

            });

            if (labelerDates == null)

                labelerDates = new ArrayList<>();

            labelerDates.addAll(genLabelerDate(currentMonth, currentYear));

            dateAdapter = new DateAdapter(labelerDates, (int) firstItemWidthDate);

            recyclerViewDate.setAdapter(dateAdapter);

            return true;

        }

    });

}

/* this if most important, if expectedPositionDate < 0 recyclerView will return to nearest item*/


private void calculatePositionAndScrollDate(RecyclerView recyclerView) {

    int expectedPositionDate = Math.round((allPixelsDate + paddingDate - firstItemWidthDate) / itemWidthDate);


    if (expectedPositionDate == -1) {

        expectedPositionDate = 0;

    } else if (expectedPositionDate >= recyclerView.getAdapter().getItemCount() - 2) {

        expectedPositionDate--;

    }

    scrollListToPositionDate(recyclerView, expectedPositionDate);


}

/* this if most important, if expectedPositionDate < 0 recyclerView will return to nearest item*/

private void scrollListToPositionDate(RecyclerView recyclerView, int expectedPositionDate) {

    float targetScrollPosDate = expectedPositionDate * itemWidthDate + firstItemWidthDate - paddingDate;

    float missingPxDate = targetScrollPosDate - allPixelsDate;

    if (missingPxDate != 0) {

        recyclerView.smoothScrollBy((int) missingPxDate, 0);

    }

}

private void setDateValue() {

    int expectedPositionDateColor = Math.round((allPixelsDate + paddingDate - firstItemWidthDate) / itemWidthDate);

    setColorDate = expectedPositionDateColor + 1;

    //set color here

    dateAdapter.setSelecteditem(setColorDate);

}

 @Override

protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {

    super.onRestoreInstanceState(savedInstanceState);   

    allPixelsDate = savedInstanceState.getFloat(BUNDLE_LIST_PIXELS_DATE);

    allPixelsDateChanged = savedInstanceState.getFloat(BUNDLE_LIST_PIXELS_DATE_CHANGED);

}


@Override

protected void onSaveInstanceState(@NonNull Bundle outState) {

    super.onSaveInstanceState(outState);

    outState.putFloat(BUNDLE_LIST_PIXELS_DATE, allPixelsDate);

    outState.putFloat(BUNDLE_LIST_PIXELS_DATE_CHANGED, allPixelsDateChanged);

}


看这个视频链接,这是我的应用程序演示


查看完整回答
反对 回复 2019-11-25
  • 3 回答
  • 0 关注
  • 472 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信