how to change fragment views programmatically from the parent activity

后端 未结 2 1774
半阙折子戏
半阙折子戏 2021-01-26 00:43

I am having a fragment that contains for example one button, I want to allow the Activity containing the fragment to change this button view like for example color and title or

相关标签:
2条回答
  • 2021-01-26 00:49

    Try this way according to your requirement

    public abstract class BaseFragment extends Fragment {
        BaseActivity yourActivity;
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
       @Override
       public void onAttach(Context context) {
           super.onAttach(context);
          yourActivity = (BaseActivity) context;
       }
    
       @Override
       public void onDetach() {
           yourActivity = null;
           super.onDetach();
        }
    
    
        public void refreshview(){} 
    
        public void setDada(Data data){}
    
    
        @override
        publicn void onStart(){
            yourActivity.setCurrentOpenedFragment(this);
        }
    }
    

    Create the BaseActivity class

    public abstract class BaseActivity extends AppCompatActivity{
       @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
         public void setCurrentOpenedFragment(BaseFragment currentFragment){}
    }
    

    Extends the BaseFragment in your fragment i.e.

    public class YourFragment extends BaseFragment{
       View view;       
       Button button;
       @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {       
              view = inflater.inflate(R.layout.fragment, container, false);
              button = (Button)view.findViewById(R.id.button);
              return view;
        }
    
        @override
        publicn void onStart(){
          super.onStart();
          //yourActivity.setCurrentOpenedFragment(this);
       }
    
       Data data; 
       @override 
       public void setDada(Data data){
         this.data = data;
       } 
        @Override 
        public void refreshview() {
           button.setcolor(data.getcolor());
        }
    } 
    

    Write the below line of code in your Activity class

    public class YourActivity extends BaseActivity{
    
       public static final byte NONE =  -1;
       public static final byte HOME =  1;
    
       @Override
        protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
                setContentView(R.layout.your_activity);
                changeFragment(new YourFragment(), HOME);
                initview();
        }
    
       public void initview(){
            Button b = (Button)findViewById(R.id.button);
            b.setOnClickListener(new OnClickListener(){
              if(currentFragmentOnDrawer != null){
    
                Data data = new Data();
                data.setColor(getResource().getcolor(R.color.red));
                currentFragmentOnDrawer.setDada(data);
    
                currentFragmentOnDrawer.refreshview()
    
             }
           });
       }
    
       BaseFragment currentFragmentOnDrawer;
        @Override
        public void setCurrentOpenedFragment(BaseFragment currentFragment) {
            super.setCurrentOpenedFragment(currentFragment);
            this.currentFragmentOnDrawer = currentFragment;
        }
    
       public void changeFragment(Fragment targetFragment, byte state) {
    
          FragmentTransaction ft = getSupportFragmentManager()
                .beginTransaction();
          ft.replace(R.id.main_fragment, targetFragment, "" + state);
          ft.addToBackStack("" + state);
          ft.commit();
      }
    }
    

    Hope it will help for you

    0 讨论(0)
  • 2021-01-26 01:03

    1. You must get you fragment from your activity

    val fragment = supportFragmentManager.findFragmentByTag("tag")
    

    or

    val fragment = supportFragmentManager.findFragmentById(R.id.fragment)
    

    2. Create a method that will do what you wants in you fragment and call this method from the activity

    fragment.hideButton()
    

    or you can use interface

    0 讨论(0)
提交回复
热议问题