4

我在活动中的 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>

在模拟器上运行此应用程序时,如果我用鼠标单击没有任何反应。但是,当我使用键盘上的导航按钮选择列表中的任何项目并单击 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

为什么会这样?

4

4 回答 4

5

这是因为在我的 ListView 项目布局中我添加了

android:clickable="true"

对于两个 TextViews。因此,当我单击 ListView 项目时,它确实是单击未实现 onClick 的这些 TextView。从 TextViews 中删除可点击属性解决了我的问题。

感谢大家

于 2012-08-03T10:25:46.703 回答
0

试试这个

http://www.mkyong.com/android/android-listview-example/

res/layout/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>

列表显示

  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();
        }
    });

}

}

于 2012-08-03T09:44:20.730 回答
0

尝试使RelativeLayout可点击和可聚焦。

于 2012-08-03T09:29:44.597 回答
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();
    }
}
于 2012-08-03T09:37:34.323 回答