15

我正在使用选项卡式布局(带滑动)。在这里,我有 3 个由 SectionsPagerAdapter 控制的选项卡。每个选项卡都是一个 ListFragment。

现在我想在单击列表中的一项时触发一个事件。我想为每个选项卡设置一个监听器。

这是现在的代码(不工作,没有触发事件)。

public class NyhederFragment extends ListFragment {
     public static final String ARG_SECTION_NUMBER = "section_number";
     private static final String TAG="NyhederFragment";
     private List<Item> newsItems;
     private ArrayList newsHeadlines;
     private ArrayAdapter adapter;
     private BroadcastReceiver updateReciever;

public NyhederFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    ListView newsList = new ListView(getActivity());
    newsList.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    newsList.setId(R.id.list);
    DatabaseHelper dbConn = new DatabaseHelper(getActivity());

    newsItems = dbConn.getAllItemsFromNews();
    newsHeadlines = new ArrayList();
    for(Item i : newsItems){
        newsHeadlines.add(i.getTitle());
    }

    adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, newsHeadlines);

    setListAdapter(adapter);

    newsList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {
            Log.i("debug", "single click");
        }
    });

    dbConn.close();

    getActivity().registerReceiver(updateReciever, new IntentFilter("ArticlesUpdated"));
    return newsList;
}
}

怎么回事,我做错了吗?

提前非常感谢!

4

4 回答 4

35

如果您正在使用,ListFragment那么您可以简单地使用它的覆盖方法onListItemClick()

@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
    }
于 2012-12-29T09:34:28.730 回答
15

这是由于自定义列表项。默认焦点是自定义列表项(按钮/文本视图)。它导致了这个问题。

请添加android:descendantFocusability="blocksDescendants"列表元素的根布局。希望这会对某人有所帮助。

于 2013-03-27T06:53:52.407 回答
0

更改setListAdapter(adapter);newsList.setAdapter(adapter);

于 2012-12-29T09:25:08.190 回答
0

If you wanted to create or re-use or an existing handler that implements AdapterView.OnItemClickListener, rather than implementing onListItemClick() within the ListFragment, you can do so by getting a reference to the ListView of the ListFragment and setting its listener. In your ListFragment.onResume() you could do:

@Override
public void onResume() {
    super.onResume();
    ListView listView = getListView();
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d("TAG", "Stop touching me");
        }
    });
}

In my case, I can use the same listener from an Activity with a ListView, a ListActivity or a ListFragment, e.g.

listView.setOnItemClickListener(new MyListViewOnClickListener(getActivity()));

于 2015-01-24T12:52:13.463 回答