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

我想在上绘制一条中心线RecycleView,然后在滚动时(以及向左或向右移动)获取中心项,

这是我尝试绘制水平线的尝试RecycleView:


    HorizontalAdapter adapter = new HorizontalAdapter(data);

    LinearLayoutManager layoutManager

            = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

    recycleView.setLayoutManager(layoutManager);

    recycleView.setAdapter(adapter);

有什么办法知道哪个项目移动到中心RecycleView?以及如何RecycleView只向左或向右滚动一个位置?


繁星coding
浏览 473回答 3
3回答

喵喔喔

我做了这样的事情。我完全可以满足您的需求。首先,这是我的算法的工作方式&nbsp;这是我的recyclerView Adapterpublic 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) {&nbsp; &nbsp; this.dateDataList = dateData;&nbsp; &nbsp; this.paddingWidthDate = paddingWidthDate;}@Overridepublic DateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {&nbsp; &nbsp; if (viewType == VIEW_TYPE_ITEM) {&nbsp; &nbsp; &nbsp; &nbsp; final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_date,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent, false);&nbsp; &nbsp; &nbsp; &nbsp; return new DateViewHolder(view);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_padding,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent, false);&nbsp; &nbsp; &nbsp; &nbsp; RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view.getLayoutParams();&nbsp; &nbsp; &nbsp; &nbsp; layoutParams.width = paddingWidthDate;&nbsp; &nbsp; &nbsp; &nbsp; view.setLayoutParams(layoutParams);&nbsp; &nbsp; &nbsp; &nbsp; return new DateViewHolder(view);&nbsp; &nbsp; }}@Overridepublic void onBindViewHolder(DateViewHolder holder, int position) {&nbsp; &nbsp; LabelerDate labelerDate = dateDataList.get(position);&nbsp; &nbsp; if (getItemViewType(position) == VIEW_TYPE_ITEM) {&nbsp; &nbsp; &nbsp; &nbsp; if(labelerDate.dateType.equals(BirthDayActivity.DateType.C31))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.tvDate.setText(String.valueOf(labelerDate.valueDate));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.tvDate.setVisibility(View.VISIBLE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.imgSmall.setVisibility(View.VISIBLE);&nbsp; &nbsp; &nbsp; &nbsp; if (position == selectedItem) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.tvDate.setTextColor(Color.parseColor("#094673"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.tvDate.setTextSize(35);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.imgSmall.setBackgroundResource(R.color.textviewbold);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.tvDate.setTextColor(Color.GRAY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.tvDate.setTextSize(35);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.imgSmall.setBackgroundResource(R.color.gray);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}public void setSelecteditem(int selecteditem) {&nbsp; &nbsp; this.selectedItem = selecteditem;&nbsp; &nbsp; notifyDataSetChanged();}@Overridepublic int getItemCount() {&nbsp; &nbsp; return dateDataList.size();}@Overridepublic int getItemViewType(int position) {&nbsp; &nbsp; LabelerDate labelerDate = dateDataList.get(position);&nbsp; &nbsp; if (labelerDate.dateType.equals(BirthDayActivity.DateType.NONE)) {&nbsp; &nbsp; &nbsp; &nbsp; return VIEW_TYPE_PADDING;&nbsp; &nbsp; }&nbsp; &nbsp; return VIEW_TYPE_ITEM;}public class DateViewHolder extends RecyclerView.ViewHolder {&nbsp; &nbsp; public TextView tvDate;&nbsp; &nbsp; public ImageView imgSmall;&nbsp; &nbsp; public DateViewHolder(View itemView) {&nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; tvDate = (TextView) itemView.findViewById(R.id.tvNumberDate);&nbsp; &nbsp; &nbsp; &nbsp; imgSmall = (ImageView) itemView.findViewById(R.id.small_marked_dob);&nbsp; &nbsp; }}}这是最重要的算法:public void getRecyclerviewDate() {&nbsp; &nbsp; recyclerViewDate = (RecyclerView) findViewById(R.id.recyclerViewDay);&nbsp; &nbsp; ViewTreeObserver vtoDate = recyclerViewDate.getViewTreeObserver();&nbsp; &nbsp; vtoDate.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public boolean onPreDraw() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recyclerViewDate.getViewTreeObserver().removeOnPreDrawListener(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finalWidthDate = recyclerViewDate.getMeasuredWidth();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemWidthDate = getResources().getDimension(R.dimen.item_dob_width);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; paddingDate = (finalWidthDate - itemWidthDate) / 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firstItemWidthDate = paddingDate ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; allPixelsDate = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final LinearLayoutManager dateLayoutManager = new LinearLayoutManager(getApplicationContext());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dateLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recyclerViewDate.setLayoutManager(dateLayoutManager);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recyclerViewDate.addOnScrollListener(new RecyclerView.OnScrollListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onScrollStateChanged(RecyclerView recyclerView, int newState) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.onScrollStateChanged(recyclerView, newState);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; synchronized (this) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(newState == RecyclerView.SCROLL_STATE_IDLE){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; calculatePositionAndScrollDate(recyclerView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onScrolled(RecyclerView recyclerView, int dx, int dy) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.onScrolled(recyclerView, dx, dy);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; allPixelsDate += dx;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (labelerDates == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; labelerDates = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; labelerDates.addAll(genLabelerDate(currentMonth, currentYear));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dateAdapter = new DateAdapter(labelerDates, (int) firstItemWidthDate);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recyclerViewDate.setAdapter(dateAdapter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}/* this if most important, if expectedPositionDate < 0 recyclerView will return to nearest item*/private void calculatePositionAndScrollDate(RecyclerView recyclerView) {&nbsp; &nbsp; int expectedPositionDate = Math.round((allPixelsDate + paddingDate - firstItemWidthDate) / itemWidthDate);&nbsp; &nbsp; if (expectedPositionDate == -1) {&nbsp; &nbsp; &nbsp; &nbsp; expectedPositionDate = 0;&nbsp; &nbsp; } else if (expectedPositionDate >= recyclerView.getAdapter().getItemCount() - 2) {&nbsp; &nbsp; &nbsp; &nbsp; expectedPositionDate--;&nbsp; &nbsp; }&nbsp; &nbsp; scrollListToPositionDate(recyclerView, expectedPositionDate);}/* this if most important, if expectedPositionDate < 0 recyclerView will return to nearest item*/private void scrollListToPositionDate(RecyclerView recyclerView, int expectedPositionDate) {&nbsp; &nbsp; float targetScrollPosDate = expectedPositionDate * itemWidthDate + firstItemWidthDate - paddingDate;&nbsp; &nbsp; float missingPxDate = targetScrollPosDate - allPixelsDate;&nbsp; &nbsp; if (missingPxDate != 0) {&nbsp; &nbsp; &nbsp; &nbsp; recyclerView.smoothScrollBy((int) missingPxDate, 0);&nbsp; &nbsp; }}private void setDateValue() {&nbsp; &nbsp; int expectedPositionDateColor = Math.round((allPixelsDate + paddingDate - firstItemWidthDate) / itemWidthDate);&nbsp; &nbsp; setColorDate = expectedPositionDateColor + 1;&nbsp; &nbsp; //set color here&nbsp; &nbsp; dateAdapter.setSelecteditem(setColorDate);}&nbsp;@Overrideprotected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {&nbsp; &nbsp; super.onRestoreInstanceState(savedInstanceState);&nbsp; &nbsp;&nbsp; &nbsp; allPixelsDate = savedInstanceState.getFloat(BUNDLE_LIST_PIXELS_DATE);&nbsp; &nbsp; allPixelsDateChanged = savedInstanceState.getFloat(BUNDLE_LIST_PIXELS_DATE_CHANGED);}@Overrideprotected void onSaveInstanceState(@NonNull Bundle outState) {&nbsp; &nbsp; super.onSaveInstanceState(outState);&nbsp; &nbsp; outState.putFloat(BUNDLE_LIST_PIXELS_DATE, allPixelsDate);&nbsp; &nbsp; outState.putFloat(BUNDLE_LIST_PIXELS_DATE_CHANGED, allPixelsDateChanged);}看这个视频链接,这是我的应用程序演示
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android