Android GridView causing lagg - android

I am currently working on a file sharing application. I created a GridView, where I would like to list the already downloaded files with a thumbnail. To achieve this, I use a custom adapter. It loads fine, everything is visible, seems perfect.
And the trick is the following: after the view is loaded, the getView method of my custom adapter is called on position 0 so many times, that it causes massive laggs, with about 12% CPU usage on my Xperia XZ Premium.
I already read answers about laggs in GridView, but in those cases it was caused by scrolling. In my case, no scroll is needed, currently there is only 1 item in the GridView, but it is still struggling.
While lagging, it also spams a message. This message can disappear, if I add the following line of code to my TextViews:
android:singleLine="true"
But this attribute is deprecated and doesn't solves the issue, only the message disappears.
The lagg is not present, when the view is empty.
The getView method of my adapter:
#Override
public View getView(int position, View convertView, #NonNull ViewGroup parent){
View listItem = convertView;
if(listItem==null){
listItem = LayoutInflater.from(getContext()).inflate(R.layout.downloaded_grid_item, parent, false);
}
final Downloaded current = getItem(position);
final ImageView thumbnail = (ImageView)listItem.findViewById(R.id.downloaded_grid_item_thumb);
final TextView name = (TextView)listItem.findViewById(R.id.downloaded_grid_item_name);
final TextView subtext = (TextView) listItem.findViewById(R.id.downloaded_grid_item_subtext);
thumbnail.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
int width = thumbnail.getMeasuredWidth();
thumbnail.setLayoutParams(new LinearLayout.LayoutParams(width, width));
return true;
}
});
listItem.setTag(position);
thumbnail.setTag(position);
name.setTag(position);
subtext.setTag(position);
name.setText(current.getName());
subtext.setText(DateFormat.getDateInstance().format(new Date(current.getTime())));
thumbnail.setImageBitmap(ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(current.getPath()), 400, 400));
return listItem;
}
The GridView:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/downloaded_grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:layout_margin="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
The custom layout of the items:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/downloaded_grid_item"
android:background="#drawable/downloaded_grid_item_background"
android:orientation="vertical"
android:padding="4dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/downloaded_grid_item_thumb"
android:layout_width="match_parent"
android:layout_height="90dp"
android:src="#drawable/ic_menu_gallery"
android:scaleType="centerInside"/>
<TextView
android:id="#+id/downloaded_grid_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
tools:text="name"
android:ellipsize="end"
android:lines="1"/>
<TextView
android:id="#+id/downloaded_grid_item_subtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
tools:text="download date"
android:ellipsize="end"
android:lines="1"/>
</LinearLayout>
And the message, spammed by the device:
W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1

After running your code in an emulator (Android 7), I did not manage to get the message about the maxLineHeight but I can confirm that there was no end of getView() being called.
The reason for this behavior is that each time getView() is executed you add a new OnPreDrawListener without ever removing it. Changing the ImageView's height forces a recalculation of how it is to be drawn. This causes the whole GridView to go through a new layout pass (measure - draw - layout). So getView() is called again (normally at least twice for position 0). Because you never remove a OnPreDrawListener they all will fire once more, only causing more and more work for the CPU without actually changing the value for the ImageView's height.
You need to remove the Listeners you add to a ViewTreeObserver as soon as you obtained the desired information. So you need to write
thumbnail.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
thumbnail.getViewTreeObserver().removeOnPreDrawListener(this);
int width = thumbnail.getMeasuredWidth();
thumbnail.setLayoutParams(new LinearLayout.LayoutParams(width, width));
return true;
}
});

Related

Android Mystery: Text Disappearing in random TextView

