aunkang

main

May 24th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.42 KB | None | 0 0
  1. public class MainFragment extends Fragment {
  2.     int someVar;
  3.     TextView tvHello;
  4.     LinearLayout layout;
  5.     SecondFragment secondFragment = new SecondFragment();
  6.  
  7.     public static MainFragment newInstance(int someVar){
  8.         MainFragment fragment = new MainFragment();
  9.         Bundle args = new Bundle(); //argument
  10.         args.putInt("someVar", someVar);
  11.         fragment.setArguments(args);
  12.         return fragment;
  13.     }
  14.  
  15.     @Override
  16.     public void onCreate(@Nullable Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.     }
  19.  
  20.     @Nullable
  21.     @Override
  22.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  23.  
  24.         View rootView = inflater.inflate(R.layout.fragment_main, container, false);
  25.  
  26.         initInstances(rootView);
  27.  
  28.  
  29.         return rootView;
  30.  
  31.  
  32.     }
  33.  
  34.     View.OnClickListener clickListener = new View.OnClickListener() {
  35.         @Override
  36.         public void onClick(View v) {
  37.             Button b = (Button) v;
  38.             if(b.getTag().toString().equals("normal")) {
  39.                 secondFragment.setAlbumnName(b.getText().toString());
  40.                 getActivity().getSupportFragmentManager().beginTransaction()
  41.                         .replace(R.id.contentContainer, SecondFragment.newInstance())
  42.                         .addToBackStack(null)
  43.                         .commit();
  44.             }else Toast.makeText(getContext(), "หมวดหมู่นี้ยังไม่มีข้อมูล", Toast.LENGTH_SHORT).show();
  45.  
  46.         }
  47.     };
  48.     public void initInstances(final View rootView){
  49.         ConnectDb connectDb = new ConnectDb();
  50.         connectDb.connectDb();
  51.         Call<PhotoItemCollectionDao> call = connectDb.getService().loadPhotoList();
  52.  
  53.         call.enqueue(new Callback<PhotoItemCollectionDao>() {
  54.             @Override
  55.             public void onResponse(Call<PhotoItemCollectionDao> call, Response<PhotoItemCollectionDao> response) {
  56.                 if(response.isSuccessful()){
  57.                     PhotoItemCollectionDao dao = response.body();
  58.                     secondFragment.setDao(dao.getData());
  59.                     layout = (LinearLayout) rootView.findViewById(R.id.index);
  60.                     int childs = 0;
  61.                     int temp=0;
  62.                     int temp2=0;
  63.                     LinearLayout a = new LinearLayout(getContext());
  64.                     a.setOrientation(LinearLayout.HORIZONTAL);
  65.                     int count=0;
  66.                     for(int i=0; i<dao.getData().size();i++){
  67.                         temp2=i;
  68.                         if(count%2==0) {
  69.                             a = new LinearLayout(getContext());
  70.                             a.setOrientation(LinearLayout.HORIZONTAL);
  71.                         }
  72.                         if(dao.getData().get(i).getParent().equals("")) {
  73.                             childs = 0;
  74.                             while (true) {
  75.  
  76.                                 if ((dao.getData().get(i).getNode() + "").equals(dao.getData().get(childs).getpNode()) || dao.getData().get(i).getStep() != null) {
  77.                                     childs = 1000;
  78.                                     break;
  79.                                 }
  80.                                 else if(childs == dao.getData().size() - 1) {
  81.                                     childs = -1000;
  82.                                     break;
  83.                                 }
  84.                                 childs++;
  85.                             }
  86.                                 count += 1;
  87.                                 if (temp == 0) {
  88.                                     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(300, 100);
  89.                                     lp.weight = 3;
  90.                                     View view = new View(getContext());
  91.                                     a.addView(view, lp);
  92.                                     lp = new LinearLayout.LayoutParams(
  93.                                             LinearLayout.LayoutParams.WRAP_CONTENT,
  94.                                             LinearLayout.LayoutParams.WRAP_CONTENT);
  95.                                     lp.weight = 3;
  96.                                     Button button = new Button(getContext());
  97.                                     if(childs==1000){
  98.                                         button.setTag("normal");
  99.                                     }
  100.                                     else if(childs==-1000){
  101.                                         button.setTag("unNormal");
  102.                                     }
  103.                                     button.setText(dao.getData().get(i).getAlbumnName());
  104.                                     button.setBackgroundResource(R.drawable.button_grid2);
  105.                                     button.setTextColor(Color.BLACK);
  106.                                     button.setTextSize(20);
  107.                                     button.setOnClickListener(clickListener);
  108.                                     a.addView(button, lp);
  109.                                     temp = 1;
  110.                                 } else {
  111.                                     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
  112.                                             LinearLayout.LayoutParams.WRAP_CONTENT,
  113.                                             LinearLayout.LayoutParams.WRAP_CONTENT);
  114.                                     lp.weight = 3;
  115.  
  116.                                     Button button = new Button(getContext());
  117.                                     if(childs==1000){
  118.                                         button.setTag("normal");
  119.                                     }
  120.                                     else if(childs==-1000){
  121.                                         button.setTag("unNormal");
  122.                                     }
  123.                                     button.setText(dao.getData().get(i).getAlbumnName());
  124.                                     button.setBackgroundResource(R.drawable.button_grid2);
  125.                                     button.setTextSize(20);
  126.                                     button.setTextColor(Color.BLACK);
  127.                                     button.setOnClickListener(clickListener);
  128.                                     a.addView(button, lp);
  129.                                     layout.addView(a);
  130.                                     temp = 0;
  131.                                 }
  132.                                 childs = 0;
  133.  
  134.                         }
  135.                     }if(temp==1){
  136.                         LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
  137.                                 LinearLayout.LayoutParams.WRAP_CONTENT,
  138.                                 LinearLayout.LayoutParams.WRAP_CONTENT);
  139.                         lp.setMargins(0,0,0,0);
  140.                         lp.weight = 3;
  141.                         Button button = new Button(getContext());
  142.                         button.setText("");
  143.                         button.setBackgroundResource(R.drawable.button_grid2);
  144.                         button.setTextColor(Color.BLACK);
  145.                         button.setTextSize(20);
  146.                         button.setOnClickListener(clickListener);
  147.                         button.setVisibility(View.INVISIBLE);
  148.                         a.addView(button,lp);
  149.                         lp = new LinearLayout.LayoutParams(
  150.                                 LinearLayout.LayoutParams.WRAP_CONTENT,
  151.                                 LinearLayout.LayoutParams.WRAP_CONTENT);
  152.                         lp.setMargins(0,0,0,0);
  153.                         layout.addView(a, lp);
  154.                     }
  155.  
  156.                 }else{
  157.                     //handle
  158.                     try {
  159.                     } catch (Exception e) {
  160.                         e.printStackTrace();
  161.                     }
  162.                 }
  163.             }
  164.  
  165.             @Override
  166.             public void onFailure(Call<PhotoItemCollectionDao> call, Throwable t) {
  167.             }
  168.         });
  169.     }
  170.  
  171.     public void setHelloText(String text){
  172.         tvHello.setText(text);
  173.     }
  174.  
  175.  
  176.     @Override
  177.     public void onSaveInstanceState(Bundle outState) {
  178.         super.onSaveInstanceState(outState);
  179.     }
  180.  
  181.     @Override
  182.     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  183.         super.onActivityCreated(savedInstanceState);
  184.         if(null != savedInstanceState){
  185.             //
  186.         }
  187.     }
  188.  
  189.  
  190. }
Add Comment
Please, Sign In to add comment