Not working Notifydatasetchange on RecyclerView with center selection Horizontal Scrollview?

被刻印的时光 ゝ 提交于 2019-12-10 12:43:13

问题


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

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

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.

Get center visible item of RecycleView when scrolling

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

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

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.

Thanks in advance.

   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);

    }
}

/* 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);
}

//Type Adapter

 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 ************************************************


回答1:


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

Try using recyclerView.scrollToPosition(INDEX_YOU_WANT_TO_SCROLL_TO);

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



来源:https://stackoverflow.com/questions/39820788/not-working-notifydatasetchange-on-recyclerview-with-center-selection-horizontal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!