I have a problem in my application Life Gallery. In this application, I have a fragment that displays the user's media directory in a gridView. Here is a screenshot :
As you can see, each directory is represented with an ImageView and a TextView, this latter containing the name of the directory. If you check the third line, you will see that the text of two TextView are empty...This is my bug. The elements of my gridview that present such a bug varies if I scroll or if rotate my phone...
Here is my layout for an element of the gridView :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_global"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="1dp" >
<ImageView
android:id="#+id/directory_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/content_description_album_imageview" />
<TextView
android:id="#+id/directory_text"
style="#style/Act_MyOwnLife_TextView_large"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/directory_image"
android:background="#color/black_transparent"
android:gravity="right"
android:paddingRight="4dp"
android:singleLine="true"
/>
</RelativeLayout>
And here is the code for my Adapter, focusing on the function getView:
#Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder viewHolder = null;
if (view == null) {
LayoutInflater inflater = this.activity.getLayoutInflater();
view = inflater.inflate(R.layout.fragment_directory_gridview, parent, false);
viewHolder = new ViewHolder();
viewHolder.relativeLayout = (RelativeLayout)view.findViewById(R.id.layout_global);
viewHolder.textViewDirectory = (TextView) view.findViewById(R.id.directory_text);
viewHolder.imageViewDirectory = (ImageView) view.findViewById(R.id.directory_image);
view.setTag(R.id.viewHolder, viewHolder);
}else{
viewHolder = (ViewHolder) view.getTag(R.id.viewHolder);
}
viewHolder.relativeLayout.setLayoutParams(this.viewFragmentDirectory.layoutParams);
viewHolder.imageViewDirectory.setTag(position);
viewHolder.imageViewDirectory.setImageBitmap(null);
viewHolder.position = position;
ModelDirectoryAndMedia directoryAndImage =this.directoriesAndMedias.get(position);
viewHolder.textViewDirectory.setText(directoryAndImage.directoryView);
ImageView imageView = viewHolder.imageViewDirectory;
// creation/retrieve the thumbnail with a thread
job = new Job(imageView,
directoryAndImage.media, Options.OPTIONS_THUMBNAIL,
position);
this.bitmapCreate.addJobThumbnail(job);
return view;
}
What I did to find the bug :
I check that my String was not empty when I call "viewHolder.textViewDirectory.setText(s);"
I try to check with hierarchyViewer, but I can not browse an element if the TextView is empty...I do not know why...
I remove the transparency of the background, remove the background property of the textView, I set "ImageView.setImageBitmap(null);" to check that it was not due to the ImageView...
But without success...the bug was still here...
After more test, I found the reason of the bug: it is due to this line of code in the getView method :
viewHolder.relativeLayout.setLayoutParams(this.viewFragmentDirectory.layoutPara‌​ms);
I use this line to display the empty ImageView to the correct size. So that when the thumbnail is ready, the insertion in the ImageView will not change its size ( the settings of my LayoutParams is done with the size of my thumbnail )
Any idea ? any comments on my code ?
Thanks a lot !
Ok...The bug was due to the call to :
viewHolder.relativeLayout.setLayoutParams(this.viewFragmentDirectory.layoutParams);
But I do not understand why...anyway, I did this to ensure that my ImageView had the correct initial size to display the thumbnail, loaded by a thread.
So I found an other solution to set a minimum size to the ImageView even if they are empty :
viewHolder.imageView.setMinimumHeight(this.viewControlerAlbum.sizeThumbnail);
viewHolder.imageView.setMinimumWidth(this.viewControlerAlbum.sizeThumbnail);
My layout.xml file is a bit updated :
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/content_description_album_imageview"
android:scaleType="centerCrop"
android:adjustViewBounds="true"/>
I added the adjustViewBounds attribute.

How to tell which of multiple getView results is visible?

