Android ListView焦点搞砸了

时间:2011-12-08 14:42:36

标签: android listview focus

在我的应用程序中,我想实现一个ListView,每个itemview都是一个LinearLayout,其中有两个可聚焦的按钮,并且每个行项数据都是从页面中的服务器加载的。但似乎调用adapter.notifyDataSetChanged时,如果焦点位于itemview的右侧按钮,它将跳转到左侧按钮。我能以任何方式使焦点稳定吗?

以下是重现问题的示例代码。任何人都可以帮忙提一些建议吗?感谢。

package com.listviewfocus;

import java.util.Vector;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

public class SimpleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ListView list = new ListView(this);
        final MyAdapter adapter = new MyAdapter();

        list.setItemsCanFocus(true);
        list.setAdapter(adapter);

        setContentView(list);

        final Handler handler = new Handler();

        new Thread(new Runnable(){
            public void run() {
                while(true) {
                    handler.post(new Runnable() {
                        public void run() {
                            adapter.addItem(System.currentTimeMillis()+"");
                        }
                    });

                    try {
                        Thread.currentThread().sleep(5000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    class MyAdapter extends BaseAdapter {

        private Vector<String> items = new Vector<String>();

        public MyAdapter() {

        }

        public void addItem(String s) {
            items.add(s);
            this.notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LinearLayout view;

            if (convertView instanceof LinearLayout) {
                view = (LinearLayout) convertView;
            } else {
                view = new LinearLayout(parent.getContext());
                Button b1 = new Button(parent.getContext());
                Button b2 = new Button(parent.getContext());
                view.addView(b1);
                view.addView(b2);
            }

            ((Button)(((ViewGroup) view).getChildAt(0))).setText(position+":b1");
            ((Button)(((ViewGroup) view).getChildAt(1))).setText(position+":b2");

            return view;
        }

    }

}

0 个答案:

没有答案