android dev:列表视图中的项目无法正确获得焦点
我面临列表视图焦点问题。 列表视图上方有一个按钮(不是列表视图标题)。我将 listview.setItemsCanFocus 设置为 true。当我使用轨迹球滚动时,列表视图中的项目无法正确聚焦。我发布了布局文件和代码,希望这有帮助。
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 created. */
@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();
}
});
}
}
布局文件:
<?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>
我认为这是一个列表视图错误并将其发布在android报告错误页面上。但我被拒绝了,Android 团队要求我在这里发帖寻求帮助。所以任何帮助将不胜感激。 多谢。
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 created. */
@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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使按钮和 listView 都可点击,您需要为每个按钮添加下一个属性:
仅此而已:)
make buttons and listView both clickable you need add for each Button next attribute:
Thats all :)