Reputation: 431
I want to refresh the fragment when the view pager change to it.
package com.mcivisoft.rcbeam;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import com.mcivisoft.rcbeam.R;
public class FragBeamRec extends Fragment {
public static FragBeamRec newInstance() {
FragBeamRec fragment = new FragBeamRec();
return fragment;
}
public FragBeamRec() {}
private EditText tasss = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_frag_beam_rec, container, false);
tasss = (EditText)v.findViewById(R.id.txttttaasss);
tasss.setText(String.valueOf(var.asspass));
ActivityBeamRec.mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
if (position == 2) {
Fragment frg = null;
frg = getFragmentManager().findFragmentByTag("FragBeamRec");
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
@Override
public void onPageScrollStateChanged(int state) {}
});
return v;
}
}
This isn't working, can anyone tell me why. I have been trying to refresh this fragment for three days now please help.
The reason I want it to refresh that I got other fragment in the same activity (tabbed activity), in the first fragment there is a button that do some calculation and put the result in a global variable, and this fragment show the value of the public variable, and because it's created before the button is clicked it show 0.0
that's why I want it to refresh so it show the current value of the global variable.
Upvotes: 0
Views: 732
Reputation: 393
I think your entire implementation is shaky
You have FragBeamRec inside a ViewPager which is inside ActivityBeamRec.
Going by this, If you have 4 fragments inside your ViewPager, chances are that FragBeamRec has never even been created when changes occur to the said data model.
The best implementation is fire all refresh from the ActivityBeamRec. Register the viewPagerChangeListener inside of the activity. You might also tie a Listener interface that is fired on the ActivityBeamRec activity when the model changes.
You can now get the Fragment from the Activity using
private Fragment getFragmentFromPager(int index) {
return getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.parentViewPager + ":" + index);
}
By passing in the Fragment index position inside the viewPager, you would get a Fragment object with which you can carryout any form of refresh
Upvotes: 0
Reputation: 431
I did it by replacing private EditText tasss = null;
by public static EditText tasss = null;
this way I can access it from anywhere.and in the button on click listener FragBeamRec.tasss.setText(String.valueOf(var.asspass));
Upvotes: 0
Reputation: 1019
Instead of refreshing the fragment, you should simply refresh the view. See the guide for communicating between fragments (http://developer.android.com/training/basics/fragments/communicating.html) for a tutorial. The same concepts there apply here.
Basically you will want to notify the FragBeamRec
fragment that the calculation is complete inside the button's OnClickListener
like this
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int result = 4 + 4;
fragBeamRec.onCalculationComplete(result);
}
});
with an interface
public interface OnCalcComplete {
void onCalculationComplete(int result);
}
Then in the fragment have something like
public class FragBeamRec extends Fragment implements OnCalcComplete {
public void onCalculationComplete(int result) {
textView.setText(result);
}
...
}
Upvotes: 1