没有在 RecyclerView 上工作 Notifydatasetchange 与中心选择水平滚动视图?
Posted
技术标签:
【中文标题】没有在 RecyclerView 上工作 Notifydatasetchange 与中心选择水平滚动视图?【英文标题】:Not working Notifydatasetchange on RecyclerView with center selection Horizontal Scrollview? 【发布时间】:2017-02-10 18:13:47 【问题描述】:自动滚动是个问题,而且使用代码移动到某个特定位置也很困难。
我正在通过水平滚动和中心选择使两个 recyclerView 相互依赖。
所以我的问题是使用 Notifydatasetchanged 方法并将 recyclerView 位置重置为 0 并且它正在滚动选择范围,因为它返回错误的索引...
当我想在更改数据后获取中心选择索引时。 我正在使用下面的示例通过一些编辑来实现这一点。
Get center visible item of RecycleView when scrolling
我需要将第一个 recyclerView Adapter 的滚动数据更改为第二个 recyclerView Adapter 并更改数据。
但是滚动视图将位置设置在第一个位置 我尝试了 notifyItemRangeChanged(int, int) notifyItemRangeInserted(int, int) 方法...
详细说明:我正在更改类型并重置 Look 滚动视图的值。我需要更改底部滚动视图的选定位置。特别是我无法通过更改数据来获得中心位置。意味着如果我通知适配器,则索引将保持原样。重置数据后,我需要像普通适配器一样工作。
提前致谢。
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
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 ************************************************
【问题讨论】:
我不确定您要做什么。您是否希望顶部列表滚动到与底部列表相同的位置?您希望顶部列表何时更改其选定位置? 我正在更改类型并重置 Look 滚动视图的值。我需要更改底部滚动视图的选定位置。特别是我无法通过更改数据来获得中心位置。意味着如果我通知适配器,则索引将保持原样。重置数据后,我需要像普通适配器一样工作。 @Caleb : 我详细解释了这个案子。你现在可以检查一下吗 【参考方案1】:我不确定我是否正确理解了您,但您想更改一个 recyclerview 的数据并重置其他 recyclerview 的值
尝试使用 recyclerView.scrollToPosition(INDEX_YOU_WANT_TO_SCROLL_TO);
如果您想达到数据使用的中心位置 recyclerview.scrollToPosition(arraylist.size()/2); 存储可绘制数据的arrayList
【讨论】:
以上是关于没有在 RecyclerView 上工作 Notifydatasetchange 与中心选择水平滚动视图?的主要内容,如果未能解决你的问题,请参考以下文章
Android Paging 3 在 invalidate() 上清除 recyclerview
Firebase:RecyclerView 跳过布局没有附加适配器
单击不在 Fragment 内的 RecyclerView 项目上工作
滚动时单击不在 CoordinatorLayout 中的 RecyclerView 上工作
Dpad 导航无法在 Appbarlayout 下方的 recyclerview 的电视上工作(recycler view 无法通过 dpad 获得焦点)