I am implementing the Grid View in my android application.
I want the set the focus on my selected grid. How can i get the solution for this problem ?
I recommend to set a flag to each grid items which indicates selected or not.
and, when selecting grid item set the flags properly and call notifyDatasetChanged() on your adapter.
this will work. I did same.
If you need to set focus to GridView indeed, than call GridView's requestFocus() method. If you need to set focus at specified item in GridView, than find it by getChildAt() and also call requestFocus(). You also should be sure that your device isn't in Touch mode
Related
I have implemented a RecyclerView and I have set it up to use CAB. But how can I highlight the selected items? If a certain position I checked I stored in a SparseBooleanArray.
My first thought was to store the specific View containg all the elements in my ViewHolder and then in onBindViewHolder set the background somehow to: ?android:attr/activatedBackgroundIndicator
But how can I do that? Is that a useful approach?
I finally solved this by simply adding some minor things:
First of all, the items of the RecyclerView have to use this as background:
android:background="?android:attr/activatedBackgroundIndicator"
Then for the RecyclerView simply call:
setSelected(true); on the indiviual views.
If you want to change the View itself, you need to dispatch adapter.notifyItemChanged(position) and in return, recycler view will call onBind method where you can set the background.
If you don't need to update the view itself, I would suggest using an item decorator.
Just like a ListView but instead it doesn't scroll. Its content is added programatically via an ArrayAdapter. Is there any view that can be used for this purpose?
PS: Not LinearLayout, since it doesn't get its content from an adapter (I want the observer pattern)
Edit: Let me explain a little bit more. Suppose you need a list of items, but the list itself is not scrollable, what is scrollable is the screen.
That being said, the list of items should show ALL items, not a limited amount based on a fixed height. The way to go is LinearLayout as it is both non-scrollable and shows all items within itself.
But there is a third requierement. You don't want to add(View) directly, but instead, you want something similar to an ArrayAdapter so that you have a control of the items and their position, so you can handle on item click events based on their position. As far as I know, this can't be done using a LinearLayout. So my question is, does any view exist for this purpose?
You could try using a ListView, but disable scrolling, as described here
Put your layout inside a ScrollView and yes you have to use a Linearlayout. You need to set tag for each of your view that you add dynamically and whenever your view is clicked you can get it's position by view.getTag() and perform the required operation.
Note : Adding views at run time may affect performance.
I set up adapter for ListView and in my inflaterLayout i put more one TextView to display Category, and when i get same category name that time i hide Textview from ListView and when i get the different category that time i set visible for TextView, so now i get perfect data but when i am trying to scroll the ListView that time each row changed automatically,
So what is the solution for it to prevent the changes the position of list items?
If you have any idea related to it, than please help me.
Thanks in advance.
Don't add any view at getView(). Instead, add the other TextView before, and set its visibilty to be 'gone'. At getView() check if you need this view, if so, set its visibility to be 'visible' otherwise, set 'gone ' (you must set the visibilty for every list item even if you believe that you don't have to do so).
How I can hide or delete certain listview separator? Of course I can hide all dividers
getListView().setDivider( null );
getListView().setDividerHeight(0);
but I need hide one or two dividers in my listview. for example by position. I am using custom Adapter for list data.
Thanks.
You can hide horizontal divider for disabled items of your list view (commonly used as section headers). To do this return false in areAllItemsEnabled BaseAdapter callback. Again, this only works for those views that are disabled (you return false in isEnabled callback for this item). The documentation for this callback is a little vague:
Indicates whether all the items in this adapter are enabled. If the
value returned by this method changes over time, there is no guarantee
it will take effect. If true, it means all items are selectable and
clickable (there is no separator.)
Reference.
Note that it mentions separator. I'm not sure if this is intended behavior or some kind of side effect. But it works. You can see this in ApiDemos List demo 5 (Separators).
You can either create a custom View for each ListItem where you can turn on or turn off the separator, or you can create a separator view that you add into your list view at the proper locations.
When you touch some grid or listview items in an android app, he change the background color telling you that you have pressed it.
I'm having a problem in my app. When I create that a listview for instance, and I press it in my phone, he change color, but if I set the OnClick property, the item dont change it color. Whats wrong? The OnClick works as its suppose.
Thanks
Generally with ListView you want to use an OnItemClickListener.
This gives you a chance to perform an action based on the position in the list that was clicked.
If you need access to the view adapter from within onItemClick then you can use AdapterView#getAdapter() on the AdapterView that is supplied to you.