How to call a fragment from BaseAdapter Class Android?

前端 未结 7 644
走了就别回头了
走了就别回头了 2020-12-17 03:38

I want to call a Fragement from my BaseAdapter Class. In this class I have button on click of which I want to call the new fragment, but I am not able to get this. I have to

相关标签:
7条回答
  • 2020-12-17 03:46

    Simply use this.

    public void onClick(View view) {
     LogFrag fragment2 = new LogFrag();
     FragmentManager fragmentManager = getFragmentManager();
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
     fragmentTransaction.replace(R.id.fragment1, fragment2);
     fragmentTransaction.commit();
    }
    
    0 讨论(0)
  • 2020-12-17 03:49

    Easiest way: Do in Fragment

    Adapter a = new Adapter(arg,Fragment.this);// or only this. This will pass the fragment object to the adapter.
    

    and in Adapter

    v.ckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
    
                if(fragment!=null)
                    fragment.ValidateList();// call any public method of the fragment
                }
            });
    
    0 讨论(0)
  • 2020-12-17 03:50
    FragmentManager manager = ((AppCompatActivity) 
    context).getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = manager.beginTransaction();
                fragmentTransaction.replace(R.id.content_frame, new AlbumFragment());
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
    
    0 讨论(0)
  • 2020-12-17 03:52

    Perfect way to call fragment from Custom Base Adapter

    fragmentManager.beginTransaction()
            .add(R.id.content_frame, new YourFragmentName())
            .addToBackStack("fragBack")).commit();
    ((Activity) context).setTitle("Title For Action Bar");`
    
    0 讨论(0)
  • 2020-12-17 03:56

    Use

    ((Activity) mContext).getFragmentManager();//use this
    
    view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment = new CallThisFragment();
                FragmentManager fragmentManager = ((Activity) mContext).getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.main_activity, fragment).commit();
            }
        });    
    
    0 讨论(0)
  • 2020-12-17 03:59

    Honestly if you have to call your fragment from a BaseAdapter something is terribly wrong with your application's architecture, you are tightly coupling components and Spaghetti Code will be a problem soon, if you want to keep it clean, make a listener or send a broadcast from it, and call your fragment from your activity as you usually do, the point is to keep components doing the job they are intended for and not having all in one single class, that's a terrible thing to do and code becomes less legible.

    As explained a simple approach to decouple things in android is by sending broadcast messages, so this is one way of doing it:

     view_log.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
    
                //Here i want to call my fragment
                //You need to pass a reference of the context to your adapter...
                context.sendBroadcast(new Intent("start.fragment.action"))
    
            }
        });
    

    Now in your activity all you have to do is register a BroadcastReceiver with the "start.fragment.action" and that's it inside of it just call your fragment:

    //In your activity...
    context.registerReceiver(mBroadcastReceiver, new IntentFilter("start.fragment.action"))
    .
    .
    .   
    BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    
            @Override
            public void onReceive(Context context, Intent intent) {
                        //This piece of code will be executed when you click on your item
                // Call your fragment...
            }
        };
    

    Do not forget to unregister when done, and if you need to pass some parameters to the fragment you can use extras in the intent when sending the broadcast message...

    NOTE: LocalBroadcastManager would be better to use now.

    Regards!

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