I am using Listview, i want to scroll down to list's 10 item when view get loaded, how can do that ????
Try yourListView.setSelection(position)
Try this:
mMessageList.addAll(result);
mMessageArrayAdapter.notifyDataSetChanged();
mMessageListView.clearFocus();
mMessageListView.post(new Runnable() {
#Override
public void run() {
mMessageListView.setSelection(searchedMessagePosition);
}
});
Related
When my recyclerview gets a new item inserted using notifyItemInserted and smoothScrollToPosition, it creates a flickering/blink effect. What is causing it? I just want it to scrollDown when a new item is inserted.
mMessageList.add(messageObject);
runOnUiThread(new Runnable() {
#Override
public void run() {
mChatMessageAdapter.notifyItemInserted(mMessageList.size());
mRecyclerView.smoothScrollToPosition(mMessageList.size());
}
});
I am working on an application where I need a spinner if the user chooses some values from the spinner then I need to smooth scroll for that particular position and highlight the item of that position . I have done something for that I have a method that will highlight the position if the view is visible if the view is not visible it will not highlight. So, I am facing some issues like this.
This is the place where I get the position from the spinner
versesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
final int possition = (int) versesSpinner.getSelectedItemId();
recyclerView.smoothScrollToPosition(possition);
setBackgroundColor(possition);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
And this is the method where I highlight the item
public void setBackgroundColor(int position) {
for (int i = 0; i < versesList.size(); i++) {
View view = mLayoutManager.findViewByPosition(i);
try {
if (view != null) {
if (i == position)
view.setBackgroundColor(Color.parseColor("#504cbee7"));
else
view.setBackgroundColor(Color.WHITE);
}
} catch (Exception e) {}
}
}
Here the problem is while calling the setBackground() method before the smooth scroll method reaches that particular item.
The highlighting will be done only if the view is visible and if the view is not null.
Please tell me if there is any other way is there to achieve this else please tell me how can I find out if the smooth scroll to position has reached the particular item
I hope it make sense.
You are trying to change the background of item before it has completed the scrolling. Use a handler to schedule that task after some time.
recyclerView.postDelayed(new Runnable(){
public void run(){
setBackgroundColor(possition);
}
},1000);
You might consider having a loom at my answer here to check how to highlight the items in your RecyclerView on demand. Now if you want a smooth scroll to the position highlighted just add smoothScrollToPosition(position) right after calling the notifyDataSetChanged() function.
So the highLightTheListItems() function might look like this which I'm referring from my answer there.
public void highLightTheListItems() {
// Modify your list items.
// Call notifyDataSetChanged in your adapter
yourAdapter.notifyDataSetChanged();
// Now smooth scroll to the position you want.
}
Hope that helps.
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'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;
}
}
});
}
Sometimes, when I call goToLast() it throws me a null exception in vista=lista.getChildAt(), it happens when the list is full, I dont know why I have this code:
private void goToLast() {
lista.post(new Runnable() {
#Override
public void run() {
lista.setSelection(mensajes.getCount() - 1);
View vista = lista.getChildAt(mensajes.getCount() - 1);
TextView txtMensaje = (TextView)vista.findViewById(R.id.txtMensajeLista);
txtMensaje.setBackgroundColor(Color.RED);
}
});
}
You should log lista.getChildCount(), see how many children the ListView has. ListView recycles views, which means it only hold limit number of views.
So if you want to get the last view, you should do something like this
private void goToLast() {
lista.post(new Runnable() {
#Override
public void run() {
lista.setSelection(mensajes.getCount() - 1);
// Figure out the last position of the view on the list.
int lastViewIndex = lista.getChildCount() - 1;
View vista = lista.getChildAt(lastViewIndex);
TextView txtMensaje = (TextView)vista.findViewById(R.id.txtMensajeLista);
txtMensaje.setBackgroundColor(Color.RED);
}
});
}
ListView.getChildAt() position is different to the position in your adapter. ListView recycles its views, so if you have more items in your adapter, than can fit on the screen it will not create views for all of those, but only the ones that are visible. If you want to update an item in the ListView you need to update it in the adapter and call notifyDataSetChanged()