How to change selected item position to top in Recyclerview? - android

I have recyclerview in that I want to change item's position dynamically to top after selecting the item of recyclerview.
Please suggest best way to achieve the above problem?

You need to swap selected item with top item in list, then notify your adapter about position change.
A sample code would look like:
Collections.swap(orderItems.getItems(), position, 0);
notifyItemMoved(position, 0);
Hope it helps!

Try this
String Item = List.get(position);
List.remove(position); // remove item from the list
List.add(0,removedItem); // add at 0 index of your list
notifyDataSetChanged();
linearLayoutManager.scrollToPositionWithOffset(0, 20); // scroll recyclerview to top
OR
recyclerview.getLayoutManager().scrollToPosition(0) // scroll recyclerview to top
OR
recyclerview.smoothScrollToPosition(0); // scroll recyclerview to top

add below code in onclick of item,
String removedItem = actualList.remove(position);
//actualList is list used to populate recylerview and position is selected
//item position
actualList.add(0,removedItem);
notifyDataSetChanged();

If you have different cases like this, we can achieve this approach from #manohar-reddy and call notifyItemChanged(position) it will also call the onBindViewHolder, then update your layout accordingly.
*In this case, if the 0 item has a different layout from the other items
You might wanna read this reference for getting the item position correctly
thanks to #amit-srivastava answer and #manohar-reddy

Related

Change the background color of item in RecylerView , having position only

