I want to make one screen function a different file.

Asked 2 years ago, Updated 2 years ago, 80 views

I have a question about Android.

I'm thinking of an app that has a layout that places GridView and ListView up and down in a 1:1 ratio on one screen.

When I tap on GridView above, I would like to link it up like updating ListView at the event, but I am a little worried that the activity file is getting bigger.

I would like to drop the management of each of these views into a different activity, is that possible?

In that case, I would also like to know the flow of getting tap events.
Thank you for your cooperation.

android

2022-09-30 21:17

1 Answers

I think you can use Fragment.
I don't know if it's the best way to do it, but I'll write down some code.

MainActivity.java

//import is omitted.
public class MainActivity extensions AppCompatActivity implements View.OnClickListener{
    private EditText text;

    @ Override
    protected void onCreate (Bundle state) {
        super.onCreate(state);
        setContentView (R.layout.main);
        // Add FrameLayout, etc. to the layout and replace it with the Fragment that added ListView.
        // Here we use Button and EditText.Replace it with GridView and think about it.
        etext=(EditText)findViewById(R.id.etext);
        Button btn=(Button) findViewById (R.id.button);
        btn.setOnClickListener(this);
    }

    @ Override
    public void onClick (View v) {
        // Invoke the onItemAdd method implemented in Fragment.
        if(listener!=null){
            listener.onItemAdd(etext.getText().toString);
        }
    }

    private onItemAddListener listener;

    public void setOnItemAddListener(OnItemAddListener){
        This.listener=listener;
    }

    public interface onItemAddListener{
        // Specifies the type of value that you want to pass to Fragment.
        void onAddItemAdd(String label);
    }
}

Fragmemt
to which ListView is added ListFragment.java

//import is omitted.
public class ListFragment extensions Fragment implements MainActivity.OnItemAddListener{
    public ListFragment() {/*empty*/}

    private TextView textView;

    @ Override
publicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
        View v=inflater.inflate(R.layout.list, container, false);

        textView=(TextView) v.findViewById (R.id.textView);
        MainActivity=(MainActivity) getActivity();
        activity.setOnItemAddListener(this);

        return v;
    }

    @ Override
    public void onItemAdd(String label) {
        textView.setText(textView.getText().toString()+label);
    }

Although the Fragment processing was expressed in TextView, it is possible to update to ListView by changing the processing of ListFragment's onItemAdd method.


2022-09-30 21:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.