开发者

android dev: item in listview can not get focus correctly

I face a listview focus problem. There is a button above the listview(not listview header). And I set listview.setItemsCanFocus to true. When I scroll using trackball, the items in listview can not get focused correctly. I post the layout file and code, hope this helps.

package com.gaocx.trackballtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;    
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
/** Called when the activity is first 开发者_StackOverflowcreated. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ListView listView = (ListView) findViewById(R.id.listviewId);
    Button headerBtn = new Button(this);
    headerBtn.setWidth(LayoutParams.FILL_PARENT);
    headerBtn.setHeight(LayoutParams.WRAP_CONTENT);
    headerBtn.setText("ListView Header");
    listView.addHeaderView(headerBtn);
    findViewById(R.id.layoutid1).setFocusable(true);

    Button button1 = (Button)findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Button1", 2000).show();
        }
    });
    Button button2 = (Button)findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Button2", 2000).show();
        }
    });

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    for (int i = 0; i < 20; ++i) {
        adapter.add("value" + i);
    }
    listView.setAdapter(adapter);
    listView.setItemsCanFocus(true);
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Toast.makeText(MainActivity.this, "Value" + arg2, 2000).show();
        }
    });
}
}

The layout file:

<?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"
>
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:id="@+id/layoutid1">
    <TextView  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="left"
        android:text="@string/hello"
        /> 
    <Button  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="right"
        android:text="@string/hello"
        android:id="@+id/button1"
        />
    <Button  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="right"
        android:text="@string/hello"
        android:id="@+id/button2"
        />
</LinearLayout>
<ListView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listviewId"/>
</LinearLayout>

I think this is a listview bug and post it on android report bug page. But I am rejected, and the android team ask me to post here for help.. So any help would be appreciated. thanks a lot.


make buttons and listView both clickable you need add for each Button next attribute:

android:focusable="false"

Thats all :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