Additional questions about extensions in Fragment

Asked 2 years ago, Updated 2 years ago, 79 views

public abstract class AbstractDataInterface : DataInterface 
{
    public static int SEND_TO_LAYER1 = 0, SEND_TO_LAYER2 = 1, SEND_TO_LAYER3 = 2, SEND_TO_LAYER4 = 3, SEND_TO_DATA = 4, SEND_TO_PROC = 5;
    public static int
            IDX_LAYER_DISP_BASE = 0,    // IDX_LAYER1 = 0
            IDX_LAYER_DISP_MASK = 1,    // IDX_LAYER2 = 1
            IDX_LAYER_DISP_CHAR = 2,    // IDX_LAYER3 = 3
            IDX_LAYER_DISP_MENU = 3,    // IDX_LAYER4 = 4
            IDX_PROCS_DISP_DATA = 4,
            IDX_PROCS_DISP_PROC = 5;
    public static int MAX_HANDLER = 6;
    public static Handler[] _Handler = new Handler[MAX_HANDLER];

    public abstract void SetHandler(int dest, Handler handler);
    public abstract Handler GetHandler(int dest);
    public abstract int SendMessage(Handler handler, int dest, int cmd, Object o);

    public abstract void onDestroy();

}
interface DataInterface
{
}
public class Main_Menu_Fragment : Fragment, Activitys.AbstractDataInterface
{
    Button _btnGeneral, _btnAIS, _btnSAT, _btnNotices, _btnRAP, _btnFiles, _btnMap, _btnUnits, _btnFF, _btnFace, _btnRD, _btnIS;
    FragmentTransaction _Main_Fragment;
    Main_Title_Fragment _Text_Change;

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View Main_Menu = inflater.Inflate(Resource.Layout.Main_Menu_Layout, container, false);

        return Main_Menu;
    }

It's a three-class file in this way The ABS that I mentioned in the previous question is the interface at the top. How do I refer to ABS interface in a fragment in this state?<

fragment extends

2022-09-22 10:57

2 Answers

I'm not sure what the purpose of the DataInterface was, but I can write it as follows.

DataManipulableFragment

abstract class DataManipulableFragment extends Fragment {
    public static int SEND_TO_LAYER1 = 0, SEND_TO_LAYER2 = 1, SEND_TO_LAYER3 = 2, SEND_TO_LAYER4 = 3, SEND_TO_DATA = 4, SEND_TO_PROC = 5;
    public static int
            IDX_LAYER_DISP_BASE = 0,    // IDX_LAYER1 = 0
            IDX_LAYER_DISP_MASK = 1,    // IDX_LAYER2 = 1
            IDX_LAYER_DISP_CHAR = 2,    // IDX_LAYER3 = 3
            IDX_LAYER_DISP_MENU = 3,    // IDX_LAYER4 = 4
            IDX_PROCS_DISP_DATA = 4,
            IDX_PROCS_DISP_PROC = 5;
    public static int MAX_HANDLER = 6;
    public static Handler[] _Handler = new Handler[MAX_HANDLER];

    public abstract void setHandler(int dest, Handler handler);

    public abstract Handler getHandler(int dest);

    public abstract int sendMessage(Handler handler, int dest, int cmd, Object o);

    public abstract void destroy();
}

MainMenuFragment

public class MainMenuFragment extends DataManipulableFragment {
    @Override
    public void setHandler(int dest, Handler handler) {

    }

    @Override
    public Handler getHandler(int dest) {
        return null;
    }

    @Override
    public int sendMessage(Handler handler, int dest, int cmd, Object o) {
        return 0;
    }

    @Override
    public void destroy() {

    }
}

But seeing that you're going to use Handler [] statically, I think you're trying to write a class that acts as a relay, regardless of whether it's a good or bad way.


2022-09-22 10:57

I don't understand the exact situation, but I'm not sure if it helps, but I'll upload an additional example code.

Fragment's value for Swwich to Activity to handle something event (MainActivity).java) Assuming that the following method is written,

public void changedSwitchValue(boolean value) {
    //do something
}

Fragment will be available by invoking as below.

However, if there are several classes that need to be processed by receiving an event for changing the switch value, you can also use the interface as follows.

public interface OnSwitchChangedListener {
    void changedSwitchValue(boolean value);
}

TestFragment.java

public class TestFragment extensions {
    private List<OnSwitchChangedListener> mListeners = new ArrayList<>();
    ....

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                dispatchSwitchChangedEvent(isChecked);
            }
        });
    }

    public void addOnSwitchChangedListener(OnSwitchChangedListener listener){
        if(!mListeners.contains(listener)) mListeners.add(listener);
    }

    public void removeOnSwitchChangedListener(OnSwitchChangedListener listener){
        mListeners.remove(listener);
    }

    private void dispatchSwitchChangedEvent(boolean isChecked){
        for(OnSwitchChangedListener l : mListeners){
            l.changedSwitchValue(isChecked);
        }
    }
}

This allows you to implement the OnSwitchChangedListener in the class where you want to obtain a change event in the switch value of the TestFragment, and then register and use the listener through addOnSwitchChangedListener().


2022-09-22 10:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.