views:

239

answers:

0

My Question - I want the text of a TextView I have in a custom ListView to marquee scroll ONLY when the item is selected in the ListView. When the list item loses focus, I want the marquee to stop scrolling. I know that I need to set the TextView to enabled (setEnabled(true)) for scrolling to work however when I do this as items are added, they ALL scroll, which is not the behavior I want (but it does make sense why this happens). I tried setting them programmatically using the click handler of the ListView to set the TextView to enabled (setEnabled(true)), keep track of the current selected item in a private variable, then conversely setting the previous Views TextView enabled status to false when a new item is clicked and so on. I hope this makes sense. Here's some Java code that might better illustrate it:

    private View tempListItemView = null;

    if (this.tempListItemView != null) {
       TextView tempTxtView = (TextView) tempListItemView.findViewById(R.id.txtArticleTitle);
       tempTxtView.setSelected(false);
       tempTxtView.setEllipsize(TruncateAt.END);
    }
    // Set marquee of currently selected list item
    TextView tt = (TextView) v.findViewById(R.id.txtArticleTitle);
    tt.setEllipsize(TruncateAt.MARQUEE);
    tt.setSelected(true);
    tempListItemView = v; 

This does work, sort of. However, it's a little clunky. For example if the previous item leaves the display it never gets turned off and when I select the list item, I lose all of the custom listSelector properties I have set (see below). Also, I've had some other odd behavior with the background item of the scrolling TextView changing to black (and even the entire ListView listSelector disappearing entirely. None of this occurs when I use the keypad on my Droid. It works as it should, however I'm just trying to emulate this functionality when list items are pressed. I haven't found much on the web on how to achieve this. Below are snippets and screen shots.

I have a ListActivity that I've bound to an ArrayAdapter with a custom list view. The content view that this activity is tied to looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:listSelector="@drawable/list_selector"
    />
</LinearLayout>

The list_selector drawable looks like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  <item android:state_focused="true" android:drawable ="@drawable/maroon_list_select_bg" />
  <item android:drawable="@drawable/maroon_list_select_bg" />
</selector>

Its Drawable is simply a 1x1 maroon pixel to paint a maroon background color of the list item selection. In the interest of saving space, I've attached a screenshot of what the custom view looks like: (my first post so I couldn't :(

and here is a brief snippet of the custom layout that defines each list item (with the marquee TextView)...

            <TextView
                android:id="@+id/txtArticleTitle"
                android:textSize="16sp"
                android:textStyle="bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/list_item"
                android:singleLine="true"
                android:ellipsize="end">  
            </TextView>
            <TextView
                android:id="@+id/txtArticleSnippet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/list_desc_copy"
                >
            </TextView>
            <TextView
                android:id="@+id/txtArticleVotes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Votes: 1576"
                android:textColor="@color/list_small_copy"
                >
            </TextView>

Please remember, I do know that I need to set android:ellipsize to "marquee" to get the scrolling but as explained above, I was doing this programmatically to currently selected list tiems.

Does anyone have a solution for this? Thanks in advance and I apologize for the lengthy post...just trying to be thorough. Thanks again!