I have a RecycleView . I updated its adapter, and call
notifydatasetchanged()
. I want to wait until the list finishes drawing
just add this:
recycler.post(() -> {
do something...
});
done.
try this one.
recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//At this point the layout is complete and the
//dimensions of recyclerView and any child views are known.
}
});
Related
I have a recyclerView. One of recyclerView items has an onClick method. In this method, I setVisibility for one Item to Visible(default is gone), also get the position and add it to an arrayList.
onClick :
public void onClickImageView(ImageView imageView, int position) {
imageView.setVisibility(View.VISIBLE);
positionArrayList.add(position);
}
Before i go to another activity, I want to the ImageView visibility be reset to default (gone) so I use an interface to send positionArrayList to the recyclerView activity to get views by position and reset ImageViews to default.
Interface :
public interface RecyclerViewMainActivityImp {
void resetImageViewToDefault(ArrayList<Integer> positionArrayList);
}
Where I use interface method in adapter :
public void OnClickNextActivity() {
Intent intent = new Intent();
intent.setClass(context, ClassActivity.class);
context.startActivity(intent);
recyclerViewInterface.resetImageViewToDefault(positionArrayList);
}
Activity :
#Override
public void resetImageViewToDefault(ArrayList<Integer> positionArrayList) {
for (int position = 0; position < positionArrayList.size(); position++) {
View row = binding.recyclerView.getLayoutManager().findViewByPosition(positionArrayList.get(position));
ImageView imageView = row.findViewById(R.id.imageView);
imageView.setVisibility(View.GONE);
}
}
When i scroll the recyclerView, and if some items are not in view anymore their views going to null so some positions in positionArrayList no longer have their view.
My problem is some views are null after scrolling and I don't know how to get access to them.
I searched on google and found some solutions but I could not solve my problem
Note : I have used data binding
there's workaround you can try for this,
create a function in your activity
function void updateRecyclerView(position){
yourArrayListForVisibility.set(position,'hide');
mAdapter.notifyDataSetChanged();
}
#Override
public void onResume(){
super.onResume();
mAdapter.notifyDataSetChanged();
}
call this function in your adapter before intent and in your adapter check for this arraylist value to hide or show content, always set default value to 'yourArrayListForVisibility' for every position
Any Activity that restarts has its onResume() method executed first.
So, call notifyDataSetChanged() of RcyclerView from onResume()
#Override
public void onResume(){
super.onResume();
mAdapter.notifyDataSetChanged();
}
I'm using RecyclerView inside another RecyclerView and when loading data the RecyclerView scroll to position 1 not position 0.
How can I make the parent RecyclerView scroll to position 0?
This line solve my issue
recyclerView.setFocusable(false);
Try it!
recyclerView.postDelayed(new Runnable() {
#Override
public void run() {
recyclerView.scrollToPosition(0);
}
},100);
I have a RecyclerView with rows that have views that when clicked will be disabled for that row position.
The problem is after I update the adapter like this:
adapterData.clear();
adapterData.addAll(refreshedAdapterData);
notifyDataSetChanged();
After refreshing the data, the disabled views at the previous recycler position still remain disabled even though the data is refreshed.
How can I reset the views to the original state after refreshing adapter data.
Use below code.
adapterData.clear();
adapterData.addAll(refreshedAdapterData);
adapter.notifyDataSetChanged();
OR
recyclerView.invalidate();
When you call notifyDataSetChanged(), the onBindViewHolder() method of every view is called. So you could add something like this in the onBindViewHolder() of your Adapter method:
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
if (refreshedAdapterData.get(position).isInDefaultState()) {
//set Default View Values
} else {
//the code you already have
}
}
I have resolved this by putting a conditional statement inside onBindViewHolder method instructing all positions to reset the disabled views if data meets the required conditions for a refreshed data.
#Christoph Mayr, thanks for your comments. It helped point me in the right direction.
I cleared the data then notify change but the selected checkbox doesn't reset but just moves up one position. Let say I selected item #1 , move out of the RecyclerView, came back and it will auto select item #0.
So, I created new adapter again at onResume(), i worked for me but i don't know if it's the right way to handle this situation.
#Override
public void onResume() {
super.onResume();
if(selectedItems != null && selectedItems.size() > 0){
selectedItems.clear(); // if no selected items before then no need to reset anything
if(adapter != null && recyclerView != null){
// to remove the checked box
adapter = null;
adapter = new MyAdapter(items, new MyAdapter.MyAdapterListener() {
#Override
public void onSelected(int pos) {
selectedItems.add(items.get(pos));
}
#Override
public void onUnSelected(int pos) {
selectedItems.remove(items.get(pos));
}
});
recyclerView.setAdapter(adapter);
}
}
}
I've a Listview with add button when i click add new row of buttons
created dynamically. When i scroll listview these new buttons are
visible. How can i click add buttons then the buttons are immediately
visible. Why it is happened. There is any way to handle this issue.
I've try invalidateViews() invalidate() it doesn't work. Please help me to solve this .
My sample code here
a
dd_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
button = new Button(getApplicationContext());
linear.addView(button1, lparams);
listview.invalidateViews();
}
}
To refresh a Listview, create a ListAdapter and populate it with your items (buttons). Every time you put in it a new set of items, your list gets updated.
Check this reference on ListViews: http://developer.android.com/guide/topics/ui/layout/listview.html
you can use listview.notifyDatasetChanged() method to refresh your list.
I've solve my issue like this. Call this method
//Here running boolean value
//lv listview
private void scrollMyListViewToBottom() {
lv.post(new Runnable() {
public void run() {
if (!running) {
// Select the last row so it will scroll into view...
iv.setSelection(daily_dairy_2.getCount() - 1);
lv.invalidateViews();
running = true;
}
}
});
}
I have used ListView added and delete option but when i clicked delete button database value is deleted but ListView is not refreshed. I have put
adapter.notifyDataSetChanged();
but the list doesn't refresh.
Try this, call adapter.notifyDataSetChanged() inside runOnUiThread.
YourActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
adapter.notifyDataSetChanged();
}
});
UI is update outside the main thread. Put all the logic inside an asynctask and in postexecute, call the adapter.notifyDataSetChanged();
You have to call .invalidate(); on the ListView to tell the framework the the view is outdated and should be redrawen!