how to hide Fragment element from another fragment in Android - issue android
I wanted to hide component of a Fragment from another Fragment. I am using ViewPager for Fragments. previously I transfer data from one Fragment to another Fragment, that time I before FragmentTransaction replace I added Bundle to ListDetailsFragment. so that was pretty easy stuff. i use FrameLayout Fragments. And it this case ViewPager is in place.
I was able to pass data to Activity with interface, but unable to update date as fragment is already created. looking Fragment life cycle I was hopeful that onStart() will help me to update data when that particular Fragment is displayed. But I was wrong due to ViewPager in place. than I have my answer in https://stackoverflow.com/questions/18375288/fragment-lifecycle-which-method-is-called-upon-show-hide.
Here is the solution code I am come out with.
[Objective selecting Hide / Show radio button in FragmentOne, I will Hide / Show text in FragmentTwo]
..........................................................................................................................
..........................................MainActivity......................................................
..........................................................................................................................
...........................................EmptyPagerAdapter.............................................
..........................................................................................................................
............................................FragmentOne.......................................................
..........................................................................................................................
............................................FragmentTwo.......................................................
I was able to pass data to Activity with interface, but unable to update date as fragment is already created. looking Fragment life cycle I was hopeful that onStart() will help me to update data when that particular Fragment is displayed. But I was wrong due to ViewPager in place. than I have my answer in https://stackoverflow.com/questions/18375288/fragment-lifecycle-which-method-is-called-upon-show-hide.
Here is the solution code I am come out with.
[Objective selecting Hide / Show radio button in FragmentOne, I will Hide / Show text in FragmentTwo]
..........................................................................................................................
..........................................MainActivity......................................................
package android.exapmple.com.myapptestfragmentm2; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import static android.webkit.ConsoleMessage.MessageLevel.LOG; public class MainActivity extends AppCompatActivity implements FragmentOne.MyPakageListener{ public ViewPager pager; public static boolean noGuardianStatus; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pager = (ViewPager) findViewById(R.id.pager); assert pager != null; pager.setAdapter(new EmptyPagerAdapter(getSupportFragmentManager(), pager)); } @Override public void getGuardianStatus(boolean noGuardianStatus) { Log.e("noGuardianStatus", "--Main--" + noGuardianStatus); this.noGuardianStatus = noGuardianStatus; } }
..........................................................................................................................
...........................................EmptyPagerAdapter.............................................
package android.exapmple.com.myapptestfragmentm2; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; /** * Created by jAVA USER on 8/3/2017. */ class EmptyPagerAdapter extends FragmentPagerAdapter { private ViewPager pager; private static int pageCount=2; public EmptyPagerAdapter(FragmentManager fm) { super(fm); } public EmptyPagerAdapter(FragmentManager fm, ViewPager pager) { super(fm); this.pager=pager; } @Override public Fragment getItem(int position) { switch(position) { case 0: return FragmentOne.newInstance(position + 1, position == getCount() - 1, pager); case 1: return FragmentTwo.newInstance(position + 1, position == getCount() - 1, pager); default: return FragmentOne.newInstance(position + 1, position == getCount() - 1, pager); } } @Override public int getCount() { return pageCount; } public void setCount(int pageCount){ this.pageCount =pageCount; } }
..........................................................................................................................
............................................FragmentOne.......................................................
package android.exapmple.com.myapptestfragmentm2; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RadioGroup; /** * A simple {@link Fragment} subclass. */ public class FragmentOne extends Fragment { RadioButton radioShow, radioHide; Button btn; public static ViewPager pager; private MyPakageListener myPakageListener; // public FragmentOne() {// // Required empty public constructor// } public static FragmentOne newInstance(int page, boolean isLast, ViewPager pager) { FragmentOne.pager =pager; Bundle args = new Bundle(); args.putInt("page", page); if (isLast) args.putBoolean("isLast", true); final FragmentOne fragment = new FragmentOne(); fragment.setArguments(args); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_fragment_one, container, false); radioShow = (RadioButton) view.findViewById(R.id.radioShow); radioHide = (RadioButton) view.findViewById(R.id.radioHide); btn = (Button) view.findViewById(R.id.btn); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); myPakageListener = (MyPakageListener) getContext(); radioShow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myPakageListener.getGuardianStatus(false); } }); radioHide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myPakageListener.getGuardianStatus(true); } }); } public interface MyPakageListener{ void getGuardianStatus(boolean noGuardianStatus); } }
..........................................................................................................................
............................................FragmentTwo.......................................................
package android.exapmple.com.myapptestfragmentm2; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * A simple {@link Fragment} subclass. */public class FragmentTwo extends Fragment { public static ViewPager pager; private boolean noGuardianStatus; TextView textintwo; // public FragmentTwo() {// // Required empty public constructor// } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_fragment_two, container, false); textintwo = (TextView) view.findViewById(R.id.textintwo); return view; } public static FragmentTwo newInstance(int page, boolean isLast, ViewPager pager) { FragmentTwo.pager =pager; Bundle args = new Bundle(); args.putInt("page", page); if (isLast) args.putBoolean("isLast", true); final FragmentTwo fragment = new FragmentTwo(); fragment.setArguments(args); return fragment; } // @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); this.noGuardianStatus = MainActivity.noGuardianStatus; if(textintwo != null){ if(noGuardianStatus) textintwo.setVisibility(View.GONE); else textintwo.setVisibility(View.VISIBLE); } } }
Comments
Post a Comment