简体   繁体   中英

android : marquee on listview don't work

I write simple ListView but I have problem, I want to marquee text when I select some item from my listview but it not working I don't know why I think I write code correctly when I copy my code from listView.setOnItemSelectedListener(new OnItemSelectedListener() to listView.setOnItemClickListener(new OnItemClickListener() that's work correctly when I click, but I want this only for select. That's my code :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
    for (int i = 0; i < 7; ++i) {
        HashMap<String, String> hm = new HashMap<String, String>();
        hm.put("firstLine", menu[i]);
        hm.put("secondLine", submenu[i]);
        hm.put("icon", Integer.toString(ikons[i]));
        aList.add(hm);
    }
    String[] from = { "icon", "firstLine", "secondLine" };
    int[] to = { R.id.icon, R.id.firstLine, R.id.secondLine };
    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
            R.layout.alarm_row, from, to);
    ListView listView = (ListView) findViewById(R.id.list);
    listView.setAdapter(adapter);

    listView.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View view,
                int arg2, long arg3) {
            TextView t = (TextView) view.findViewById(R.id.secondLine);
            t.setSelected(true);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            /*
             * TextView t = (TextView) view.findViewById(R.id.secondLine);
             * t.setFocusable(true); t.setSelected(true);
             */
        }
    });
}

XML :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/panel1"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:background="@drawable/listview_bg"
    android:padding="6dip" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignBottom="@+id/secondLine"
        android:layout_alignParentTop="true"
        android:scaleType="center"
        android:src="@drawable/alarm_icon" />

    <TextView
        android:id="@+id/secondLine"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:paddingLeft="15dip"
        android:paddingRight="15dip"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="Alarm" />

    <TextView
        android:id="@+id/firstLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/secondLine"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:paddingLeft="15dip"
        android:paddingRight="15dip"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="Ustawienia Alarmu" />

</RelativeLayout>

I think that should work I don't have any idea what is wrong, I will be very gratefull for any help.

Try adding

t.requestFocus();

after

t.setSelected(true);

in onItemSelected

you should define a customized adapter, which extends eg BaseAdapter or which adapter you like and then add t.setSelected(true) in the getView method of that:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   View view = convertView;
   if (convertView == null)
    view = inflater.inflate(R.layout.row, null);

   TextView t = (TextView) view.findViewById(R.id.secondLine);
   text2.setSelected(true);
   return view;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM