gpt4 book ai didi

java - 如何使用 SlideMenu 检测 ListView 行单击

转载 作者:行者123 更新时间:2023-12-02 04:34:17 25 4
gpt4 key购买 nike

我正在使用SlideMenu ,我已经实现了 Fragment 和 ListView,我现在需要的是检测用户从幻灯片菜单中单击了哪一行。我怎么可能做到呢?这是listView的代码:

public class listFragment extends ListFragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.menu, null);
}

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SampleAdapter adapter = new SampleAdapter(getActivity());
adapter.add(new SampleItem("menu left A", android.R.drawable.ic_menu_search));
adapter.add(new SampleItem("menu left B", android.R.drawable.ic_menu_search));
adapter.add(new SampleItem("menu left C", android.R.drawable.ic_menu_search));
adapter.add(new SampleItem("menu left D", android.R.drawable.ic_menu_search));
adapter.add(new SampleItem("menu left A", android.R.drawable.ic_menu_search));
setListAdapter(adapter);

}

private class SampleItem {
public String tag;
public int iconRes;
public SampleItem(String tag, int iconRes) {
this.tag = tag;
this.iconRes = iconRes;
}
}

public class SampleAdapter extends ArrayAdapter<SampleItem> {

public SampleAdapter(Context context) {
super(context, 0);
}

public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
}
ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
icon.setImageResource(getItem(position).iconRes);
TextView title = (TextView) convertView.findViewById(R.id.row_title);
title.setText(getItem(position).tag);

return convertView;
}

}
}

最佳答案

据我了解,方法

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.menu, null);

将返回ListView对象。所以你可以调用 setOnItemClickListener() 方法。像这样的事情:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ListView list = (ListView) inflater.inflate(R.layout.menu, null);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// write your click handler here
}
});
return list;

关于java - 如何使用 SlideMenu 检测 ListView 行单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31027823/

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