I want to highlight the selected item in list view. There is one scenario in which if user selects any item from list view, list view freezes and if user again comes to that screen that item will show as selected. (Note that this is not the case every time). So Is there some way by which I can show some item selected say via some background color. But this need to be done in activity class file NOT IN ADAPTER.
I tried
MYListAdapter adapter = (GenericListAdapter) lvList.getAdapter();
View view = adapter.getView(position, null, null);
view.setBackgroundColor(color.holo_orange_dark);
But its not working as expected. Can any body suggest me any thing.
adapter.getView(position, null, null) creates a new view, and not return view which is used in listView. You can try to get listView and get its child by position. Smth like that:
lvList.getChildAt(position).setBackgroundColor(color.holo_orange_dark);
But I think it's not the right way, because ListView can recreate view at any time.
So the right way is adapter..
you can see apiDemo->Views->Lists->Activate items.It's very simple,just setChoiceMode and provide a backgroud selector.
By the way,you should see android4.x apiDemo.
Related
There is custom view and a ListView, both are coupled as such if one changes its state other must change. I am able to control custom view if one item of ListView is selected.
If a item is selected in custom view, corresponding item in List view should become visible and text in it must be hilighted.
I tried
listView.smoothScrollToPosition(clueIndex);
no scrolling happens on execution of this line.
Second question -
How can I get access to TextView in a particular adaptor postion and change its text color outside OnItemLongClickListener() ?
Issue resolved, due to error clueIndex was always -1.
I've working on app that need custom list view...
List view should act like some kind of vertical gallery.
In gallery selected item is always on center.
In my app I use navigation keys only or remote controller.
So there is no fling or regular scrolling effect.
I need for example second element in my list to be selected always.
If I want to move up, all elements are scrolled one place up, and selected item has to be changed, but on same place on the screen like previous selected item.
It's the same thing like gallery only vertical.
Is there a simple way to do this?
Is there a vertical gallery implementation?
How can I make gallery widget vertical?
Or how to customize list view to act like that?
Tnx!
try setting onClick Listener on ListView
ListView listView1=(ListView) findViewById(R.id.listView1);
then implemt a class class SearchItemClickListener implements OnItemClickListener
and set listview.onItemClickListenr you will get position and view.
Hope this helps
You can get the current visible first postion by using,
mListView.getFirstVisiblePosition();
after getting the current visible position and its respective data, you can perform your desired task (i.e. display the large image after getting the thumbnail or something like this).
OR
If you don't want to depend on only first or last position and want to select some other visible row then you can do something like this,
public View getView(int position, View convertView, ViewGroup parent)
{
int visiblePosition = position % nObjects; // nObjects if the total number of objects to display
if(visiblePosition == yourDesiredPosition){
// do your custom work here.
}
}
Here is a demo for vertical slide show,
Hope this will give you some hint about implementing your functionality.
I've managed this using setSelected() method on ListView item...and managing key listener.
Didn't found smarter way to handle this.
I have a ListView with a custom adapter, displaying information from a database.
When I start the app, the data is read from the database, given to the adapter, which is then attached to the ListView. A LayoutAnimationController is run on the ListView, displaying the contents smoothly.
this._lvList = (ListView)_v.findViewById(R.id.lvList);
this._lvList.setAdapter(new TableAdapter(getActivity(),R.layout.tablerow,Category.getCategories()));
LayoutAnimationController lac = new LayoutAnimationController(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_in_left));
lac.setDelay(0.2f);
this._lvList.setLayoutAnimation(lac);
this._lvList.startLayoutAnimation();
No problem at all.
Now when I click on any entry of the ListView, the entries which were not clicked disappear, the clicked entry becomes the one and only entry in the list and is displayed at the top, as expected.
View v;
for(int i = 0;i<_lvList.getChildCount();i++) {
v = this._lvList.getChildAt(i);
if(v!=clickedView) {
Animation animSlideOut = AnimationUtils.loadAnimation(getActivity(), i%2==0?R.anim.slide_out_left:R.anim.slide_out_right);
animSlideOut.setStartOffset(i*50);
// some more stuff
}
v.startAnimation(animSlideOut);
}
This works as well, but now the problem, if I click again on that single list entry, I want the list to repopulate, displaying all items again.
I thought I could use the code from the start (the first snippet), as it works fine when starting the app, but...this time...it doesn't. No animation happening. The reason is, there are no views to animate in my ListView (except the one from the previous step).
Instead of creating a new Tableadapter I already tried to clear it, fill it new, called notifyDataSetChanged()... no use, as
_lvList.getChildCount();
stills returns 1 view, while the adapter holds all 18 entries.
I as well tried
requestLayout();
forceLayout();
invalidate();
invalidateViews();
to force the ListView to generate its child views before the animation, but it's not working. So atm the ListView just appears instantly, somewhen after my call to the layout animation.
Summary : my ListView contains no child views before the start of the layout animation, how can I force it to generate them?
Thx in advance :)
Hmm... have you done this yet? (below)
in your custom adapter, override the method:
#Override
public View getView(int position, View convertView, ViewGroup parent){
//the position of the item, the convertView is the view layout of what that row is
}
to change how the listview is updated. this getView method gets called by Android every once a while to refresh the view (you don't call it manually)
also, i think you only need to call
listView.notifiyDataSetChanged() and listView.invalidate() to force the update, after you repopulate the list
you can read more on listviews here:
http://www.vogella.com/articles/AndroidListView/article.html
http://developer.android.com/guide/topics/ui/layout/listview.html
i am trying to change the background color of the selected row in a listview and i am able to do it.
But when i am clicking on another row then the background color of the previously selectedrow remains unchanged. I have the position of the previously selected row, Can anybody help me that how can change the background color of previously selected row as it was before??
If you keep track and update the listItems clicked state in your model you can just put the code for displaying has been clicked-color in the adapter and then call
adapter.notifyDatasetChanged();
I think this might be easier if you look at it another way.
Currently, your logic is "if I click on this row, change its color to a special color and change the old row's color back to the original color". However, this doesn't seem to be the logic you actually want. Rather, you want the last clicked (aka selected) row to be a different color.
You haven't posted any code, so I don't know if you are implementing your own ListAdapter in this project. That is the approach I would take. Create a class which extends ListAdapter, and create an additional private variable that stores the position of the last selected row. Then in the overriden getView() method, do a quick check
if(rowPosition == lastSelectedRowPosition)
viewToReturn.setBackgroundColor();
If you aren't sure how to make your own list adapter, check out the tutorial at http://jnastase.alner.net/archive/2010/12/19/custom-android-listadapter.aspx.
In your activity use setOnItemClickListener of Your_List Object.
See Demo Code:
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
v.setBackgroundColor(Color.BLUE); // <--- Use color you like here
// ^ this v gives current row.
}
});
This will make background color changed for that row forever.
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()