I need to add custom button objects to each row in a ListView. Here's a simplified row layout:
<LinearLayout android:id="#+id/table_cell"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView android:id="#+id/label"
android:textSize="19dp"
android:textStyle="bold"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:lines="1"
/>
<LinearLayout android:id="#+id/button_wrapper"
android:layout_width="100dp"
android:layout_height="match_parent"
/>
</LinearLayout>
In my custom ArrayAdapter, I place the button into the cell in getView():
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// recycle the cell if possible
View cell = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
cell = inflater.inflate(R.layout.table_cell, parent, false);
} else {
cell = convertView;
}
MyButton button = (MyButton) this.buttons.get(position);
if (button != null) {
// remove the button from the previous instance of this cell
ViewGroup parent = (ViewGroup)button.getParent();
if (parent != null) {
parent.removeView(button);
}
// add the button to the new instance of this cell
ViewGroup buttonWrapper = (ViewGroup)cell.findViewById(R.id.button_wrapper);
buttonWrapper.addView(button);
}
}
I know that getView() is called multiple times for each table row as I scroll the table or click buttons or do other things, so the code above removes the button from the previous view before adding it to the new view to avoid a "view already has a parent" exception.
The problem is that this assumes the latest view generated from getView is the one that's visible on the screen, but this is often not the case. Sometimes getView() generates new views, but an older view remains on the screen. In that situation, my button disappears because getView() moves it to a new view that is not visible. I discovered that behavior by initializing an int variable named repeatRowTest and then adding this code inside getView():
if (position == 0) {
Log.d("getView", "repeat row count: " + repeatRowCountTest);
TextView label = (TextView)cell.findViewById(R.id.label);
label.setText(String.format("%d %s", repeatRowCountTest, label.getText()));
repeatRowCountTest++;
}
This shows me how many times a given row has been generated, and which instance is currently displayed. I might see a row being generated 10 times, while only the 5th one is displayed. But my buttons will only be visible if the latest instance of the row is displayed.
So the question is, how can I tell whether a row generated in getView() is actually going to be displayed, so I know whether to move my button into it, or leave my button where it is? Or more generally, how can I add a button to a row and make sure it remains visible as getView is repeated for a given position?
I've inspected all the properties of a displayed row versus an extra, non-displayed row, and couldn't find any differences. I also tried calling notifyDataSetChanged on the array adapter after my buttons disappear, and that refreshes the list with all the latest views that contain the buttons -- but it's not clear which events trigger getView to repeat itself, so I wouldn't know when I need to call notifyDataSetChanged to make things right again. I suppose I could clone the button and add a new instance of the button to each new instance of the row, but that seems more resource-intensive than is necessary, and will create other problems since other objects have references to these buttons. I haven't found any code examples showing the best way to do this, but it seems like a common requirement, so hopefully I'm missing something simple!
UPDATE: Is there a method of the ArrayAdapter I can override that is called after the getView() methods are called? If so, I could check the parents of all the recently created rows to see if they are actually displayed in the ListView, and refresh the ListView at that point if they aren't.
You don't need to create your custom button by code, you can insert it inside the row layout xml like a normal android button. In this way you can remove the button wrapper layout and the add/remove logic from getView.
<LinearLayout android:id="#+id/table_cell"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView android:id="#+id/label"
android:textSize="19dp"
android:textStyle="bold"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:lines="1"
/>
<yourpackagename.MyButton
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="match_parent"
/>
</LinearLayout>
Is simpler to understand with code, but maybe you have to adapt it.
XML:
<LinearLayout android:id="#+id/table_cell"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView android:id="#+id/label"
android:textSize="19dp"
android:textStyle="bold"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:lines="1"
/>
<yourpackagename.MyButton
android:id="#+id/button1"
android:layout_width="12dp"
android:layout_height="match_parent"
/>
<yourpackagename.MyButton
android:id="#+id/button2"
android:layout_width="12dp"
android:layout_height="match_parent"
/>
<yourpackagename.MyButton
android:id="#+id/button3"
android:layout_width="12dp"
android:layout_height="match_parent"
/>
<yourpackagename.MyButton
android:id="#+id/button4"
android:layout_width="12dp"
android:layout_height="match_parent"
/>
<yourpackagename.MyButton
android:id="#+id/button5"
android:layout_width="12dp"
android:layout_height="match_parent"
/>
<yourpackagename.MyButton
android:id="#+id/button6"
android:layout_width="12dp"
android:layout_height="match_parent"
/>
<yourpackagename.MyButton
android:id="#+id/button7"
android:layout_width="12dp"
android:layout_height="match_parent"
/>
<yourpackagename.MyButton
android:id="#+id/button8"
android:layout_width="12dp"
android:layout_height="match_parent"
/>
</LinearLayout>
Model class that you pass to the Adapter:
public class MyRowModel
{
public boolean isButton1Visible;
public boolean isButton2Visible;
public boolean isButton3Visible;
public boolean isButton4Visible;
public boolean isButton5Visible;
public boolean isButton6Visible;
public boolean isButton7Visible;
public boolean isButton8Visible;
}
ViewHolder:
private class ViewHolder {
public MyButton b1;
public MyButton b2;
public MyButton b3;
public MyButton b4;
public MyButton b5;
public MyButton b6;
public MyButton b7;
public MyButton b8;
}
getView method:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.table_cell, parent, false);
viewHolder = new ViewHolder();
viewHolder.b1 = (MyButton)convertView.findViewById(R.id.button1);
viewHolder.b2 = (MyButton)convertView.findViewById(R.id.button2);
viewHolder.b3 = (MyButton)convertView.findViewById(R.id.button3);
viewHolder.b4 = (MyButton)convertView.findViewById(R.id.button4);
viewHolder.b5 = (MyButton)convertView.findViewById(R.id.button5);
viewHolder.b6 = (MyButton)convertView.findViewById(R.id.button6);
viewHolder.b7 = (MyButton)convertView.findViewById(R.id.button7);
viewHolder.b8 = (MyButton)convertView.findViewById(R.id.button8);
convertView.setTag(viewHolder);
} else {
viewHolder = convertView.getTag();
}
MyRowModel myRowModel = getItem(position);
if(myRowModel.isButton1Visible)
{
viewHolder.b1.setVisibility(View.VISIBLE);
}
else
{
viewHolder.b1.setVisibility(View.INVISIBLE);
}
if(myRowModel.isButton2Visible)
{
viewHolder.b2.setVisibility(View.VISIBLE);
}
else
{
viewHolder.b2.setVisibility(View.INVISIBLE);
}
//and so on
return convertView;
}
I noticed that if I scroll the ListView after the problem occurs, all the rows redraw with the buttons showing, so apparently Android intends to display the latest view for each row, but it isn't always refreshing the view.
Then I tried to figure out what is causing getView to run repeatedly for the currently visible rows (normally it would only run when new rows come into view). Unfortunately, lots of things that are happening elsewhere in this activity are triggering the ListView to regenerate its views, like a ProgressBar that moves as audio plays, an animation that shortens and lengthens the ListView to show another view next to it, and the buttons inside the table rows updating with different graphics to show the status of different things the app is tracking. I was able to eliminate some of this, for example by checking to see if a button is already in the desired state before updating its state, but I can't eliminate all of it.
Since the most frequent action that triggers getView is updating the audio ProgressBar, I added a line to call invalidateViews() on the ListView whenever I update the ProgressBar. That keeps the ListView refreshed so that the latest views always remain visible and therefore my views always remain visible. When running in the debugger, that slows the app down quite a bit, but when running on a standalone device, the performance change isn't noticeable.
Perhaps a better question to ask at this point is why a ProgressBar that isn't related to the ListView causes the ListView to constantly regenerate its views. If I have time or I run into more problems with this, I'll post that as a separate question.

