0

我已经实现了ListActivity, 并且想intent为列表中每个可能的项目选择启动一个不同的选项。

例如:

在此处输入图像描述

单击关于项目应该启动intent一个新活动,然后单击访问电话库项目应该启动一个intent打开电话库(例如选择图像)。

我想了解两点:

  1. 如何确定单击了哪个项目,以便我可以启动正确的intent
  2. 如何使列表中的项目在被选中时更改样式?(比如说突出显示以便用户体验更好)

以下是MyActivity(实现ListActivity)的代码:

public class MyActivity extends ListActivity implements OnItemClickListener 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    { 

        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_activity);

        ListView listView = (ListView) findViewById(android.R.id.list);

        listEntryClass listEntries[] = new listEntryClass[]
        {
            new listEntryClass( "About", "About" ),
            new listEntryClass( "Access Phone Gallery", "Access Gallery and Choose an Image" )
        };

        listEntryArrayAdapter adapter = new listEntryArrayAdapter(this, R.layout.list_entry_layout, listEntries);

        View header = (View)getLayoutInflater().inflate(R.layout.list_header_layout, null);
        TextView headerValue = (TextView) header.findViewById(R.id.list_header);
        headerValue.setText( this.getString(R.string.headerPrefs) );

        listView.addHeaderView(header);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener( this );

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {

        Log.d(TAG, "Clicked an entry");
    }

}

我想在某个地方应该有类似 aswitch-caseid点击项目,但不知道该怎么做。

4

2 回答 2

1
 @Override
 public void onItemClick(AdapterView <? > parent, View view, int position, long id)
 {
     if (postion == 0) {
         startActivity(MyActivity.this, FirstActivity.class);
     } else if (possion == 1) {
         startActivity(MyActivity.this, SecondActivity.class);
     } else {
         startActivity(MyActivity.this, ThirdActivity.class);
     }
 }
于 2012-07-02T08:57:23.557 回答
1

如何确定单击了哪个项目,以便启动正确的意图?

   @Override
    public void onItemClick(AdapterView <? > parent, View view, int position, long id) {

       // Here position denotes which row index you clicked.
      //If you clicked first item in ListView then row index will be zero
     // and so on...

       switch (position) {
       case 0:
           //First Item/row clicked
           break;
       case 1:
           //Second Item/row clicked
           break;
       }
   }

如何使列表中的项目在被选中时更改样式?(比如说突出显示以便用户体验更好)

至于设置颜色,你需要一个StateListDrawable. 您可以使用android:listSelector属性在列表中设置它,在 XML 中定义可绘制对象,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_enabled="false" android:state_focused="true"
        android:drawable="@drawable/item_disabled" />
  <item android:state_pressed="true"
        android:drawable="@drawable/item_pressed" />
  <item android:state_focused="true"
        android:drawable="@drawable/item_focused" />
</selector>

ListView xml 如下所示。

<ListView android:id="@+id/list" 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:dividerHeight="0dip"
      android:divider="@null" 
      android:listSelector="@drawable/list_selector"
      android:layout_gravity="center" />
于 2012-07-02T08:58:56.867 回答