我在带有自定义单元适配器的片段中有一个列表,
问题是onItemSelected没有响应,
如何解决这个问题?
public class PhoneMenuList extends SherlockFragment implements OnItemSelectedListener {
Fragment newContent = null;
ListView productList;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View mView = inflater.inflate(R.layout.list, container, false);
return mView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
//SET THE LIST ADAPTER!
Hamburger hambu_data[] = new Hamburger[] {
new Hamburger(R.drawable.icon_hambu_folder, "My Documents"),
new Hamburger(R.drawable.icon_hambu_favs, "Top 10 viewed"),
new Hamburger(R.drawable.icon_hambu_validate, "Validate Document"),
new Hamburger(R.drawable.icon_hambu_how, "How to use"),
new Hamburger(R.drawable.icon_hambu_about, "About")
};
productList= (ListView) getActivity().findViewById(R.id.listView1);
HamburgerAdapter adapter = new HamburgerAdapter(getActivity(), R.layout.hamburger_item_row, hambu_data);
productList= (ListView) getActivity().findViewById(R.id.listView1);
View header = (View)getLayoutInflater(savedInstanceState).inflate(R.layout.hamburger_item_row, null);
productList.addHeaderView(header);
productList.setAdapter(adapter);
//listener
productList.setOnItemSelectedListener(this);
}
//@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.d("mensa", "chapuzea");
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.d("mensa", "abacus");
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Log.d("mensa", "semper");
}
}
答案 0 :(得分:1)
为什么要两次初始化listview?
productList= (ListView) getActivity().findViewById(R.id.listView1);
并使用setOnItemClick()
代替setOnItemSelected()
删除productList.setOnItemSelectedListener(this);
使用productList.setOnItemClickListener(this);
答案 1 :(得分:0)
您必须为ListView设置选择模式。 setChoiceMode
答案 2 :(得分:0)
您可以覆盖SherlockListFragment的onListItemClick方法。
试试此代码。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
System.out.println("onListItemClick");
}
或代替使用OnItemSelectedListener
使用OnItemClickedListener
。