I am sure this is not the best solution but it works. I created a function in the wrapper to set the second onListItemClick listener and then call that function from the child fragment onCreate. It works fine but there is likely a cleaner way to do this.
Parent Wrapper Fragment handling two OnItemClick events
質問
I have created a Tab FragmentActivity class that manages 4 tabs.
Tab one contains a wrapper Fragment to act as a parent to:
1) A ListFragment (Displaying categories) - When an item in the list is clicked the sublist of item is displayed (# 2 below) in the same tab by calling list.setOnItemClickListener(new OnItemClickListener() in the wrapper fragment and using:
getChildFragmentManager()
.beginTransaction()
.replace(R.id.container, details)
.addToBackStack(null)
.commit();
Clicking on the first list and having the second displayed is no problem works great. Clicking on the second list to have the detail displayed... that is where I am having issues.
2) A List Fragment (Displaying the sublist) -- I am trying to implement the second setOnItemClickListener in the wrapper to listen for the click on this sub list. I cant figure out how to implement this.
3) A Fragment (Displaying the detail of the item selected in 2 above)
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;
}
....
public class DListFragment extends 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;
}
} ......
解決