Make a listview programmatically selectable only - android

I have a Listview in a Fragment, and when I click an item a new Fragment is shown.
The problem is that for few milliseconds i can see the item selected by the touch. Is there a way to make these Listview programmatically selectable only? For example when I return to the Fragment and something connected is running (i already know how to select an item in Java, i just need to know how to disable touch clicks).
The item is colored with a selector and the Listviews are set to choice mode single.

One way to do it would be to implement the ontouchListener() of the fragment, and rewrite the onTouch() method with a simple:
return false
to consume the event.

You can achieve this quite easily. The two ways that are on top of my head are :
Disable touch on the list.
Put your ListView in a RelativeLayout (rootLayout) in the RelativeLayout(rootLayout) first item should be your ListView and the second can be another RelativeLayout(coverLyt) with height and width set as match_parent and clickable set to true. This will make the coverLyt take the touch events instead of your ListView. When you want the listView touch events to work set coverLyt's visibility to gone and visible when vice versa.

Related

Android: Detect if user touches a view inside parent view

I have 40 ImageViews inside a GridLayout, they have different colors and I want to know if user touched the desirable image or somewhere else(for example if user touched red image). How should I do that?
Set an OnClickListener for each view and store the views. In onClick you can check the view and know what ImageView was clicked. You could also think about changing the type to ImageButtons!
If you have further problems with your Grid not being able to be clicked, check this out: http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/
TLDR: Disable focusing of the views in your layout.
If you manage your images in a collection maybe think about switching to gridview and implement an adapter with an itemClickListener. So depend on which item is clicked do you action.
How GridLayout items come from the Adapter(List/Image) in Android App

Android - Why setClicable(true) sets item non-clicable and vice versa?

According to developers' site View.setClicable(true) should
make the view clickable, false otherwise
, so why is it reversed in real? Only setClicable(false) makes my View clicable. Have I found a mistake in API description?
EVIDENCE:
gridview.getChildAt(position).setClickable(true);
Feel free to try it at home. After this the child in gridview won't be clicable.
I assume the clicking you're referring to is what you can listen to with an OnItemClickListener. This is handled entirely by the GridView (the GridView is also what draws the selector on the view), not the child view. You can control which items the GridView should consider as clickable with isEnabled in your adapter.
When you make the child view clickable it will handle all touch events over it. Because of this the GridView will not be notified of touch events on that view, and as such can't handle the click event.
So your view is perfectly clickable, you'd just have to use an OnClickListener to get notified of the clicks.
I think you have misunderstood what is is saying. It says exactly what you said. You pass true to make the view clickable or false otherwise (i.e to make it not clickable).

Android listview with buttons

I have a listview with custom listitem. I have bunch of textviews in Relative layout. And when I click on listitem it goes to different screen. Now I have added 3 buttons and toggle button to list item and I CAN NOT CLICK THE LIST ITEM. What am I missing :(
set focusable property of all the buttons to false and so that you can click on list and also touch event for all buttons will work
When the user clicks the row, the ListView will look for a widget implementing the Checkable interface. The RelativeLayout does not. An easy way to do that is simply to create your own CheckableRelativeLayout class. See http://www.marvinlabs.com/2010/10/custom-listview-ability-check-items/ for a full tutorial with code.
Also, the buttons and other items in your row layout should not be focusable. You can see other SO questions: Android Row becomes Unclickable with Button

Trying to alternate between GridView & ListView leaving artifacts

I have an app whose default style is to present data in a GridView. When the user presses the Menu button they can choose to present the data as a ListView.
The problem is that once the first switch happens, it's as though the ListView is simply superimposed above the GridView. The GridView layout doesn't go away. I have to scroll with my finger a little bit before it disappears.
What I'm doing is basically setting another setContentView call to my XML file that defines the ListView. It's not just a simple matter of changing the adapters.
I am guessing that I need to completely destroy or remove my GridView--but how do I do this? Note that if I switch from ListView to GridView the problem remains.
Consider putting both in a ViewFlipper, rather than using setContentView().
for list
gridview.setNumColumns(1);
and for grid
gridview.setNumColumns(2);
What about playing with visibility?
When List is selected in menu, make gridview visibility to 'GONE',and list to 'VISIBLE'
and vice versa when Grid is selected in menu

how to scroll up when choose some item at item list in android?

I jave a list of item (linearlayout inside a scrollview where i add buttons vertically to linearlayout dynamically from the java code ) ? i need when i click on one button the item moves up (scroll up) , to make the item at the first of the screen ??!
Well I never did that, but from the ListView reference it seems pretty clear that set setSelectionFromTop is what you are searching for.
See also how-to-autoscroll-a-list-view-in-android

Categories

Resources