I have an app with functionality to show one record or a list of them. I have a fragment for single record, so I decided for a list I want to fill a RecyclerView with cards, and add a fragment into each card. I am getting IllegalArgumentException: Cannot add a null child to a ViewGroup on a line in onBindViewHolder():
holder.frameLayout.addView(ResultFragment.newInstance(id).getView())
I'm not so sure how to work with a Recycled View and Fragments but if you use a linearLayout or RelativeLayout, you can use FragmentManager in the onCreateView method or generate you're pwn method to change the content in the view (fragment) that you want to change:
FragmentManager fm = getSupportFragmentManager();
transaction = fm.beginTransaction();
oneFragment = new OneFragment();
transaction.replace(R.id.fragmentContent, oneFragment);
transaction.commit();
the R.id.fragmentManager are a <fragment> section in your view
Regards
Related
I have a Recycler View in a Fragment that I have an adapter class for in a separate file. I am trying to make it so that when you click on a RecyclerView Item it replaces the current fragment with a new one. The code to replace a fragment with another that I had been using previously is this:
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.nav_host_fragment, NewFragment.class, null);
transaction.addToBackStack("CurrentFragment");
transaction.commit();
And this works inside the fragment, but since I have to call it from the java class I tried this instead:
FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.nav_host_fragment, NewFragment.class, null);
transaction.addToBackStack("CurrentFragment");
transaction.commit();
And this just layers the new fragment on top of the current one. If I try making a class in the Current Fragment with the transaction code in it and call it, it doesn't work because of the static context.
Is there any way to do this?
I have a view pager that returns same fragment with different data from adapter every time i swap but despite of the fact that different data is received it displaying the same data over and over again, how can i make fragment update its data
the structure is, there is a fragment that has a listview and when i click on the listview item it replaces a fragment that has viewpager and setting its adapter, the adapter is FragmentPagerAdapter and returns a fragment with argument now fragment uses its argument and display data, every time getview called of adapter returning the same fragment with different data
here is the code:
when list item clicked:
replaceFragment(ReportPaginationFragment.newInstance(i, isFromList), true, true);
ReportPaginationFragment:
viewPager.setAdapter(new ReportPaginationAdapter(getFragmentManager(), reportID, appointmentObjectArrayList, isFromList, listPosition ));
Adapter:
reportId = appointmentObjectArrayList.get(item++).getId();
return NewDrReportFragment.newInstance(reportId);
appointmentObjectArrayList is arraylist that has data
Thanks in advance
Instead of addFragment, replace is better choice.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
for (int i=0;i<categoryList.size();i++){
Category category=categoryList.get(i);
FoodFragment foodFragment = new FoodFragment();
foodFragment.setCategory(category);
pagerAdapter.addFragment(foodFragment, category.getName());
}
lets take this example. Maybe you are not setting differrent data onto pageAdapter properly.
can you share your code for more information?
I have a fragment and I need to completely refresh/reinstantiate it.
By refreshing I mean recreating fragment.
I have tried using FragmentManager with detach() method but it didn't help. All EditText children of that fragment still have their values entered even though the fragment was refreshed
Is there any way to achieve this result?
if there are only edit texts , create a method that sets all edit texts to empty string that is et(edit_text object),
et.setText(""); //edit text will be reset
Put the following code inside the fragment which need to be refreshed.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
Use fragment manger according to your code (if it is an old version then this will be supportFragmentManager)
Put and call this method to your fragment
private void reloadFragment(){
Fragment frg = getActivity().getSupportFragmentManager().findFragmentByTag(ReceiptFragment.TAG);
final FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
}
I need to show several fragments on the screen. All fragments are instances of one fragment class, but I need to be able to set my values to view attrbutes on these fragments (text in TextView for example). I tried many solutions from here, but didn't find one.
What I'm doing now:
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction = manager.beginTransaction();
List<Comment> comments = place.getComments();
int i = 0;
for (Comment comment : comments) { //some cycle...
ReviewFragment reviewFragment = new ReviewFragment();
transaction.add(scrollView.getId(), reviewFragment, "review" + i);
((TextView) reviewFragment.getView().findViewById(R.id.author)).setText(comment.getAuthor());
i++;
}
transaction.commit();
But I get NullPointerException: reviewFragment.getView() is null. I tried to commit the transaction and begin new after each fragment, but it didn't helped. How can I set custom values in fragment views?
P.S. I didn't do something special in overriden methods in my ReviewFragment. Should I?
Thanks!
You can call the fragment's method setArguments() when you construct your fragment and pass the text you would like to display. Then inside the ReviewFragment class you can call getArguments() to retrieve the text and display it.
in the part that you are creating your fragment:
ReviewFragment reviewFragment = new ReviewFragment();
Bundle args = new Bundle();
args.putString("text", "The text to display here");
reviewFragment.setArguments(args);
transaction.add(scrollView.getId(), reviewFragment, "review" + i);
and in the onCreateView() of your ReviewFragment
// after inflating the view and before returning it
String textToDisplay = getArguments().getString("text");
myTextView.setText(textToDisplay);
I want to be able to setText and getText of Views of individual Fragments. As it is now, when I setText of a Framgent's TextView it changes the text of that View in all Fragments.
I've been experimenting by moving things around, but here is my code as of this moment:
Fragment class
public class TestFragment extends Fragment{
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.test_fragment, container, false);
TextView tv = (TextView) view.findViewById(R.id.huh);
//tv.setText("AAAAAAAAAAAAAAAAAAA");
return view;
}
public void setText(String asdf) {
TextView test = (TextView) view.findViewById(R.id.huh);
test.setText(asdf);
}
}
Activity Class
public class Manage extends BaseActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.manage);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
TestFragment fragment = new TestFragment();
//fragment.setText("ASDF");
fragmentTransaction.add(R.id.test_fragment, fragment, "testtag");
fragmentTransaction.commit();
}
}
The framgent.xml is pretty plain; just a single TextView.
Fragments are added to stack with a parameter named tag. In your case you've added your fragment with "testtag".
fragmentTransaction.add(R.id.test_fragment, fragment, "testtag");
If you create multiple instances of same fragment and add them with unique tags, then you are able to get them with that unique tags. When you get a fragment then you can reach its content.
FragmentManager fm = this.getSupportFragmentManager();
Fragment testtagFragment = fm.findFragmentByTag("testtag");
View targetView = testtagFragment.getView().findViewById(R.id.anyViewInsideContentOfYourFragment);
Edit:
I want to be able to setText and getText of Views of individual
Fragments.
This question has 2 parts.
To setText while initializing you have to pass your initial
parameters to your fragment while creating its instance. I suggest
you to use a static newInstance method for this. See sample
here
To getText read my answer above. Note that, you can get the content of a fragment after its onCreateView method is executed. So If you try to call getView method of a fragment at your activities onCreate method (after you add the fragment), that will return null. You can get its content successfully under a click event to test that, and use get or set operations of any view on that fragment's content.