I have listviews inside a pager adapter, and I'm trying to make it so when I click on an item inside my listview, it will scroll that item to the top. I have an onclicklistener to the item, but I'm not sure how exactly to implement the scrolling given what I have.
public Object instantiateItem(ViewGroup container, int position) {
List<App> app = this.apps.get(position);
ListView list = new ListView(this.activity);
list.setAdapter(adapter);
container.addView(list);
return list;
}
First, use ListView's onItemClickListener.
Secondly, use the setSelected() method with the item's position (which you get on the listener).
smoothScrollToPosition
setSelectedPosition
Related
I have a listview. What I've implemented in that listview is that when user clicks a list item a 2 button view is inflated to replace the content of that list item like this:
This works fine but what I want is when I click the second list item the first one should come back to its original layout. Currently, it is like this:
This is my code implemented in onClick method of listview:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView planName = view.findViewById(R.id.planNameText);
TextView planDate = view.findViewById(R.id.planDateText);
ImageView planImage = view.findViewById(R.id.homePlanImageView);
planName.setVisibility(View.INVISIBLE);
planDate.setVisibility(View.INVISIBLE);
planImage.setVisibility(View.INVISIBLE);
RelativeLayout rl_inflate = (RelativeLayout)view.findViewById(R.id.rl_inflate);
View child = getLayoutInflater().inflate(R.layout.inflate, null);
rl_inflate.addView(child);
}
});
Thanks.
Maybe you need to initialice a boolean variable to check if is clicked or not and refresh all the views. I really recommend you to use a RecyclerView and use 2 viewHolders. If you want information about this check this. If you implement a recycler with 2 viewholder it will be easier than the way that you want to implement it, and you can use notifyDataSetChanged to refresh the recycler. Whatever, you will need anyways a boolean to check if is clicked or not.
Can anyone tell me how I can hide(not collapsing all items) all the items in an expandable list view except for the one item which is clicked. Can this be done using the adapter class implementation? or Just by hiding the Expandable list view on it's item click and then showing the required view with just one item? My current implementation is done by hiding the view and showing the other one with just one item. Any other way to do this?
You can do one thing:
List<String> items, selectedItem;
ExpandableListAdapter adapter;
view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedItem = new ArrayList<String>();
selectedItem.add(items.get(position));
adapter.notify(selectedItem);
}
});
I assume that you have list of string and you set items via ExpandableListAdapter.
Implement above in your fragment/activity.
Now, when user touch on any item, you will get that item and notify adapter with new list where there is only selected item, so other items will be hide.
Yes of course this will be done using the adapter class implementation.
I have a horizontal Recyclerview which displays bitmaps.
The Way it is implemented is I have a Imageview and a recyclerview underneath it. The currently selected item is displayed on the image view. The selected image view is given a blue background to indicate it is selected. I can choose images from the gallery and each time a new image is selected, I want to scroll to the last position and make the item selected.
The list of images is maintained in a array list and each time a new image is added, I add the image to the list and notifyDataChanged().
Currently when I am binding a view, I toggle the visibility of the blue background in
public void onBindViewHolder(final MyRecyclerViewHolder holder, int position) {
}
But the problem is, if the child is off screen, the bind view is not called and I dont scroll to the new position.
I read through the documentation of recycler view and could not figure out how to scroll to that particular child view. I do not there is a SmoothScrollTo method but my question is where do I trigger this ?
There is one solution:
In your RecyclerView adapter, add a variable selectedItem and a methods setSelectedItem():
private static int selectedItem = -1;
... ...
public void setSelectedItem(int position)
{
selectedItem = position;
}
In your onBindViewHolder(...), add:
#Override
public void onBindViewHolder(ViewHolder holder, final int position)
{
... ...
if(selectedItem == position)
holder.itemView.setSelected(true);
}
Now you can scroll to specific item and set it selected programmatically by:
myRecyclerViewAdapter.setSelectedItem(position_scrollTo);
myRecyclerView.scrollToPosition(position_scrollTo);
For example, you want to scroll to last position and make it selected, just:
int last_pos = myRecyclerViewAdapter.getItemCount() - 1;
myRecyclerViewAdapter.setSelectedItem(last_pos);
myRecyclerView.scrollToPosition(last_pos);
[UPDATE]
To add item and make it selected, you should have a addItem(...) method in adapter, which will add item to item list. After adding item, refresh list and scroll to new added/latest item:
myRecyclerViewAdapter.addItem(...);
myRecyclerViewAdapter.notifyDataSetChanged();
myRecyclerViewAdapter.setSelectedItem(myRecyclerViewAdapter.getItemCount() - 1);
myRecyclerView.scrollToPosition(myRecyclerViewAdapter.getItemCount() - 1);
Hope this help!
Your view will not be created until you scroll to the item.
You must call
recyclerView.scrollToPosition(position)
Where position is
recyclerView.getAdapter().size()
So the item became visible.
Use RecyclerView LayoutManager to scroll item at position
recyclerView.getLayoutManager().scrollToPosition(position)
I forgot to address the timing part ideally this should be called when the adapter has been identified of the dataset change.
You could use
recyclerView.smoothScrollToPosition(View.FOCUS_DOWN);
I am having a grid view ,I am passing data which I want to display in grid view to the gridview adaptor class.Now I want to remove the item when an item is long pressed.
I want to delete in onItemLongClickListener .
What you need to do is this:
Create a custom adapter for your GridView. In that adapter, provide a method for removing an item from the list of items it maintains, for example 'void removeItem(int position)`
Call setOnItemLongClickListener to your grid. In this method, you get the position of the item that the long click occurred on. From this method, call the method to remove the item with the position you just received.
Notify the GridView that the data has changed using notifyDataSetchanged method. If you want the GridView UI to update immediately, you need to use Handler for this request to make sure it happens on the UI thread.
if mThumbIdsList is the integer array of all the gridview items ids then you can try this code. this may help you.
final ImageAdapter adapter = new ImageAdapter(this);
gridview.setAdapter(adapter);
gridview.onItemLongClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
adapter.mThumbIdsList.remove(position);
adapter.notifyDataSetChanged();
}
});
I have a custom listview. I have to select some items from this listview and display it in the next layout listview when a button click event is clicked.
lv5=(ListView)findViewById(R.id.ListView05);
lv5.setAdapter(new ArrayAdapter<String>(this,R.layout.productselecttext,R.id.pstext,arr));
lv5.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(view.findViewById(R.id.oi).getVisibility()==ImageView.VISIBLE){
ImageView icon = (ImageView)view.findViewById(R.id.oi);
icon.setImageResource(R.drawable.vi);
}
}
});
In this way I have selected using an imageview. How can I get only the selected items and display in another listview?
It all depends on what you call a selected item. We need some more code to understand how you "selected" items in the first listview.
But generally speaking, all you have to do is to build a new Adapter for the second list view that just lists the selected item. You could even recylcle the first adapter and the first view, just eliminating the items that are not selected from the adapter's list and calling
notifyDatasetChanged()
on your first adapter to update your first list.
Regards,
stéphane