Hi people!
task: it is necessary to transfer to the fragment a collection of buttons extended from android.widget.Button
My solution: I create a Button class that inherits from Button and implements the Parcelable interface, then I create a Bundle in which I throw a collection of buttons using putParcelableArrayList () and in a fragment from Bundle I pull out the collection
Question: Is the correct solution. And how to solve the problem in the highlighted place of the button class code
button:
import android.content.Context; import android.os.Parcel; import android.os.Parcelable; import android.widget.Button; public class LvlButton extends Button implements Parcelable{ private int size; private String number; public LvlButton(Context context, String number , int size) { super(context); this.number = number; this.size = size; setText(number); setWidth(size); setHeight(size); } //_______________________________________________________________ //здесь просит конструктор супер класса без параметров protected LvlButton(Parcel in) { size = in.readInt(); number = in.readString(); } //_________________________________________________________________ @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(size); dest.writeString(number); } @Override public int describeContents() { return 0; } public static final Creator<LvlButton> CREATOR = new Creator<LvlButton>() { @Override public LvlButton createFromParcel(Parcel in) { return new LvlButton(in); } @Override public LvlButton[] newArray(int size) { return new LvlButton[size]; } }; } Activate:
import android.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import android.widget.FrameLayout; import android.widget.LinearLayout; import java.util.ArrayList; public class MainGameWindowActivity extends AppCompatActivity { private Button btnLeft; private Button btnRight; private Button btnSound; private Button btnMusic; private Button btnLanguage; private Button btnGooglePlay; private Button btnExit; private ArrayList <LvlButton> listBtn; private Bundle bundle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_game_window_layout); listBtn = new ArrayList<LvlButton>(); bundle = new Bundle(); for(int i = 1; i <= 10; i++){ listBtn.add(new LvlButton(this, String.valueOf(i),WindowConfiguration.getHeightDevice()/3)); } bundle.putParcelableArrayList("btnList",listBtn); //устанавливаем настройки экрана (смотри класс WindowConfiguration) WindowConfiguration.setWindowConfig(this); WindowConfiguration.getSizeDevice(this); //получаем высоту устройства int heightDevice = WindowConfiguration.getHeightDevice(); FrameLayout frameLayout = findViewById(R.id.frame_layout); //устанавливаем высоту и ширену контейнера равную реальной высоте устройства frameLayout.setLayoutParams(new LinearLayout.LayoutParams(heightDevice, heightDevice)); FragmentOfGameLevels fragment = new FragmentOfGameLevels(); fragment.setArguments(bundle); FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); fragmentTransaction.add(R.id.frame_layout, fragment); fragmentTransaction.commit(); } fragment:
import android.app.Fragment; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TableLayout; import android.widget.TableRow; import java.util.ArrayList; public class FragmentOfGameLevels extends Fragment { private int widthBtn; private int heightBtn; private ArrayList<LvlButton> list; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Context context = getActivity().getApplicationContext(); widthBtn = heightBtn= WindowConfiguration.getHeightDevice()/3; Log.i("mylog", String.valueOf(heightBtn)+ " "+ String.valueOf(widthBtn)); TableLayout table = new TableLayout(context); list = savedInstanceState.getParcelableArrayList("btnList"); TableRow row1 = new TableRow(context); TableRow row2 = new TableRow(context); TableRow row3 = new TableRow(context); row1.addView(list.get(0)); row1.addView(list.get(1)); row1.addView(list.get(2)); table.addView(row1); table.addView(row2); table.addView(row3); return table; } }