我创建了一个管理 4 个选项卡的 Tab FragmentActivity 类。
选项卡一包含一个包装片段作为父级:
1)A 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;
}
} ......