Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainFragment extends Fragment {
- int someVar;
- TextView tvHello;
- LinearLayout layout;
- SecondFragment secondFragment = new SecondFragment();
- public static MainFragment newInstance(int someVar){
- MainFragment fragment = new MainFragment();
- Bundle args = new Bundle(); //argument
- args.putInt("someVar", someVar);
- fragment.setArguments(args);
- return fragment;
- }
- @Override
- public void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment_main, container, false);
- initInstances(rootView);
- return rootView;
- }
- View.OnClickListener clickListener = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Button b = (Button) v;
- if(b.getTag().toString().equals("normal")) {
- secondFragment.setAlbumnName(b.getText().toString());
- getActivity().getSupportFragmentManager().beginTransaction()
- .replace(R.id.contentContainer, SecondFragment.newInstance())
- .addToBackStack(null)
- .commit();
- }else Toast.makeText(getContext(), "หมวดหมู่นี้ยังไม่มีข้อมูล", Toast.LENGTH_SHORT).show();
- }
- };
- public void initInstances(final View rootView){
- ConnectDb connectDb = new ConnectDb();
- connectDb.connectDb();
- Call<PhotoItemCollectionDao> call = connectDb.getService().loadPhotoList();
- call.enqueue(new Callback<PhotoItemCollectionDao>() {
- @Override
- public void onResponse(Call<PhotoItemCollectionDao> call, Response<PhotoItemCollectionDao> response) {
- if(response.isSuccessful()){
- PhotoItemCollectionDao dao = response.body();
- secondFragment.setDao(dao.getData());
- layout = (LinearLayout) rootView.findViewById(R.id.index);
- int childs = 0;
- int temp=0;
- int temp2=0;
- LinearLayout a = new LinearLayout(getContext());
- a.setOrientation(LinearLayout.HORIZONTAL);
- int count=0;
- for(int i=0; i<dao.getData().size();i++){
- temp2=i;
- if(count%2==0) {
- a = new LinearLayout(getContext());
- a.setOrientation(LinearLayout.HORIZONTAL);
- }
- if(dao.getData().get(i).getParent().equals("")) {
- childs = 0;
- while (true) {
- if ((dao.getData().get(i).getNode() + "").equals(dao.getData().get(childs).getpNode()) || dao.getData().get(i).getStep() != null) {
- childs = 1000;
- break;
- }
- else if(childs == dao.getData().size() - 1) {
- childs = -1000;
- break;
- }
- childs++;
- }
- count += 1;
- if (temp == 0) {
- LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(300, 100);
- lp.weight = 3;
- View view = new View(getContext());
- a.addView(view, lp);
- lp = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- LinearLayout.LayoutParams.WRAP_CONTENT);
- lp.weight = 3;
- Button button = new Button(getContext());
- if(childs==1000){
- button.setTag("normal");
- }
- else if(childs==-1000){
- button.setTag("unNormal");
- }
- button.setText(dao.getData().get(i).getAlbumnName());
- button.setBackgroundResource(R.drawable.button_grid2);
- button.setTextColor(Color.BLACK);
- button.setTextSize(20);
- button.setOnClickListener(clickListener);
- a.addView(button, lp);
- temp = 1;
- } else {
- LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- LinearLayout.LayoutParams.WRAP_CONTENT);
- lp.weight = 3;
- Button button = new Button(getContext());
- if(childs==1000){
- button.setTag("normal");
- }
- else if(childs==-1000){
- button.setTag("unNormal");
- }
- button.setText(dao.getData().get(i).getAlbumnName());
- button.setBackgroundResource(R.drawable.button_grid2);
- button.setTextSize(20);
- button.setTextColor(Color.BLACK);
- button.setOnClickListener(clickListener);
- a.addView(button, lp);
- layout.addView(a);
- temp = 0;
- }
- childs = 0;
- }
- }if(temp==1){
- LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- LinearLayout.LayoutParams.WRAP_CONTENT);
- lp.setMargins(0,0,0,0);
- lp.weight = 3;
- Button button = new Button(getContext());
- button.setText("");
- button.setBackgroundResource(R.drawable.button_grid2);
- button.setTextColor(Color.BLACK);
- button.setTextSize(20);
- button.setOnClickListener(clickListener);
- button.setVisibility(View.INVISIBLE);
- a.addView(button,lp);
- lp = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- LinearLayout.LayoutParams.WRAP_CONTENT);
- lp.setMargins(0,0,0,0);
- layout.addView(a, lp);
- }
- }else{
- //handle
- try {
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public void onFailure(Call<PhotoItemCollectionDao> call, Throwable t) {
- }
- });
- }
- public void setHelloText(String text){
- tvHello.setText(text);
- }
- @Override
- public void onSaveInstanceState(Bundle outState) {
- super.onSaveInstanceState(outState);
- }
- @Override
- public void onActivityCreated(@Nullable Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- if(null != savedInstanceState){
- //
- }
- }
- }
Add Comment
Please, Sign In to add comment