convertView loses onitemClick after the listView is scrolled

I'm having a slightly weird error here on my adapter.
The view the adapter is creating is a thumbnail on the left side and a table with a few rows. Each row have two textviews.
One of the textviews have the android:autoLink="web" property set and the listview have an onItemClickListener on it.
the problem is that every time a TextView auto-links it's content, next time its parent view is converted, it doesn't receive clicks from the onItemClickListener anymore.
Let me clarify with an example:
view1, view2, view3 and view4 are on the list view on the screen.
view2 have a link and it appears, and onClick the link opens.
the item click works normally for view1, view 3 and view4.
scroll the listview and view1 is converted to position5 and then view2 is converted to position6.
the item at position6 does not contain a link, but the onItemClick is also not fired for the position6 element.
the autolink feature of the textview is certainly changing something with my layout, but I don't know what. There must a property I can reset for every call to getView on my adapter, but which?
thanks for any help.
edit
let's see some code, it's pretty standard/good practices.
the getView from my adapter is:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate a new layout if needed
if (convertView == null)
convertView = createNewView();
// Gets the item from my array
AGplus item = (AGplus) getItem(position);
// Gets the holder pointing to the views
Holder h = (Holder) convertView.getTag();
// That's a test version, I won't be using date.toString() later on
h.date.setText(new Date(item.getDate()).toString());
// This guys is giving me a headache,
// If it parses the link, I can't never click on this convertView anymore,
// event re-converting them for a text that does not contain links
h.txt.setText(item.getTitle());
// some image download stuff that doesn't matter for this code
return convertView;
}
that layouts used is a image and table and the amount of rows I inflate and insert on the table varies for each adapter. The table layout is a horizontal linear layout with a imageview and the table layout with some margin stuff and here is the row layout:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/text" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/text" />
</TableRow>
if I completely remove the android:autoLink="web" I get all the clicks, but as stated before, once a view gets "auto-linked" and then I recycle that view, I can't get clicks on that view again.
edit
and here is the layout inflation:
private View createNewView() {
// Instantiate view
View v = getLayoutInflater().inflate(R.layout.expandable_child_view, null);
TableLayout table = (TableLayout) v.findViewById(R.id.table);
Holder h = new Holder();
v.setTag(h);
// Instantiate rows
h.thumb = (ImageView) v.findViewById(R.id.img);
h.date = (TextView) createNewRow(table, "Date: ");
h.txt = (TextView) createNewRow(table, "Text: ");
return v;
}
private View createNewRow(ViewGroup group, String title) {
View row;
row = getLayoutInflater().inflate(R.layout.item_table_row, null);
((TextView) row.findViewById(R.id.title)).setText(title);
group.addView(row);
return row.findViewById(R.id.text);
}
and before someone else asks, that's the expandable_child_view layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:src="#drawable/ic_launcher" />
<TableLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TableLayout>
</LinearLayout>
as I said before, just a linear layout with a imageview and a table and a few margins.
According to Romain Guy here, this is done by design to support trackball/dpad navigation. Comment 27 has a workaround to it, by setting descendant focusability on each listview item:
setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
or
android:descendantFocusability="blocksDescendants"
I think I know what may be going on. When you do setText(), sometimes it'll wipe out a lot of stuff. You may have to use a ClickableSpan to put the link action back into the textview.

