我创建了一个管理4个标签的Tab FragmentActivity类。
选项卡1包含一个包装器片段,用作以下的父级:
1)ListFragment (显示类别) - 单击列表中的项目时,通过调用list.setOnItemClickListener(new OnItemClickListener),在同一选项卡中显示项目的子列表(下面的#2) ()在包装器片段中并使用:
getChildFragmentManager()
.beginTransaction()
.replace(R.id.container, details)
.addToBackStack(null)
.commit();
点击第一个列表并显示第二个列表是没有问题的。单击第二个列表以显示详细信息...这就是我遇到问题的地方。
2)列表片段(显示子列表) - 我试图在包装器中实现第二个setOnItemClickListener来监听此子列表上的点击。我无法弄清楚如何实现这一点。
3)片段(显示上面2中所选项目的详细信息)
public class CategoryListWrapperFragment extends Fragment {
public static final String EXTRA_ITEM_ID_STRING = "com.f.lite.item_selected_from_list_id";
private String mPassId;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DealListFragment deallist = new DealListFragment();
deallist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Log.d("Wrapper", "Inside deallist on click listner");
Bundle args = new Bundle();
args.putString(EXTRA_ITEM_ID_STRING, mPassId);
// Create details fragment based on clicked item's position
DealFragment details = new DealFragment();
details.setArguments(args);
getChildFragmentManager()
.beginTransaction()
.replace(R.id.container, details)
.addToBackStack(null)
.commit();
}
});
CategoryListFragment list = new CategoryListFragment();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Bundle args = new Bundle();
args.putString(EXTRA_ITEM_ID_STRING, mPassId);
DListFragment sublist = new DListFragment();
sublist.setArguments(args);
getChildFragmentManager()
.beginTransaction()
.replace(R.id.container, sublist)
.addToBackStack(null)
.commit();
}
});
getChildFragmentManager()
.beginTransaction()
.add(R.id.container, list)
.commit();
}
public void setPassId(String passIn) {
mPassId = passIn;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// There has to be a view with id `container` inside `wrapper.xml`
return inflater.inflate(R.layout.wrapper, container, false);
}
}
enter code here
public class CategoryListFragment extends ListFragment {
private OnItemClickListener listener;
private ArrayList<Category> mCategory;
ImageView mCategoryImageView;
@Override
public void onAttach (Activity activity) {
super.onAttach(activity);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle(R.string.category_title);
mCategory = CategoryLab.get(getActivity()).getCategory();
CategoryAdapter adapter = new CategoryAdapter(mCategory);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Category c = ((CategoryAdapter)getListAdapter()).getItem(position);
CategoryListWrapperFragment parent = (CategoryListWrapperFragment)getParentFragment();
parent.setPassId(c.getmCategory());
if(listener != null) {
listener.onItemClick(l, v, position, id);
}
}
public void setOnItemClickListener(OnItemClickListener l) {
this.listener = l;
}
...
公共类DListFragment扩展了ListFragment {
private ArrayList<Deal> mDeals;
private OnItemClickListener listener;
private String mCategoryID;
@Override
public void onAttach (Activity activity) {
super.onAttach(activity);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCategoryID = (String)getArguments().getString(CategoryListWrapperFragment.EXTRA_ITEM_ID_STRING);
mDeals = DealLab.get(getActivity()).getDeals(mCategoryID);
getActivity().setTitle(mCategoryID);
DealAdapter adapter = new DealAdapter(mDeals);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Deal d = ((DealAdapter)getListAdapter()).getItem(position);
CategoryListWrapperFragment parent = (CategoryListWrapperFragment)getParentFragment();
parent.setPassId(Long.toString(d.getmId()));
if(listener != null) {
listener.onItemClick(l, v, position, id);
}
}
@Override
public void onResume() {
super.onResume();
mDeals = DealLab.get(getActivity()).getDeals(mCategoryID);
DealAdapter adapter = new DealAdapter(mDeals);
setListAdapter(adapter);
}
public void setOnItemClickListener(OnItemClickListener l) {
this.listener = l;
}
} ......
答案 0 :(得分:0)
我确信这不是最佳解决方案,但它确实有效。我在包装器中创建了一个函数来设置第二个onListItemClick侦听器,然后从子片段onCreate调用该函数。它工作正常,但可能有一个更简洁的方法来做到这一点。