Advertisement
aunkang

ThirdFragment.java

May 18th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.29 KB | None | 0 0
  1. package com.example.analogy.fragment.Fragment;
  2.  
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.support.annotation.Nullable;
  6. import android.support.v4.app.Fragment;
  7. import android.view.Gravity;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.Button;
  12. import android.widget.ImageView;
  13. import android.widget.LinearLayout;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. import com.bumptech.glide.Glide;
  18. import com.example.analogy.fragment.R;
  19.  
  20. import java.io.IOException;
  21. import java.util.HashMap;
  22. import java.util.List;
  23.  
  24. /**
  25.  * Created by Analogy on 4/26/2017.
  26.  */
  27.  
  28. public class ThirdFragment extends Fragment{
  29.     private String url = "http://192.168.1.6/dashboard/senior-project/uploads/thumbs/";
  30.     private String urlSound = "http://192.168.1.6/dashboard/senior-project/uploads/audios/";
  31.     String tempUrl="";
  32.     private ImageView back, backHome;
  33.     private ImageView showAll;
  34.     private Sound sound = new Sound();
  35.     private int countClick=0;
  36.     private HashMap<Integer,String> imgStep = new HashMap<>();
  37.     private HashMap<Integer,String> soundStep =new HashMap<>();
  38.     private HashMap<Integer,String> description =new HashMap<>();
  39.     static private String albumnName = "";
  40.     static private List<PhotoItemDao> dao;
  41.     private TextView tvTitle;
  42.     private ImageView ivImg1,ivImg2,ivImg3,ivImg4,ivImg5,ivImg6,ivImg7,ivImg8,ivImg9,ivImg10;
  43.  
  44.     private int index=0;
  45.     public static List<PhotoItemDao> getDao() {
  46.         return dao;
  47.     }
  48.  
  49.  
  50.     public static void setDao(List<PhotoItemDao> dao) {
  51.         ThirdFragment.dao = dao;
  52.     }
  53.  
  54.     public static ThirdFragment newInstance(String someVar){
  55.         ThirdFragment fragment = new ThirdFragment();
  56.         albumnName = someVar;
  57.         Bundle args = new Bundle(); //argument
  58.         args.putString("someVar", someVar);
  59.         fragment.setArguments(args);
  60.         return fragment;
  61.     }
  62.  
  63.  
  64.     public HashMap<Integer, String> getImgStep() {
  65.         return imgStep;
  66.     }
  67.  
  68.     public void setImgStep(HashMap<Integer, String> imgStep) {
  69.         this.imgStep = imgStep;
  70.     }
  71.  
  72.     @Override
  73.     public void onCreate(@Nullable Bundle savedInstanceState) {
  74.         super.onCreate(savedInstanceState);
  75.  
  76.         Toast.makeText(getContext(), "This is image albumn", Toast.LENGTH_SHORT).show();
  77.  
  78.     }
  79.  
  80.     @Nullable
  81.     @Override
  82.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  83.  
  84.         View rootView = inflater.inflate(R.layout.fragment_third, container, false);
  85.  
  86.         initInstances(rootView);
  87.  
  88.  
  89.         return rootView;
  90.  
  91.  
  92.     }
  93.  
  94.     View.OnClickListener clickListener = new View.OnClickListener() {
  95.         @Override
  96.         public void onClick(View v) {
  97.  
  98.  
  99.  
  100.             if(v == back){
  101.                 getFragmentManager().popBackStack();
  102.                 try {
  103.                     sound.pause();
  104.                 } catch (IOException e) {
  105.                     e.printStackTrace();
  106.                 }
  107.             }
  108.             else if(v == backHome){
  109.                 getFragmentManager().popBackStack(null, getFragmentManager().POP_BACK_STACK_INCLUSIVE);
  110.                 try {
  111.                     sound.pause();
  112.                 } catch (IOException e) {
  113.                     e.printStackTrace();
  114.                 }
  115.             }
  116.             else if(v == showAll){
  117.  
  118.                 fifthFragment fifthFragment = new fifthFragment();
  119.                 fifthFragment.setDao(getDao());
  120.                 fifthFragment.setAlbumnName(albumnName);
  121.                 fifthFragment.setImgStep(getImgStep());
  122.                 fifthFragment.setSoundStep(soundStep);
  123.                 fifthFragment.setDescription(description);
  124.                 getActivity().getSupportFragmentManager().beginTransaction()
  125.                         .add(R.id.contentContainer, fifthFragment.newInstance(index))
  126.                         .addToBackStack(null)
  127.                         .commit();
  128.             }
  129.             else{
  130.  
  131.                 Button button = (Button) v;
  132.  
  133.                 if(countClick>0){
  134.                     try {
  135.                         sound.pause();
  136.                     } catch (IOException e) {
  137.                         e.printStackTrace();
  138.                     }
  139.                 }
  140.                 if(tempUrl.equals((button.getText().toString()))){
  141.                     sound.restart();
  142.                     countClick++;
  143.                 }
  144.                 else if(tempUrl.equals("")){
  145.                     tempUrl = button.getText().toString();
  146.                     sound.prepareToPlay(button.getText().toString());
  147.                     sound.play();
  148.                     countClick++;
  149.                 }
  150.                 else if(!tempUrl.equals((button.getText().toString()))){
  151.                     tempUrl = button.getText().toString();
  152.                     sound.prepareToPlay(button.getText().toString());
  153.                     sound.play();
  154.                     countClick++;
  155.                 }
  156.  
  157.             }
  158.  
  159.             /*if(v.getTag().toString().equals("sound")){
  160.                 Button b = (Button) v;
  161.                 if(countClick>0){
  162.                     try {
  163.                         sound.pause();
  164.                     } catch (IOException e) {
  165.                         e.printStackTrace();
  166.                     }
  167.                 }
  168.  
  169.                 sound.prepareToPlay(b.getText().toString());
  170.                 sound.play();
  171.                 countClick++;
  172.             }*/
  173.         }
  174.     };
  175.     public void initInstances(final View rootView){
  176.         back = (ImageView) rootView.findViewById(R.id.back);
  177.         backHome = (ImageView) rootView.findViewById(R.id.back_home);
  178.         showAll = (ImageView) rootView.findViewById(R.id.showAll);
  179.         showAll.setOnClickListener(clickListener);
  180.         tvTitle = (TextView) rootView.findViewById(R.id.tvTitle);
  181.         tvTitle.setText(albumnName);
  182.         for(int i=0;i<getDao().size();i++){
  183.             if(albumnName.equals(getDao().get(i).getAlbumnName())){
  184.                 index = i;
  185.                 break;
  186.             }
  187.         }
  188.  
  189.  
  190.         back.setOnClickListener(clickListener);
  191.         backHome.setOnClickListener(clickListener);
  192.         List<String> temp;
  193.         temp = getDao().get(index).getStep().getAllStep();
  194.         int count=0;
  195.         for(String img : temp){
  196.             count++;
  197.             if(!img.equals("") || !img.isEmpty()){
  198.                 imgStep.put(count, img);
  199.             }
  200.         }
  201.  
  202.         temp = getDao().get(index).getSoundStep().getAllStep();
  203.         count=0;
  204.         for(String img : temp){
  205.             count++;
  206.             if(!img.equals("")){
  207.                 soundStep.put(count, img);
  208.             }
  209.         }
  210.         temp = getDao().get(index).getDescription().getAllStep();
  211.         count=0;
  212.         for(String img : temp){
  213.             count++;
  214.             if(!img.equals("")){
  215.                 description.put(count, img);
  216.             }
  217.         }
  218.         count=0;
  219.         int x=0;
  220.         for(int i=0;i<=imgStep.size();i++){
  221.             while(true){
  222.                 x++;
  223.                 if(imgStep.containsKey(x)){
  224.                     LinearLayout container = (LinearLayout)rootView.findViewById(R.id.contentContainer);
  225.                     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
  226.                             LinearLayout.LayoutParams.WRAP_CONTENT,
  227.                             LinearLayout.LayoutParams.WRAP_CONTENT
  228.                     );
  229.  
  230.                     String temp2 = "ivImg" + x;
  231.                     TextView tv = new TextView(getContext());
  232.                     tv.setText("ขั้นตอนที่"+x);
  233.                     tv.setTextSize(25);
  234.                     tv.setTextColor(Color.BLACK);
  235.                     tv.setBackgroundColor(Color.parseColor("#FFEBBE"));
  236.                     lp.gravity = Gravity.CENTER;
  237.                     container.addView(tv, lp);
  238.                     lp = new LinearLayout.LayoutParams(300,300);
  239.  
  240.                     //get url image here
  241.  
  242.                     ImageView img = new ImageView(getContext());
  243.                     Image imgLaod = new Image();
  244.  
  245.                     lp.gravity = Gravity.CENTER;
  246.                     container.addView(img, lp);
  247.                     Glide.with(getContext()).load(url+imgStep.get(x)).into(img);
  248.                     Toast.makeText(getContext(),url+imgStep.get(x),Toast.LENGTH_SHORT ).show();
  249.                     if(description.containsKey(x)){
  250.                         Toast.makeText(getContext(),description.get(x), Toast.LENGTH_LONG).show();
  251.                         lp = new LinearLayout.LayoutParams(
  252.                                 LinearLayout.LayoutParams.WRAP_CONTENT,
  253.                                 LinearLayout.LayoutParams.WRAP_CONTENT
  254.                         );
  255.                         tv = new TextView(getContext());
  256.                         tv.setText(description.get(x));
  257.                         lp.gravity = Gravity.CENTER;
  258.                         lp.setMargins(0,0,0,50);
  259.                         container.addView(tv, lp);
  260.                     }
  261.                     if(soundStep.containsKey(x)){
  262.                         lp = new LinearLayout.LayoutParams(
  263.                                 LinearLayout.LayoutParams.WRAP_CONTENT,
  264.                                 LinearLayout.LayoutParams.WRAP_CONTENT
  265.  
  266.                         );
  267.                         lp.gravity = Gravity.CENTER;
  268.                         lp.setMargins(0,0,0,200);
  269.                         Button btn = new Button(getContext());
  270.  
  271.                         //get url sound here
  272.                         btn.setText(soundStep.get(x));
  273.                         btn.setTextSize(0);
  274.                         btn.setTag("sound");
  275.                         btn.setOnClickListener(clickListener);
  276.                         container.addView(btn, lp);
  277.                     }
  278.                 }
  279.                 break;
  280.             }
  281.         }
  282.  
  283.     }
  284.  
  285.     @Override
  286.     public void onSaveInstanceState(Bundle outState) {
  287.         super.onSaveInstanceState(outState);
  288.     }
  289.  
  290.     @Override
  291.     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  292.         super.onActivityCreated(savedInstanceState);
  293.         if(null != savedInstanceState){
  294.             //
  295.         }
  296.     }
  297.  
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement