活动中的我的ListView:
ListView listView1 = (ListView) menu.findViewById(R.id.menuList);
String menuItems[] = new String[] { "My Wants", "Profile", "Notifications",
"Feedback", "Logout" };
listView1.setAdapter(new SideMenuAdapter(this, menuItems, listView1));
listView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 1) {
Intent intent = new Intent(FeedListViewActivity.this,
UserProfileActivity.class);
startActivity(intent);
}
if (position == 0) {
showMyWants();
}
}
});
菜单是:
menu = inflater.inflate(R.layout.horz_scroll_menu, null);
horz_scroll_menu.xml是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu"
android:layout_width="1dp"
android:layout_height="1dp"
android:background="#FFFFFFFF"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FF000000"
android:text="Menu"
android:textColor="#FFFFFFFF"
android:gravity="center" />
<ListView
android:id="@+id/menuList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#FFFFFFFF"
android:scrollbars="none" >
</ListView>
</LinearLayout>
我的SideMenuAdapter:
public class SideMenuAdapter extends BaseAdapter {
private static final int TYPE_MAX_COUNT = 2;
private static LayoutInflater inflater = null;
private Activity activity;
public ImageLoader imageLoader;
public static String[] values;
ListView myList;
public SideMenuAdapter(Activity a, String[] sa, ListView lv) {
values = sa;
activity = a;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myList = lv;
}
public int getCount() {
return values.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView mainText;
public TextView sideText;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
View vi = convertView;
LayoutInflater inflater = activity.getLayoutInflater();
vi = inflater.inflate(R.layout.side_menu_list_item, null);
holder.mainText = (TextView) vi.findViewById(R.id.mainText_sideMenu);
holder.sideText = (TextView) vi.findViewById(R.id.sideText_sideMenu);
vi.setTag(holder);
holder.mainText = (TextView) vi.findViewById(R.id.mainText_sideMenu);
holder.sideText = (TextView) vi.findViewById(R.id.sideText_sideMenu);
holder.mainText.setText(values[position]);
if (position == 2) {
holder.sideText.setText("3");
holder.sideText.setBackgroundResource(R.drawable.orange);
}
return vi;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
}
我的ListView项目的Xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sideMenuListItem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:padding="3dp" >
<TextView
android:id="@+id/sideText_sideMenu"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:clickable="true"
android:gravity="center"
android:textSize="20dp"
android:padding="5dp" />
<TextView
android:id="@+id/mainText_sideMenu"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/sideText_sideMenu"
android:clickable="true"
android:gravity="left|center_vertical"
android:textSize="20dp"
android:padding="5dp" />
</RelativeLayout>
在模拟器上运行此应用程序时,如果我单击鼠标没有任何反应。但是当我使用KeyBoard上的导航按钮选择列表中的任何项目并单击Enter时,它可以正常工作。
在设备上运行应用时。如果我继续点击列表中的任何项目,有时会有10到20次。
编辑:
Actually everything worked fine when i was using predefined ArrayAdapter<String> and android.R.simple_list_item. But i want a custom adapter
为什么会这样?
答案 0 :(得分:5)
因为在我的ListView项目布局中我添加了
android:clickable="true"
两个TextViews的。因此,当我单击ListView项时,它确实是单击这些TextView,其onClick未实现。从TextViews中删除可点击属性解决了我的问题。
谢谢大家
答案 1 :(得分:0)
尝试使RelativeLayout
可点击并专注。
答案 2 :(得分:0)
布局文件
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000fff"
android:layout_weight="2"
android:drawSelectorOnTop="false">
</ListView>
主要活动
public class MyListView extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, PENS));
getListView().setTextFilterEnabled(true);
}
static final String[] PENS = new String[]{
"MONT Blanc",
"Gucci",
"Parker",
"Sailor",
"Porsche Design",
"Rotring",
"Sheaffer",
"Waterman"
};
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String pen = o.toString();
Toast.makeText(this, "You have chosen the pen: " + " " + pen, Toast.LENGTH_LONG).show();
}
}
答案 3 :(得分:0)
尝试这个
http://www.mkyong.com/android/android-listview-example/
RES /布局/ list_fruit.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="20sp" >
</TextView>
的ListView
public class ListFruitActivity extends ListActivity {
static final String[] FRUITS = new String[] { "Apple", "Avocado", "Banana",
"Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
"Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// no more this
// setContentView(R.layout.list_fruit);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_fruit,FRUITS));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}