rollview无法在RecyclerView上运行Notify

rollview无法在RecyclerView上运行Notify

本文介绍了使用中心选择水平Scrollview无法在RecyclerView上运行Notifydatasetchange吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自动滚动出现问题&也很难使用代码移动到某些特定位置.

Auto Scrolling is issue & also move to some specific position using code is difficult.

我通过水平滚动和居中选择使两个recyclerView相互依赖.

I am making two recyclerView dependent to each other with the Horizontal Scroll and center selection.

所以我的问题是使用Notifydatasetchanged方法,并将recyclerView位置重置为0,并且滚动选择范围,因为它返回错误的索引...

So my problem is using the method of Notifydatasetchanged and reseting recyclerView postion to 0 and it's scrolling selection range because it's returning wrong index...

当我想在更改数据后获取中心选择索引时.我使用下面的示例通过一些编辑来实现这一点.

When i want to get center selection index after changing data.I am using below example to achieve this with some edits.

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

我需要通过数据更改将第一个recyclerView Adapter滚动条上的数据更改为第二个recyclerView Adapter.

I need to change the data on scroll of First recyclerView Adapter to second recyclerView Adapter with data change.

但是scrollview将位置设置在第一位置我尝试了notifyItemRangeChanged(int,int)notifyItemRangeInserted(int,int)方法...

But scrollview set the position in first positionI tried the notifyItemRangeChanged(int, int)notifyItemRangeInserted(int, int) methods...

详细说明:我正在更改类型并重置Look scrollview的值.我需要更改底部滚动视图的选定位置.特别是我无法通过更改数据来获得中心位置.表示我如果我通知适配器比索引保留原样.重置数据后,我需要像普通适配器一样进行操作.

Detail Explanation : I am changing the type and reset the value of Look scrollview. I need to change the selected position of bottom scrollview. Specially I can't get the center position by changing data. Means I if i am notifying the adapter than index will remain as it is. I need to do work it like normal adapter after reset data.

谢谢.

   public void getRecyclerview_Type() {

    final RecyclerView recyclerView_Type = (RecyclerView) findViewById(R.id.recycleView);
    if (recyclerView_Type != null) {
        recyclerView_Type.postDelayed(new Runnable() {
            @Override
            public void run() {
                setTypeValue();
            }
        }, 300);
        recyclerView_Type.postDelayed(new Runnable() {
            @Override
            public void run() {
                // recyclerView_Type.smoothScrollToPosition(Type_Adapter.getItemCount() - 1);
                setTypeValue();
            }
        }, 5000);
    }
    ViewTreeObserver treeObserver = recyclerView_Type.getViewTreeObserver();
    treeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {
            recyclerView_Type.getViewTreeObserver().removeOnPreDrawListener(this);
            finalWidthDate = recyclerView_Type.getMeasuredWidth();
            itemWidthDate = getResources().getDimension(R.dimen.item_dob_width_padding);
            paddingDate = (finalWidthDate - itemWidthDate) / 2;
            firstItemWidthDate = paddingDate;
            allPixelsDate = 0;

            final LinearLayoutManager dateLayoutManager = new LinearLayoutManager(getApplicationContext());
            dateLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            recyclerView_Type.setLayoutManager(dateLayoutManager);
            recyclerView_Type.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    synchronized (this) {
                        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                            calculatePositionAndScroll_Type(recyclerView);
                        }
                    }
                }

                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    allPixelsDate += dx;
                }
            });
            if (mTypeBeanArrayList == null) {
                mTypeBeanArrayList = new ArrayList<>();
            }
            getMedicationType();
            Type_Adapter = new Medication_Type_RecyclerAdapter(Add_Reminder_medicationlook_Activity.this, mTypeBeanArrayList, (int) firstItemWidthDate);
            recyclerView_Type.setAdapter(Type_Adapter);
            Type_Adapter.setSelecteditem(Type_Adapter.getItemCount() - 1);
            return true;
        }
    });
}

private void getMedicationType() {

    for (int i = 0; i < mTypeBeanArrayList.size(); i++) {
        Medication_TypeBean medication_typeBean = mTypeBeanArrayList.get(i);


        Log.print("Image name :" +medication_typeBean.getType_image_name());
        if (i == 0 || i == (mTypeBeanArrayList.size() - 1)) {
            medication_typeBean.setType(VIEW_TYPE_PADDING);
        } else {
            medication_typeBean.setType(VIEW_TYPE_ITEM);
        }
        mTypeBeanArrayList.set(i, medication_typeBean);

    }
}

/*如果最重要的话,如果期望位置日期< 0 recyclerView将返回到最近的项目*/

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

