gpt4 book ai didi

android - fragment onListItemClick

转载 作者:太空宇宙 更新时间:2023-11-03 11:59:25 26 4
gpt4 key购买 nike

当我点击项目时,我的 onListItemClick 永远不会被调用,类是扩展 fragment 而不是列表 fragment ,因为我在这个 fragment 中有其他 View 项目不是列表,那么如何在类扩展 fragment 中实现 onlistitemclick?

public class MainFiles extends Fragment 
{
ArrayList<String> items;

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

Button button_up = (Button) view.findViewById(R.id.button_up);
items = new ArrayList<String>();

MyAdapter adapter = new MyAdapter(getActivity(), R.layout.row, items);
ListView myList = (ListView) view.findViewById(R.id.list);
myList.setAdapter(adapter);

return view;
}

public void onListItemClick(ListView l, View v, int position, long id)
{

}
}

最佳答案

OnItemClickListener 显式添加到您的 ListView

myList.setOnItemClickListener(this);

您还必须确保您的 Fragment 实现了 OnItemClickListener 类型:

public class MainFiles extends Fragment implements OnItemClickListener

另一种方法是创建 OnItemClickListener 的专用子类以传递给 ListView:

myList.setOnItemClickListener(new MyOnItemClickListener());

/* ... */

private class MyOnItemClickListener implements OnItemClickListener {

/* ... */

}

关于android - fragment onListItemClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13811023/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com