1

我刚刚开始接触 Android,并构建了一个 UI,其中包含一个带有三个选项卡的 TabHost。每个选项卡都由其自己的 Activity 提供支持。第一个选项卡包含一个列表视图,其中包含一组预填充的行,并且是从自定义 ArrayAdapter 构建的。

我遇到的问题是没有一个 ListView 行是可点击的。换句话说,当我点击它们时,没有橙色选择。如果我在 Nexus One 上使用滚动球,它会选择,但任何触摸手势似乎都没有响应。

所有 UI 都使用 XML 文件处理,其中 main.xml 包含 TabHost -> LinearLayout -> TabWidget/FrameLayout 和包含我的 ListView UI 的 near_activity.xml 文件

<?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">
  <TextView  
    android:id="@+id/android:empty" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:text="@string/nearby_no_events" 
  />  

  <ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_weight="1.0"
    android:choiceMode="multipleChoice"
    android:divider="#d9d9d9"
    android:dividerHeight="1px"    
    android:cacheColorHint="#eee"  
  />  
</LinearLayout>

我的活动中的相关代码设置为显示在所选选项卡中。

public class NearbyActivity extends ListActivity
{
    private ArrayList<Event> m_events = null;
    private EventAdapter m_adapter = null;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nearby_activity);

            getEvents();
        this.m_adapter = new EventAdapter(this, R.layout.eventrow, m_events);
        setListAdapter(this.m_adapter);
    }

    private void getEvents() 
    {
        m_events = new ArrayList<Event>();

        for (int i = 0; i < 100 ; i++)
        {
            Event e = new Event();
            e.setEventName("Event " + i);
            e.setVenueName("Staples Center");
            e.setStartDate(new Date());
            m_events.add(e);
        }
    }

    private class EventAdapter extends ArrayAdapter<Event>
    {
        private ArrayList<Event> items;

    public EventAdapter(Context context, int textViewResourceId, ArrayList<Event> items) 
    {
        super(context, textViewResourceId, items);
        this.items = items;
    }

    @Override
    public View getView (int position, View convertView, ViewGroup parent)
    {
      View v = convertView;
      if (v == null) 
      {
          LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          v = vi.inflate(R.layout.eventrow, null);
      }


      Event e = items.get(position);        
      if (e != null) 
      {
        TextView nameText = (TextView)v.findViewById(R.id.eventName);
        TextView venueNameText = (TextView)v.findViewById(R.id.venueName);
        if (nameText != null) 
        {
            nameText.setText(e.getEventName());                            
        }

        if(venueNameText != null)
        {
            venueNameText.setText(e.getVenueName());
        }
      }

      return v;
    }
    }
}

我的列表视图行也由 XML 文件填充。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="?android:attr/listPreferredItemHeight"
  android:orientation="vertical"
  android:padding="4dip">

  <TextView
    android:id="@+id/eventName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:inputType="text"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:textSize="18sp"
    android:textColor="#000"
   />

  <TextView
    android:id="@+id/venueName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/eventName"    
    android:layout_alignParentBottom="true"
    android:layout_marginRight="55dip"    
    android:singleLine="true"
    android:ellipsize="end"
    android:scrollHorizontally="true"
    android:textSize="13sp"
    android:textColor="#313131"
    android:layout_alignWithParentIfMissing="true"
    android:gravity="center_vertical"
   />      

</RelativeLayout>

谢谢你尽你所能的帮助。

4

1 回答 1

5

您想要一个可点击的 Listview,那么您需要实现 onListItemClick() 方法

    @Override
    protected void onListItemClick(ListView l, View v, final int position, long id) {
    super.onListItemClick(l, v, position, id);                 
Toast.makeText(this,  "This is my row number " + position,Toast.LENGTH_LONG).show();
    }

并确保橙色,您必须设置属性

android:focusable="false"

在您的 Listview Row.xml

于 2010-05-28T22:30:03.440 回答