private void calculatePositionAndScroll_Type(RecyclerView recyclerView) {
    int expectedPositionDate = Math.round((allPixelsDate + paddingDate - firstItemWidthDate) / itemWidthDate);

    if (expectedPositionDate == -1) {
        expectedPositionDate = 0;
    } else if (expectedPositionDate >= recyclerView.getAdapter().getItemCount() - 2) {
        expectedPositionDate--;
    }
    scrollListToPosition_Type(recyclerView, expectedPositionDate);

}

/* this if most important, if expectedPositionDate < 0 recyclerView will return to nearest item*/
private void scrollListToPosition_Type(RecyclerView recyclerView, int expectedPositionDate) {
    float targetScrollPosDate = expectedPositionDate * itemWidthDate + firstItemWidthDate - paddingDate;
    float missingPxDate = targetScrollPosDate - allPixelsDate;
    if (missingPxDate != 0) {
        recyclerView.smoothScrollBy((int) missingPxDate, 0);
    }
    setTypeValue();

}

private void setTypeValue() {
    int expectedPositionDateColor = Math.round((allPixelsDate + paddingDate - firstItemWidthDate) / itemWidthDate);
    int setColorDate = expectedPositionDateColor + 1;

    Type_Adapter.setSelecteditem(setColorDate);
    mTxt_type_name.setText(mTypeBeanArrayList.get(setColorDate).getMedication_type_name());

    mSELECTED_TYPE_ID = setColorDate;

    //NotifyLookChangetoType(setColorDate);
}

