SHARE
TWEET

Untitled

a guest Jun 24th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Fragment1 extends Fragment implements View.OnClickListener {
  2.  
  3.     RoundedImageView imgFairytales;
  4.     TextView txtFairytales;
  5.     TextView txtAuthor;
  6.     AdView mAdView;
  7.     private ImageButton exo_pause;
  8.     private ImageButton exo_play;
  9.     private ImageButton btnNext;
  10.     private ImageButton btnPrev;
  11.  
  12.     private int songsCount = 0;
  13.  
  14.     private int id;
  15.     private int currentID;
  16.     String songTitle;
  17.     String songImage;
  18.     String songFile;
  19.     String songAuthor;
  20.     String songText;
  21.  
  22.     static boolean isPlay = false;
  23.  
  24.     private Animation scaleAnimation = null;
  25.  
  26.     OnDataSelectListener onDataSelectListener;
  27.  
  28.     public static Fragment1 newInstance(int id, String songTitle, String songImage, String songFile, String songAuthor, String songText, int songsCount) {
  29.         Bundle args = new Bundle();
  30.         args.putInt("ID", id);
  31.         args.putString("songTitle", songTitle);
  32.         args.putString("songImage", songImage);
  33.         args.putString("songFile", songFile);
  34.         args.putString("songAuthor", songAuthor);
  35.         args.putString("songText", songText);
  36.         args.putInt("songsCount", songsCount);
  37.  
  38.         System.out.println("id on newInstance " + id); //ТУТ ЕСТЬ ЗНАЧЕНИЕ
  39.  
  40.         Fragment1 fragment1 = new Fragment1();
  41.         fragment1.setArguments(args);
  42.         return fragment1;
  43.     }
  44.  
  45.     @Nullable
  46.     @Override
  47.     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  48.         View rootView =  inflater.inflate(R.layout.fragment1, container, false);
  49.  
  50.         imgFairytales = rootView.findViewById(R.id.imgFairytales);
  51.         txtFairytales = rootView.findViewById(R.id.txtFairytales);
  52.         txtAuthor = rootView.findViewById(R.id.txtAuthor);
  53.  
  54.         KidsPlayer.playerView = new PlayerView(getContext());
  55.         KidsPlayer.playerView = rootView.findViewById(R.id.player);
  56.  
  57.         exo_pause = rootView.findViewById(R.id.exo_pause);
  58.         exo_play = rootView.findViewById(R.id.exo_play);
  59.         btnNext = rootView.findViewById(R.id.btnNext);
  60.         btnPrev = rootView.findViewById(R.id.btnPrev);
  61.  
  62.         mAdView = rootView.findViewById(R.id.adView);
  63.  
  64.         return rootView;
  65.     }
  66.  
  67.     @Override
  68.     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  69.         super.onViewCreated(view, savedInstanceState);
  70.  
  71.         id = getArguments().getInt("id", 0);
  72.         songTitle = getArguments().getString("songTitle");
  73.         songImage = getArguments().getString("songImage");
  74.         songFile = getArguments().getString("songFile");
  75.         songAuthor = getArguments().getString("songAuthor");
  76.         songText = getArguments().getString("songText");
  77.         songsCount = getArguments().getInt("songsCount");
  78.  
  79.         System.out.println("id on onViewCreated " + id); //ТУТ УЖЕ 0 ПОЛУЧАЮ
  80.  
  81.         txtFairytales.setText(songTitle);
  82.         txtAuthor.setText(songAuthor);
  83.  
  84.         Glide.with(getActivity()).load(Uri.parse("file:///android_asset/img/" + songImage + ".jpg")).into(imgFairytales);
  85.  
  86.         startPlayerService();
  87.  
  88.         btnPrev.setOnClickListener(this);
  89.         exo_pause.setOnClickListener(this);
  90.         exo_play.setOnClickListener(this);
  91.         btnNext.setOnClickListener(this);
  92.     }
  93.  
  94.     @Override
  95.     public void onClick(View view) {
  96.         switch (view.getId()) {
  97.             case R.id.btnPrev:
  98.                 onPrev();
  99.                 setScaleAnimation(view);
  100.                 break;
  101.             case R.id.exo_pause:
  102.                 stopPlayerService();
  103.                 setScaleAnimation(view);
  104.                 break;
  105.             case R.id.exo_play:
  106.                 startPlayerService();
  107.                 setScaleAnimation(view);
  108.                 break;
  109.             case R.id.btnNext:
  110.                 onNext();
  111.                 setScaleAnimation(view);
  112.                 break;
  113.         }
  114.     }
  115.  
  116.     public void setScaleAnimation(View v) {
  117.         scaleAnimation = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.scale);
  118.         v.startAnimation(scaleAnimation);
  119.     }
  120.  
  121.     public void startPlayerService() {
  122.         Intent serviceIntent = new Intent(getActivity(), PlayerService.class);
  123.         serviceIntent.putExtra(PlayerService.KEY_STREAM, songFile);
  124.         serviceIntent.putExtra(PlayerService.KEY_TITLE, songTitle);
  125.         serviceIntent.setAction(PlayerConstants.ACTION.STARTFOREGROUND_ACTION);
  126.         getActivity().startService(serviceIntent);
  127.         isPlay = true;
  128.         if(exo_play.getVisibility() == View.VISIBLE) {
  129.             exo_play.setVisibility(View.GONE);
  130.             exo_pause.setVisibility(View.VISIBLE);
  131.  
  132.         }
  133.     }
  134.  
  135.     private void stopPlayerService() {
  136.         Intent serviceIntent = new Intent(getActivity(), PlayerService.class);
  137.         serviceIntent.setAction(PlayerConstants.ACTION.STOPFOREGROUND_ACTION);
  138.         getActivity().stopService(serviceIntent);
  139.         isPlay = false;
  140.         if(exo_pause.getVisibility() == View.VISIBLE) {
  141.             exo_pause.setVisibility(View.GONE);
  142.             exo_play.setVisibility(View.VISIBLE);
  143.         }
  144.     }
  145.  
  146.     public void onNext() {
  147.         currentID = id + 1;
  148.         if (currentID < 1) {
  149.             btnPrev.setEnabled(false);
  150.         } else {
  151.             btnPrev.setEnabled(true);
  152.             btnNext.setEnabled(true);
  153.             selectData(currentID);
  154.             startPlayerService();
  155.         }
  156.     }
  157.  
  158.     public void onPrev() {
  159.         currentID = id - 1;
  160.         if (currentID > songsCount) {
  161.             btnNext.setEnabled(false);
  162.         } else {
  163.             btnPrev.setEnabled(true);
  164.             btnNext.setEnabled(true);
  165.             selectData(currentID);
  166.             startPlayerService();
  167.         }
  168.     }
  169.  
  170.     private void selectData(int idSong) {
  171.  
  172.         SongDao songDao = AppDatabase.createPersistentDatabase(getActivity()).songDao();
  173.  
  174.         songDao.getById(idSong).subscribeOn(Schedulers.io())
  175.                 .observeOn(AndroidSchedulers.mainThread())
  176.                 .subscribe(new DisposableMaybeObserver<Song>() {
  177.                     @Override
  178.                     public void onSuccess(Song song) {
  179.  
  180.                         id = song.getId();
  181.                         String strSongFile = song.getFilename();
  182.                         songTitle = song.getTitle();
  183.                         songImage = song.getImage();
  184.                         songAuthor = song.getAuthor();
  185.  
  186.                         songText = song.getText();
  187.                         songText = songText.replace("\n", "n");
  188.  
  189.                         songFile = "file:///android_asset/music/" + strSongFile + ".mp3";
  190.  
  191.                         txtFairytales.setText(songTitle);
  192.                         txtAuthor.setText(songAuthor);
  193.                         Glide.with(getActivity()).load(Uri.parse("file:///android_asset/img/" + songImage + ".jpg")).into(imgFairytales);
  194.  
  195.                         onDataSelectListener.onSelectData(songText);
  196.  
  197.                         currentID = id;
  198.  
  199.                     }
  200.  
  201.                     @Override
  202.                     public void onError(Throwable e) {
  203.  
  204.                     }
  205.  
  206.                     @Override
  207.                     public void onComplete() {
  208.  
  209.                     }
  210.                 });
  211.     }
  212.  
  213.     public interface OnDataSelectListener{
  214.         void onSelectData(String query);
  215.     }
  216.  
  217.     @Override
  218.     public void onAttach(Context context) {
  219.         super.onAttach(context);
  220.  
  221.         try {
  222.             onDataSelectListener = (OnDataSelectListener) getActivity();
  223.         } catch (ClassCastException e) {
  224.             throw new ClassCastException("Error in retrieving data. Please try again");
  225.         }
  226.     }
  227. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top