lundi 10 avril 2017

android : how to separate logic of activity, fragment n popup window

i am trying to separate the logic of fragment n popupwindow in separate java file but having variable assigned in the event of popup window passed to the field in activity class.

the sequence flow of event n the variable to be passed is like this.

  1. fragment layout loaded based on a variable x from application class if x =4, fragment layout with 4 buttons is loaded n so on.

  2. when a button in the loaded fragment is clicked, a popup window shows up. the layout of loaded popup window also depends on a variable y if y=5, popup window layout with 5 buttons is loaded n so on.

  3. when a button in the popupwindow is clicked, a certain variable z is assigned to a field in the activity class n the popup window is dismissed.

if all these r coded in the same activity class, my problem might be gone but this is not dat elegant.

i wonder how to do all these with the logic of activity, fragment n popup window separated in 3 different classes. It would be best if there is any code example.

The following is the code of my fragment class. when the button in the fragment loaded was clicked, it didn't even hv any response.

also, there seems to be some problem in the inflation of popup window when the fragment is loaded as i hv seen inflation exception when popup window layout is loaded. as long as the above problem haven't been solved, i juz commented out the code of popup window.

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

public class ColorPickerFragment extends Fragment 
implements View.OnClickListener{

int gNumPin, gNumColor, gPickerLayout, gPaletteLayout;
View cView;
PopupWindow gPalettePopUp;

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.guess_401:
                Log.d(getClass().toString(),"guess 401");
                Toast.makeText(getActivity().getApplicationContext()," guess 401",Toast.LENGTH_SHORT).show();
                break;
            case R.id.color_401:
                Toast.makeText(getActivity().getApplicationContext()," color 401",Toast.LENGTH_SHORT).show();
                break;
            case R.id.color_402:

                break;
            default:
                break;
        }
    }


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    MMApp mm=  (MMApp) this.getActivity().getApplication();
    gNumPin=mm.get_num_pin();
    gNumColor=mm.get_num_color();

    switch(gNumPin){
        case 4:
            gPickerLayout =R.layout.player_color_guess_4;
            break;
        case 5:
            gPickerLayout =R.layout.player_color_guess_5;
            break;
        case 6:
            gPickerLayout =R.layout.player_color_guess_6;
            break;
        default:
            gPickerLayout =R.layout.player_color_guess_4;
    }

    cView = inflater.inflate(gPickerLayout, container,false);

    Button btn_guess01 = (Button) cView.findViewById(R.id.guess_401);
    Button btn_guess02 = (Button) cView.findViewById(R.id.guess_402);
    Button btn_guess03 = (Button) cView.findViewById(R.id.guess_403);
    Button btn_guess04 = (Button) cView.findViewById(R.id.guess_404);

    btn_guess01.setOnClickListener(this);

    //initColorPickerBtn();

    //initColorPalette();

    return inflater.inflate(gPickerLayout, container, false);
}

private void initColorPickerBtn() {
    Button btn_guess01 = (Button) cView.findViewById(R.id.guess_401);
    Button btn_guess02 = (Button) cView.findViewById(R.id.guess_402);
    Button btn_guess03 = (Button) cView.findViewById(R.id.guess_403);
    Button btn_guess04 = (Button) cView.findViewById(R.id.guess_404);

    btn_guess01.setOnClickListener(this);
}

private void initColorPalette(){

    gPalettePopUp = new PopupWindow(
            cView,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);

    initColorPaletteBtn();

}

private void initColorPaletteBtn() {

    Button btn_color01 = (Button)cView.findViewById(R.id.color_401);
    Button btn_color02 = (Button)cView.findViewById(R.id.color_402);
    Button btn_color03 = (Button)cView.findViewById(R.id.color_403);
    Button btn_color04 = (Button)cView.findViewById(R.id.color_404);

    btn_color01.setOnClickListener(this);
}

}

Aucun commentaire:

Enregistrer un commentaire