I have a listview containing Category of items. When I press a category title on the list, it will display another listview containing items inside the chosen Category.
Let your FragmentActivity
implement an interface that has a categorySelected(int categoryId)
method.
Inside CategoryOverviewFragment you call this when a category is selected:
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
((CategorySelectedListener)getActivity()).categorySelected(i);
}
Then in the activity you implement categorySelected
and replace the overview fragment with CategoryFragment
.
When you create you CategoryFragment
set the cateogry ID as argument. It's best to use the newInstance pattern to setArguments()
.
To replace the category overview list fragment with the category details fragment use the FragmentManager to beginTransaction() and then replace().
Assuming the category overview fragment is added dynamically and not in XML use code like this:
CategoryFragment newFragment = CategoryFragment.newInstance(categoryIdSelected);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
If the category list fragment is added in XML you need to remove that change it to a FrameLayout
and add the fragment dynamically in the code.
Communication Between Two Fragment
is Done Via Activity
using Interface
Fragment
A-------------------------->Activity
-------------------->Fragment
B
(defines Interface
) (implements interface
) ( pass to Fragment
B)
Doc reference is Here
Use this ........
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
int new_position = position;
String disp = dataList.get(new_position).get("displaytext");
String msg = dataList.get(new_position).get("message");
String pht = dataList.get(new_position).get("photo");
String mt = dataList.get(new_position).get("messagetime");
String fpht = dataList.get(new_position).get("feedphoto");
String prdctprice = dataList.get(new_position).get(
"productprice");
String pid = dataList.get(new_position).get("productid");
String cmntcount = dataList.get(new_position).get(
"commentcount");
String storeid = dataList.get(new_position).get("storeid");
addfragment(new FeedChat(c, disp, msg, pht, mt, fpht,
prdctprice, pid, storeid, cmntcount), true,
FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
}
});
You should use Interface
, it's the "official" way to pass values to Fragment
.
Take a look at this documentation here :
http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
So what you need to do :
FragmentA --> Activity --> FragmentB
An invisible TextView
is definitely not the correct solution...
EDIT A more detailed solution :
You define an interface (in a separate file or in your FragmentA class) :
public interface MyFragmentListener {
public void onFragmentItemSelected(String url);
}
in your FragmentA class :
MyFragmentListener myListener;
public void setMyFragmentListener(MyFragmentListener listener) {
myListener = listener;
}
// here I used the onClick method but you can call the listener whereever you like.
public void onClick(View view) {
if (myListener != null) {
myListener.onFragmentItemSelected(url);
}
}
Declaration your FragmentB class :
public class FragmentB extends Fragment implements MyFragmentListener {
public void onFragmentItemSelected(String url) {
// do what you want to do
}
}
in your Activity :
// you tell your fragmentA that his listener is the FragmentB. And because you implemented the listener in FragmentB, everything is allright;
fragmentA.setMyFragmentListener(fragmentB));