//类型适配器

 public class Medication_Type_RecyclerAdapter extends RecyclerView.Adapter<Medication_Type_RecyclerAdapter.ViewHolder> {

    private ArrayList<Medication_TypeBean> medication_typeBeanArrayList;
    private static final int VIEW_TYPE_PADDING = 1;
    private static final int VIEW_TYPE_ITEM = 2;
    private int paddingWidthDate = 0;
    private Context mContext;
    private int selectedItem = -1;

    public Medication_Type_RecyclerAdapter(Context context, ArrayList<Medication_TypeBean> dateData, int paddingWidthDate) {
        this.medication_typeBeanArrayList = dateData;
        this.paddingWidthDate = paddingWidthDate;
        this.mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_ITEM) {
            final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_medication_type,
                    parent, false);
            return new ViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_medication_type,
                    parent, false);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view.getLayoutParams();
            layoutParams.width = paddingWidthDate;
            view.setLayoutParams(layoutParams);
            return new ViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Medication_TypeBean medication_typeBean = mTypeBeanArrayList.get(position);
        if (getItemViewType(position) == VIEW_TYPE_ITEM) {
            // holder.mTxt_Type.setText(medication_typeBean.getMedication_type_name());
            //holder.mTxt_Type.setVisibility(View.VISIBLE);
            holder.mImg_medication.setVisibility(View.VISIBLE);
            int d = R.drawable.ic_type_pill;
            try {
                //Due to Offline requirements we do code like this get the images from our res folder

                if (medication_typeBean.getType_image_name().equalsIgnoreCase("ic_type_pill")) {
                    d = R.drawable.ic_type_pill;
                } else if (medication_typeBean.getType_image_name().equalsIgnoreCase("ic_type_patch")) {
                    d = R.drawable.ic_type_patch;
                } else if (medication_typeBean.getType_image_name().equalsIgnoreCase("ic_type_capsule")) {

                    d = R.drawable.ic_type_capsule;

                } else if (medication_typeBean.getType_image_name().equalsIgnoreCase("ic_type_ring")) {

                    d = R.drawable.ic_type_ring;

                } else if (medication_typeBean.getType_image_name().equalsIgnoreCase("ic_type_inhaler")) {

                    d = R.drawable.ic_type_inhaler;


                } else if (medication_typeBean.getType_image_name().equalsIgnoreCase("ic_type_spray")) {

                    d = R.drawable.ic_type_spray;


                } else if (medication_typeBean.getType_image_name().equalsIgnoreCase("ic_type_bottle")) {

                    d = R.drawable.ic_type_bottle;

                } else if (medication_typeBean.getType_image_name().equalsIgnoreCase("ic_type_drop")) {

                    d = R.drawable.ic_type_drop;


                } else if (medication_typeBean.getType_image_name().equalsIgnoreCase("ic_type_pessaries")) {

                    d = R.drawable.ic_type_pessaries;

                } else if (medication_typeBean.getType_image_name().equalsIgnoreCase("ic_type_sachets")) {

                    d = R.drawable.ic_type_sachets;


                } else if (medication_typeBean.getType_image_name().equalsIgnoreCase("ic_type_tube")) {

                    d = R.drawable.ic_type_tube;


                } else if (medication_typeBean.getType_image_name().equalsIgnoreCase("ic_type_suppository")) {

                    d = R.drawable.ic_type_suppository;

                } else if (medication_typeBean.getType_image_name().equalsIgnoreCase("ic_type_injaction")) {

                    d = R.drawable.ic_type_injaction;

                } else if (medication_typeBean.getType_image_name().equalsIgnoreCase("ic_type_spoon")) {

                    d = R.drawable.ic_type_spoon;

                } else if (medication_typeBean.getType_image_name().equalsIgnoreCase("ic_type_powder")) {

                    d = R.drawable.ic_type_powder;

                } else {
                    d = R.drawable.ic_type_pill;
                }

                Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(),
                        d);
                holder.mImg_medication.setImageBitmap(icon);
            } catch (Exception e) {
                Log.print(e);
            }


            //     BitmapDrawable ob = new BitmapDrawable(mContext.getResources(), icon);
            //  img.setBackgroundDrawable(ob);
            // holder.mImg_medication.setBackground(ob);

            // Log.print("Type Adapter", "default " + position + ", selected " + selectedItem);
            if (position == selectedItem) {
                Log.print("Type adapter", "center" + position);
                //  holder.mTxt_Type.setTextColor(Color.parseColor("#76FF03"));
                //holder.mImg_medication.setColorFilter(Color.GREEN);
                //   holder.mTxt_Type.setTextSize(35);
                holder.mImg_medication.getLayoutParams().height = (int) mContext.getResources().getDimension(R.dimen.item_dob_width) + 10;
                holder.mImg_medication.getLayoutParams().width = (int) mContext.getResources().getDimension(R.dimen.item_dob_width) + 10;
            } else {
                holder.mImg_medication.getLayoutParams().height = (int) mContext.getResources().getDimension(R.dimen.item_dob_width) - 10;
                holder.mImg_medication.getLayoutParams().width = (int) mContext.getResources().getDimension(R.dimen.item_dob_width) - 10;
                //  holder.mTxt_Type.setTextColor(Color.WHITE);
                //holder.mImg_medication.setColorFilter(null);
                //  holder.mTxt_Type.setVisibility(View.INVISIBLE);
                // holder.mTxt_Type.setTextSize(18);
                //   holder.mImg_medication.getLayoutParams().height = 70;
                //   holder.mImg_medication.getLayoutParams().width = 70;
            }
        } else {
            holder.mImg_medication.getLayoutParams().height = (int) mContext.getResources().getDimension(R.dimen.item_dob_width) - 10;
            holder.mImg_medication.getLayoutParams().width = (int) mContext.getResources().getDimension(R.dimen.item_dob_width) - 10;
            //  holder.mTxt_Type.setVisibility(View.INVISIBLE);
            holder.mImg_medication.setVisibility(View.INVISIBLE);
        }
    }

    public void setSelecteditem(int selecteditem) {
        this.selectedItem = selecteditem;
        notifyDataSetChanged();

        if (medication_lookBeanArrayList != null && Look_Adapter != null) {
            NotifyLookChangetoType(selecteditem);
        }
    }

    public int getSelecteditem() {
        return selectedItem;
    }

    @Override
    public int getItemCount() {
        return medication_typeBeanArrayList.size();
    }

    @Override
    public int getItemViewType(int position) {
        Medication_TypeBean medication_typeBean = medication_typeBeanArrayList.get(position);
        if (medication_typeBean.getType() == VIEW_TYPE_PADDING) {
            return VIEW_TYPE_PADDING;
        } else {
            return VIEW_TYPE_ITEM;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        //public TextView mTxt_Type;
        public ImageView mImg_medication;

        public ViewHolder(View itemView) {
            super(itemView);
            //  mTxt_Type = (TextView) itemView.findViewById(R.id.mTxt);
            mImg_medication = (ImageView) itemView.findViewById(R.id.mImg_medication);
        }
    }
}

//Type Adapter ends ************************************************

推荐答案

我不确定您是否正确,但是您想更改一个recyclerview的数据并重置另一个recyclerview的值

I am not sure I get you properly but you want to change the data of one recyclerview and reset the value of other recyclerview

尝试使用recyclerView.scrollToPosition(INDEX_YOU_WANT_TO_SCROLL_TO);

Try using recyclerView.scrollToPosition(INDEX_YOU_WANT_TO_SCROLL_TO);

如果您想实现数据使用的中心位置 recyclerview.scrollToPosition(arraylist.size()/2);存储可绘制数据的arrayList

IF YOU WANT TO ACHIEVE THE CENTER POSTION OF THE DATA USE recyclerview.scrollToPosition(arraylist.size()/2);arrayList in which your drawable data is stored

这篇关于使用中心选择水平Scrollview无法在RecyclerView上运行Notifydatasetchange吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:22