I have the positions for all the items in the RecyclerView and I want to write a code which programmatically gives background color of the item, just on the basis of position provided to it. So far I am able to scroll to the item by using this recyclerView.smoothScrollToPosition(Integer.parseInt(value));. But not able to highlight or give background color to that item.
You have 2 options:
Put it into the model
Use a decoration
The first approach is straight forward. Along with the list of items your adapter knows about the highlighted position(s). Upon changes, you invalidate the list and apply your data in onBindViewHolder, including possible highlights.
onBindViewHolder(...) {
// ...
if (positionIsHighlighted) view.setBackground(dis) else view.setBackground(dat)
}
Another approach would be to do something similar using an ItemDecoration. You draw on top/below any views you want to highlight. This involves custom drawing and some more knowledge than the first approach.
The benefits are that it may be more reusable and that you can draw on top or outside the boundaries of your selected views.
You can define a variable in your adapter like selectedItemPosition : Int = 0, then have a setter like below:
fun setSelectedItem(position:Int){
if(position == this.selectedItemPosition) return
val oldPosition = this.selectedItemPosition
this.selectedItemPosition = position
if(oldPosition >=0 )
notifyItemChanged(oldPosition)
if(position >=0)
notifyItemChanged(position)
And in your viewholder, check if the adapterPosition equals to selectedItemPosition. this way, when you change the selected item, the old selected item gets de-selected, and the new one gets selected.

How do I apply layout changes to one individual ListView item without the adapter recycling the layout to other items?

I have a custom adapter for a ListView. On getView(), I attach an onTouchListener to the convertView.
Upon touch of the item/convertView, I update the ViewHolder's RelativeLayout's LayoutParams to be shifted left and set a couple buttons revealed underneath the uppermost layout to clickable, as described on a few swipeable ListView guides.
Unfortunately, when I scroll down my ListView, other items also now have the layout slid to the side with the buttons set clickable underneath. I am not sure why a change to a single ViewHolder is being recycled to others, but I'm sure I'm just falling prey to a simple misunderstanding. Can anyone offer an explanation and solution? Thanks!
After recycling a view you should reset the view to its initial state and populate it with the new values.
You can use an simple Array of booleans for this. for example,
List has 5 items so initially array will be-
arr[false,false,false,false,false]
When you select any item then update arrays value at that items positions-
list item 1 selected - arr [true,false,false,false,false]
list item 3 selected - arr [true,false,true,false,false]
list item 5 selected - arr [true,false,true,false,true]
and check array's values in getView's method-
if(arr[position] == false)
{
// item not selected so do changes you want in non-selected items
}
else
{
// item selected so do changes you want in selected items
}

How to add a item to Listview but always make it the last item?

I am going to manually add one item to the listView but i always want that item to be the last item in the listview because i am already adding items the listView dynamically
here is what i am using to add the items:
tableItems.Add(new TableItem("Test","Test",Resource.Drawable.Icon));
you should use your custom adapter.
In your adapter, create Add method and make the new item as the last item of ArrayObject or another way make sure that method getItem (int position) return the item you had added with position == (arrayObject.size - 1)
You could probably use listView.setFooterView(footerView); like shown here.
You need to add data in your listview adapter and need to notify your adapter.
i.e.
_youAdapter.add("ABC");
_youAdapter.notifyDataSetChanged();
Your data will add in listview at the end of the list always.
Notify your adapter after this line
tableItems.Add(new TableItem("Test","Test",Resource.Drawable.Icon));
mAdapter.notifyDataSetChnaged();
if in any case this not work then directly add item to adapter ie.
mAdapter.Add(new TableItem("Test","Test",Resource.Drawable.Icon));
mAdapter.notifyDataSetChnaged();

How to simulate selection on the first item(or whatever position) in the ListView?

I tried the following but had no luck since the getChildAt always returns null(I am sure the listView is filled).
listView.performItemClick(listView.getChildAt(0), 0, listView.getChildAt(0).getId());
Any suggestions? Thanks!
Edited: code for adding item into list
lvAdapter = new ActionStreamAdapter<HasDescription>(getBaseContext());
while ((item = actionStreamQueue.poll()) != null) {
...
lvAdapter.addItem(item);
}
ListView.setAdapter(lvAdapter);
Where ActionStreamAdapter is a class extends BaseAdapter. The queue is filled by calling the Dao of a simple data sturcure class.
There is a difference between childs and items. A Child is a view that is lower in the view hierarchy. For example, when there is a LinearLayout containing two TextViews, then the TextViews are children of the LinearLayout. A ListView has items. These are on the same hierarchical level as your ListView so they aren't children. They are items of the ListView. You should use ListView.getItemAtPosition() instead.
Then, there is a difference between selecting and clicking. Clicking means performing an action on an item and selecting is highlighting it. For selecting the first item, you should do something like: ListView.getItemAtPosition(n).requestFocus()
It is an interesting API where it takes a pointer of the viewitem, typically listview items are recycled so the item at position 0, need not be 0th item on the adapter. You could use findViewWithTagTraversal() to find the view and do the selection on it.
If you don't mind what do you want to do with selection, or do you want to give focus to the first item?
You could use setSelection()

ListView scroll to selected item

I have a ListView with an edit text and a button below it. When I click on a listView item the keyboard appears and push up the edit text and the button. I want the list to scroll to the selected item. Any idea? Thanks
You can use ListView's setSelection(int position) method to scroll to a row.
You could use ListView's smoothScrollToPosition(int position) to scroll to a particular location in the list.
For a direct scroll:
getListView().setSelection(11);
For a smooth scroll:
getListView().smoothScrollToPosition(11);
To Scroll to top
getListView().setSelectionAfterHeaderView();
Note
try to call it in post because sometime listview is not yet created while you calling it's method
getListView().postDelayed(new Runnable() {
#Override
public void run() {
lst.setSelection(15);
}
},100L);
You should use transcript mode:
getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
Setup a listener on your the list item being clicked, then use View.getTop() or View.getBottom() when clicked to get it's position within the parent. You can then use ListView.scrollTo(x, y) to scroll to the list item.
Yo can look For
listView.setSelectionFromTop(position, distanceFromHeader);
It will position the Item at position , specified pixels below the top of listview
You can use
smoothScrollToPosition(position)
Just increase the position of item with 1, and you will get the view of item.
getListView().smoothScrollToPosition(position + 1);
Using duration gives a better user experience. Use this, with duration added. Will scroll the item in position smoothly to the top of the listview.
int duration = 500; //miliseconds
int offset = 0; //fromListTop
listview.smoothScrollToPositionFromTop(position,offset,duration);
descrease duration to make scrolling faster

Categories

Resources