我有一个带有容器的活动,其中我显示了一个由listFragment生成的列表。现在我希望通过"转发"来处理列表项的点击次数。 onItemClick方法通过一个pulA接口连接到mainActivity。我做了setOnItemClickLIstener,我也尝试了很多我在这里和其他地方找到的解决方案,例如设置 机器人:可聚焦="假" 机器人:focusableInTouchMode ="假" 在我的自定义列表项的xml文件中。
片段代码看起来像这样(我不会发布整个代码以保持简单,如果需要,我将提供代码的其他部分)
public class DishListFragment extends ListFragment implements ListView.OnItemClickListener {
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private static final String ARG_PARAM3 = "param3";
private static final String ARG_PARAM4 = "param4";
private static final String ARG_PARAM5 = "param5";
//List of Dishes to contain the dishes we get from the server
//Todo: fill with dynamic content
private ArrayList<DishListItem> dishes;
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
private ListView mListView;
private ListAdapter mAdapter;
public static DishListFragment newInstance(Date day, boolean soup, boolean meat,
boolean veggies, boolean sideDishes) {
DishListFragment fragment = new DishListFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, String.valueOf(day));
args.putString(ARG_PARAM2, String.valueOf(soup));
args.putString(ARG_PARAM3, String.valueOf(meat));
args.putString(ARG_PARAM4, String.valueOf(veggies));
args.putString(ARG_PARAM5, String.valueOf(sideDishes));
fragment.setArguments(args);
return fragment;
}
public DishListFragment() { }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//dummy list of dishes addad for developing purposes
// Todo: implement dynamic server generated list content
// create this array to test the comment function
String[] commentDummy = new String[]{"gut", "lecker", "schlecht"};
dishes = new ArrayList<DishListItem>();
dishes.add(new DishListItem("Rigatonelli", commentDummy, 2.0f ,4));
dishes.add(new DishListItem("Spinatcremesuppe"));
dishes.add(new DishListItem("Hackbällchen"));
dishes.add(new DishListItem("Gemüsepfanne"));
dishes.add(new DishListItem("Reis"));
dishes.add(new DishListItem("Rumpsteak"));
dishes.add(new DishListItem("Rumpsteak"));
dishes.add(new DishListItem("Rumpsteak"));
dishes.add(new DishListItem("Rumpsteak"));
dishes.add(new DishListItem("Rumpsteak"));
dishes.add(new DishListItem("Rumpsteak"));
//end here
// set an adapter to the list and let it display the list
// Todo: implement custom list view elements for graphical UI
// Todo: implement custom adapter to handle user preferences (meat/ veggies/ soups/ sidedishes)
mAdapter = new DishListAdapter(getActivity(),dishes);
}
//create on click adapter to handle clicks on list items
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
DishListItem dishItem = this.dishes.get(position);
Toast.makeText(getActivity(), dishItem.getDishName() +" clicked!", Toast.LENGTH_SHORT).show();
mListener.onDishClicked(dishes, position);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dishlist, container, false);
View header = inflater.inflate(R.layout.list_header_day_date, mListView ,false);
mListView = (ListView) view.findViewById(android.R.id.list);
mListView.addHeaderView(header);
mListView.setAdapter(mAdapter);
// Set OnItemClickListener so we can be notified on item clicks
mListView.setOnItemClickListener(this);
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public void setEmptyText(CharSequence emptyText) {
View emptyView = mListView.getEmptyView();
if (emptyText instanceof TextView) {
((TextView) emptyView).setText(emptyText);
}
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onDishClicked(ArrayList<DishListItem> dishes, int position);
}
}
主要活动如下所示
public class CulinariumSpeiseplan extends ActionBarActivity
implements DishListFragment.OnFragmentInteractionListener
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_culinarium_speiseplan);
//add list fragment to the activity layout (put it into the container)
if(savedInstanceState == null){
getFragmentManager().beginTransaction()
.add(R.id.listDishesContainer, new DishListFragment())
.commit();
}
}
{
@Override
public void onDishClicked(ArrayList<DishListItem> dishes, int position) {
Toast.makeText(this, dishes.get(position).getDishName() +" clicked!", Toast.LENGTH_SHORT).show();
SingleDishEnhancedView singleDishEnhancedView = SingleDishEnhancedView.newInstance(dishes, position);
singleDishEnhancedView.show(getFragmentManager(), "DishDisplay");
}
}
吐司仅用于测试目的,但它们也不会显示。我希望我不是完全愚蠢但我似乎无法找到问题的答案。
谢谢你的时间。
编辑:我还应该提一下,我既没有收到编译错误,也没有在任何时候崩溃。该列表是可滚动的,并且行为与您期望的一样,您甚至可以在点击其中一个项目时看到点击动画。
EDIT2:我更新了代码,因为评论指出了一些重要的内容丢失了。我希望更新的代码能够更清晰。
答案 0 :(得分:0)
解决方案相当简单,onItemClick方法必须使用ListView而不是AdapterView,虽然我不确定为什么这不会导致崩溃或至少某种警告。无论哪种方式,问题都解决了。感谢您的评论,他们以正确的方式引导我。