Click Event on ImageButton not dispatched immediately

I'm having a weird problem, in my rather complex view layout. (I will try to simplify it a bit in my explanation)
Basically I have a ListView, where each item consists of a TextView and an ImageButton. I am able to either click the list item (on the textview), or the button (I set the ImageButton to non-focusable, otherwise it wouldn't work)
Now it seems to work fine, until I open another window and return to the listview.
From that point on, I can click the ImageButton without anything happening (not even the background changes during the click). But when I click on the TextView again, all the click events from the ImageButton are dispatched at once.
Why is that?
EDIT:
The List Item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0px"
android:minHeight="40dp"
android:orientation="horizontal"
android:paddingLeft="2px"
android:paddingRight="2px"
>
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Text"
android:textSize="19dp"
android:paddingTop="4px"
android:paddingBottom="4px"
android:layout_gravity="center_vertical"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/open_subtree_layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="0px"
android:orientation="horizontal"
android:padding="0px">
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#drawable/separator_line" />
<com.treeviewer.leveldisplay.DontPressWithParentImageButton
android:id="#+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:background="#drawable/list_selector_background"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="10dp"
android:src="#drawable/arrow_right" />
</LinearLayout>
</LinearLayout>
That's how it is inflated:
[...]
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = inflater.inflate(R.layout.tree_row, null, false);
TextView textView = (TextView)mView.findViewById(R.id.text1);
LinearLayout nextNodeButtonContainer = (LinearLayout)mView.findViewById(R.id.open_subtree_layout);
if(childCount >= 0) {
titleBuilder.append(" (" + childCount + ")");
nextNodeButtonContainer.setVisibility(View.VISIBLE);
View button = nextNodeButtonContainer.findViewById(R.id.btn_right);
button.setFocusable(false);
button.setFocusableInTouchMode(false);
//button.setClickable(true);
button.setOnClickListener(clickListener);
button.setTag(tagValue);
} else {
nextNodeButtonContainer.setVisibility(View.GONE);
}
textView.setText(titleBuilder);
Let me know, if you need more code.
Ok, I finally solved this problem.
Unfortunately, in my question I didn't provide the necessary information to solve it, as the problem was somewhere I didn't expect it:
I have a ListAdapter where the getView method looks like this:
public View getView(int position, View convertView, ViewGroup parent) {
return mNodes.get(position).getView(mNodeArrowClickListener, position);
}
And the getView method of the nodes (TreeLevelElements) looked like:
public class TreeLevelElement {
private final Context mContext;
private View mView = null;
//[...] other methods
View getView(OnClickListener clickListener, final int tagValue) {
if(mView == null) {
//[...] produce a new View from XML
}
return mView;
}
}
The problem was, that I stored the Views in my elements, so I guess that conflicted somehow with android strategy to reuse old views for new items.
I don't know what exactly happened, but now that I removed mView and create a new one every time, it works.
I will also change it to reuse the convertView instead.

ListView element not clickable

I have a ListView with SimpleCursorAdapter. The layout was using a LinearLayout, but when reading manuals about memory consumption for complex (nested) layouts, I switched to RelativeLayout instead and I've managed to setup the layout exactly as I want.
Now, there's one quirk. In the emulator, I cannot click the list items. It seems as if the elements are one big "no button". However, if I use the emulator arrows and select the listview element (highlights it) and click the button, it works fine.
Why can't I "click" the listview items since I switched to RelativeLayout?
Here's the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/locationName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Junk text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/numStores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/locationName"
android:text="Junk text #1: 117"
android:textSize="10dp" />
<TextView
android:id="#+id/numItems"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/numStores"
android:text="Junk text #2: 42"
android:textSize="10dp" />
</RelativeLayout>
I even tried android:clickable="true" for RelativeLayout, to no avail.
EDIT
Code for onClick is as follows:
listItems.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
int countyID = cursor.getInt(cursor.getColumnIndex(LocationDB.C_ID));
int numItems = cursor.getInt(cursor.getColumnIndex(LocationDB.C_NUM_ITEMS));
int numStores = cursor.getInt(cursor.getColumnIndex(LocationDB.C_NUM_STORES));
String countyName = cursor.getString(cursor.getColumnIndex(LocationDB.C_NAME));
String foo = String.format(TAG + "Clicked ID #%d (%s), stores = %d, items = %d", countyID, countyName, numStores, numItems);
Log.i(LOG_TAG, foo);
// Show the item in a new activity
Intent apan = new Intent(avBase, Browser.class);
apan.putExtra("countyID", countyID);
startActivity(apan);
}
});
EDIT 2
The code is now tested on my phone, and I get the same error; can't click the items when using RelativeLayout.
I faced some issues with this, too, which seemed to be related to the combination of the ListView's on click listeners and those I assigned each individual item in my Adapter.
In my Adapter.getView() I had to make sure the items were configured like:
item.setLongClickable( false );
item.setClickable( false );
item.setOnClickListener( null );
while my ListView had to have its OnItemClickListener and/or its OnItemLongClickListener set.
Turning on clicks on the individual items and on the listview would not work. Maybe that's what's causing your issue, too.
Seems this is a bug that Google ignores: http://code.google.com/p/android/issues/detail?id=3414

Categories

Resources