ListView OnItemClickListener Not Responding? - java

I've looked everywhere for a solution to this, but I can't figure out how to implement it. My OnItemClickListener was disabled somehow on my ListView rows, because I have an ImageButton in the row layout, which takes over the focus. There have been numerous questions I've found, but none of them have gotten me anywhere.
I've checked this question, but I couldn't really make heads or tails of it. I just need a way to get the rows clickable so that I can detect when a row is pressed. Long press and focus work fine.

Instead of an OnItemClickListener, add an OnClickListener to each of your views returned from your adapter. You'll need to use setItemsCanFocus setting up your list:
ListView list = (ListView) findViewById(R.id.myList);
list.setAdapter(new DoubleClickAdapter(this));
list.setItemsCanFocus(true);
and then in your Adapter's getView, this will yield a clickable row. The button is assumed to be in the inflated xml.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(context, R.layout.cell, null);
view.setClickable(true);
view.setFocusable(true);
view.setBackgroundResource(android.R.drawable.menuitem_background);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
new AlertDialog.Builder(context).setTitle("touched").show();
}
});
return view;
}

set your ImageButton's attribute:
android:focusable="false"
Because AbsListView.onTouchEvent check child.hasFocusable().

I've tested the following solution on SDK levels 8 and 16.
In getView()
setFocusable(false);
setClickable(false);
rather than setting them true in the Adapter's getView() does what I think the original question wanted, and means that an OnItemClickListener gets called, provided that an OnClickListener is not set in getView().
I'm assuming that anything you can do in an View's OnClickListener you can do just as easily in a ListView's OnItemClickListener.
(setOnClickListener on a View implicitly sets the view to be clickable, which prevents the ListView's corresponding OnItemClickListener getting called, apparently.)
The behaviour is as one would expect, in terms of the ImageButton's visual state when the item is pressed or rolled over.
The solution is a slight illusion, in that it is the list item that's being pressed not the ImageButton itself, so if the button doesn't occupy whole list item, clicking somewhere else in the item will still make the button's drawable state reflect the click. Same for focus. That might be a price worth paying.

This will definitely work. Add this to the layout definition.
android:descendantFocusability="blocksDescendants"
Found the solution here

One alternative to setting an OnClickListener for every view is to NOT use an ImageButton - use an ImageView instead. The ImageView can still send events to an OnClickListener and won't take over the focus.

best way to do is this:
android:focusable="false"
android:focusableInTouchMode="false"
set these properties for that Imagebutton and try.
I

For my version of this problem, the issue was that I had set my TextView object to android:inputType="textMultiLine". When I removed this line the issue of the list not being clickable was gone. Looks like a nasty little bug.
Also, I'm still able to use the android:minLines/android:maxLines properties with no problem, so it's not a big issue. Just not the solution I expected.

The following line solved the issue in my project:
<TextView ... android:textIsSelectable="false" />

As an alternative solution which worked for me you can try to extend your adapter from BaseAdapter (iso implementing ListAdapter interface)

Put This code ImageView nextpage= (ImageView)findViewById(R.id.btnEdit); instead of ImageButton . now the list item is active

I have sub-classed ImageButton and setFocusable="false" in layout definition didn't work for me. It solved calling setFocusable(false) in constructor of subclass.

Using a ScrollView can prevent the onItemClickListener from receiving the input.
Hope this helps anyone.

Related

Android: Need to reference view when using findViewById?

This comes more out of curiosity. I have had this snippet:
View mView = mInflater.inflate(R.layout.myButton, null);
ImageButton button = (ImageButton) mView.findViewById(R.id.mButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
...
And first I tried it without the mView-reference and it works as expected. So it is also described in the Buttons-section of developer.android.com
However, with mView also works.
My question is, is it really needed to reference it? And what could be the purpose behind it?
Why it works both ways? Could there be any drawbacks of any of the given usages?
Thanks.
Why it works both ways?
if it works both ways it could mean two different things. First you passed myButton.xml to setContentView as well. In this case the Activity has, as part of its view hierarchy, a view with id mButton. It works as expected, but of course, the OnClickListener, in the case of the inflated layout is purpose less, unless you add the inflated view to the Activity's view hierarchy. If you didn't pass myButton.xml to setContentView, it means that you have two separate layouts that contain the same ImageButton with the same id. Also in this case are valid the considerations about OnClickListener.
You are in two case:
Case 1
(Button) findViewById(R.id.mButton);
You are in an activity, where Activity.findViewById() will browse the actual activity view tree.
This activity will have a view from the moment you use setContentView(), addContentView(..) or other inflater method
Case 2
(Button) mView.findViewById(R.id.mButton);
You are searching for a view in another view, using the View.findViewById(). This method is used for getting a view from a Fragment for example.

Java android baseAdapter set list item one time

Hello i have a fully working code for my list adapter:
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.overview_item, null);
//getting id's
TextView name =(TextView)vi.findViewById(R.id.userUsername);
TextView date =(TextView)vi.findViewById(R.id.imageDate);
ImageView image=(ImageView)vi.findViewById(R.id.userImage);
ImageView avatar=(ImageView)vi.findViewById(R.id.userAvatar);
//setting text
name.setText(dataNames.get(position));
date.setText(dataDates.get(position));
//set image
Log.d("test: ", "Adapter wants to get picture");
imageLoader.DisplayImage(dataImage.get(position), image);
imageLoader.DisplayImage(dataAvatars.get(position), avatar);
return vi;
}
This code works perfect but the problem is this function runs everytime when you scroll throught the listview so whenever the lis item is getting in sight. And that's not what i want. i want it to do this function just once for every list item. This is because when your scrolling fast trought the list it has to load all images again so the loading image is showing and it keeps jumping because the loading image is another size then the image wich is getting loaded. I hope thay tou understand my question and can help me. Already thanks and if i'm not clear please ask my anything in the comments.
So short:
How do i run this code just once for every list-item and not everytime when it's getting in sight?
Your code might call findViewById() frequently during the scrolling of ListView, which can slow down performance. Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. A way around repeated use of findViewById() is to use the "view holder" design pattern.
Check this links:
1 - http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html
No, you should not. This is the way ListView works. Beside, you should use ViewHolder pattern for better performance.
If you still want to do this, you could remove check NULL with convertView. It will solve your problem, but lead to performance, I think.

