0

我正在开发一个应用程序,它使用 ActionBar 选项卡通过 ListFragment 显示选项列表。列表(和 ListFragment)显示没有问题,但 ListView 的 setOnItemClickListener 似乎不起作用,因为单击列表中的项目时没有任何反应。这是 ListFragment 类的代码:

package XXX.XXX;

public class AboutFrag extends SherlockListFragment
{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState)
    {
            View view = inflater.inflate(R.layout.aboutfrag, container, false);

            ListView lv = (ListView) view.findViewById(android.R.id.list);

            String[] items = new String[] {"About 1", "About 2", "About 3"};

            lv.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.list_item, items));

            lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                    switch (position)
                    {
                        case 0:
                          Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
                          startActivityForResult(browserIntent, 0);
                          break;

                        case 1:
                          Intent browserIntent2 = new Intent(Intent.ACTION_VIEW, Uri.parse("http://wikipedia.org"));
                            startActivityForResult(browserIntent2, 0);
                            break;

                        case 2:
                          Intent browserIntent3 = new Intent(Intent.ACTION_VIEW, Uri.parse("http:/android.com");
                            startActivityForResult(browserIntent3, 0);
                            break;
                    }           
                }
              });

            return view;
    }      
}

我假设它不起作用,因为该类返回视图对象,所以 FragmentActivity 无法运行侦听器代码,那么有谁知道如何使它工作?顺便说一句,我正在使用 ActionBarSherlock。提前致谢!!!

4

1 回答 1

6

您还可以覆盖从 SherlockListFragment 继承的 onListItemClick 方法。

如下:

@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
      super.onListItemClick(l, v, position, id);

   switch (position)
                    {
                        case 0:
                          Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
                          startActivityForResult(browserIntent, 0);
                          break;

                        case 1:
                          Intent browserIntent2 = new Intent(Intent.ACTION_VIEW, Uri.parse("http://wikipedia.org"));
                            startActivityForResult(browserIntent2, 0);
                            break;

                        case 2:
                          Intent browserIntent3 = new Intent(Intent.ACTION_VIEW, Uri.parse("http:/android.com");
                            startActivityForResult(browserIntent3, 0);
                            break;
                    }

}
于 2012-10-11T05:19:12.417 回答