How to implement pull to refresh on a ListFragment

I'm trying implement "pull to refresh" on a ListFragment but right now none of the drop in libraries seem to support it. There's no way to detect overscroll on the list fragment that I can see so I'm wondering if anyone has found a means to get this working?
--
Using Christian's tip I used the following for my onCreateView() method.
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
PullToRefreshListView listView = new PullToRefreshListView(getActivity());
mAdapter = new HomeTweetListAdapter(getActivity(), R.layout.tweet_list_item, tweets);
listView.setAdapter(mAdapter);
return listView;
}
Like Christian said you can only do this with a Fragment. Returning anything other than a ListView on a ListFragment errors out.
EDIT:
To clarify I am using Johan's PullToRefresh library
I actually make it work using fragments (not ListFragment). So it is basically the same, just return the PullToRefreshListView from your onCreateView method and that's it. It should also work with ListFragment; remember that you must return a ListView from onCreateView if you use ListFragment (you can return whatever you want if you use just Fragment).
Here is a Component that Johan has created that adds a pull down to refresh feature..
Pull-To-Refresh
Main logic is implemented in PullToRefreshListView that extends ListView.
n your layouts you simply add it like this.
<com.markupartist.android.widget.PullToRefreshListView
android:id="#+id/android:list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
It is now fully documented how to use ActionBar-PullToRefresh together with ListFragment:
https://github.com/chrisbanes/ActionBar-PullToRefresh/wiki/ListFragment
Hope that helps!

ListView in Preference

I've created a custon preference screen for my Android application (Honeycomb), that contains a ListView.
public class XXXPreferences extends Preference
{
#Override
protected View onCreateView(ViewGroup parent)
{
ListView...
}
}
Everyting is shown as expected, but the ListView is not scrolling. It seems that it is because the Preference class has a build in ScrollView and the ListView does not supported to be embedded in a ScrollView.
So I would like to know if it is possible to remove the ScollView of the Preference class or if there is another trick to make things work !?
Thanks in advance for your response :)
Cheers
If the preference class has a scrollview, you shouldn't need your ListView. Just use a vertical LinearLayout and add your layouts to that as needed.

Android ListView - onListItemClick does not work properly

I created a ListView in Android, and a corresponding ListActivity. Each individual item in the ListView has just one TextView (I plan to add an image and a CheckBox later).The ListActivity overrides the onListItemClick to perform certain tasks on click of any item on the list.
Heres whats happening -
When I first tried clicking on any item, nothing happened.
I then tried setting the properties "Focusable" and "Focusable in Touch Mode" to false for the TextView, as mentioned here, here and here. The List items started recognizing clicks, but only when I clicked somewhere away from the TextView. Whenever I tried clicking on the TextView or anywhere near it, it did not work.
I also tried changing various attributes like Clickable, but nothing has worked so far.
Any idea what I could be doing wrong ?
Thanks
After playing around with virtually every attribute in my TextView, I finally found the reason why it was not working. It was because of the attribute android:inputType="text" in my TextView. I'm not sure why I added that piece of code (I probably copied the TextView from one of my other applications), but removing it solves my problem.
Class which will listen clicks on ListView should implement interface AdapterView.OnItemClickListener

Categories