2017-05-31 43 views
0

我有一個音樂列表。在我的一行listview中,我有播放按鈕。 當用戶點擊播放按鈕,它開始播放我的音樂,我改變我的播放按鈕的背景。 我想要當用戶選擇另一個位置玩,先前的播放按鈕背景更改爲未選中,並選擇新的背景。 另一個我的問題是,當我滾動我的列表視圖時,我的背景更改爲第一張圖片,並且它不顯示正在播放哪個位置。我想當我的列表視圖更新時,我無法保存我的位置。如何保存我在listview中的位置並在滾動時更改我按鈕的圖像

這裏是我的代碼(這是我的播放按鈕點擊),

play.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       if (rowView != lastview || mediaPlayer == null) { 
        play.setImageResource(R.drawable.n_btn_play_selected); 
        play(position); 
        indexStore.setindex(position); 
        if (lastview != null) 

         lastview = rowView; 
       } else { 
        indexStore.setindex(0); 
        play.setImageResource(R.drawable.n_btn_play_unselected); 
        mediaPlayer.release(); 
        //lastview.setBackgroundResource(R.drawable.btn_white_matte); 
        lastview = null; 
       } 
      } 
     }); 

,這裏是我的全班同學:

public class Main extends Activity { 

    static Splash splash; 
    public static AppList applist; 
    static Noti_Queue noti_queue; 
    public static Video_List video_list; 
    AutoUpdatePushe autoUpdatePushe; 
    ListView listView; 
    Adaptor adaptor; 
    private MediaPlayer mediaPlayer; 
    static View lastview = null; 
    static MyIndexStore indexStore; 
    public static Routpic routpic; 
    static List<String> array_audio = new ArrayList<String>(); 
    List<String> lines1 = new ArrayList<String>(); 
    List<String> lines2 = new ArrayList<String>(); 


    InputStream in; 
    BufferedReader reader; 
    String line = "1"; 
    String[] tracks; 
    String[] names; 
    String[] infos; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     indexStore = new MyIndexStore(getApplicationContext()); 

     setContentView(R.layout.main_activity); 
     routpic = new Routpic(getApplicationContext()); 

     autoUpdatePushe = new AutoUpdatePushe(getApplicationContext()); 
     if (splash == null) { 
      splash = new Splash(this); 
      splash.set_identity("1"); 
     } 


     if (applist == null) { 
      applist = new AppList(this); 
      applist.set_identity("1"); 
     } 

     if (noti_queue == null) { 
      noti_queue = new Noti_Queue(this); 
      noti_queue.set_identity("1"); 
     } 


     if (video_list == null) { 
      video_list = new Video_List(this); 
      video_list.set_identity("1"); 
     } 
     Button video = (Button) findViewById(R.id.rate_btn); 
     if (getResources().getString(R.string.showvideolist).equals("1")) { 
      video.setVisibility(View.VISIBLE); 
     } else { 
      video.setVisibility(View.INVISIBLE); 
     } 

     initiate(); 


     mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.a15); 
     listView = (ListView) findViewById(R.id.list); 
     ReadText1(); 
     names = lines1.toArray(new String[0]);// = {"track one","the seconnd track","a nice track","name name name","the seconnd track","a nice track","name name name"}; 
     ReadText2(); 
     infos = lines2.toArray(new String[0]); 
     tracks = array_audio.toArray(new String[0]); 
     adaptor = new Adaptor(getApplicationContext(), tracks, names, infos); 
     listView.setAdapter(adaptor); 
     mediaPlayer.setOnCompletionListener(new OnCompletionListener() { 

      @Override 
      public void onCompletion(MediaPlayer arg0) { 
       if (lastview != null) { 
        ImageView play = (ImageView) lastview.findViewById(R.id.play_stop); 
        play.setImageResource(R.drawable.n_btn_play_unselected); 
       } 
      } 
     }); 
    } 

    private static void initiate() { 
     XmlPullParserFactory pullParserFactory; 
     try { 
      pullParserFactory = XmlPullParserFactory.newInstance(); 

      XmlPullParser parser = pullParserFactory.newPullParser(); 

      InputStream in_s = G.context.getAssets().open("temp.xml"); 
      parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); 
      parser.setInput(in_s, null); 
      routpic.parseXML(parser); 
     } catch (XmlPullParserException e) { 

      e.printStackTrace(); 
      Log.i("EROR", "nabod"); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      Log.i("EROR", "nabod"); 

     } 
     array_audio.clear(); 
     for (int i = 0; i < routpic.names.size(); i++) { 
      array_audio.add(routpic.names.get(i)); 
      Log.i("LOG2", "" + routpic.names.get(i)); 
     } 


    } 

    private void play(int index) { 
     mediaPlayer.release(); 
     index++; 
     String s = "mp3/a" + index + ".mp3"; 
     //Resources resources = getResources(); 

     AssetFileDescriptor afd; 
     try { 
      afd = getAssets().openFd(s); 
      mediaPlayer = new MediaPlayer(); 
      mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); 
      mediaPlayer.prepare(); 
      mediaPlayer.start(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 


    // final int resourceId = resources.getIdentifier(s , "raw", getPackageName()); 


    @Override 
    protected void onPause() { 
     mediaPlayer.release(); 
     listView.invalidateViews(); 
     super.onPause(); 
    } 


    private void ReadText1() { 
     lines1.clear(); 
     line = "1"; 
     try { 
      in = this.getAssets().open("names.txt"); 
      reader = new BufferedReader(new InputStreamReader(in)); 
      while (line != null) { 
       line = reader.readLine(); 
       if (line != null) 
        lines1.add(line); 
       else 
        break; 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 


    private void ReadText2() { 
     lines2.clear(); 
     line = "1"; 
     try { 
      in = this.getAssets().open("infos.txt"); 
      reader = new BufferedReader(new InputStreamReader(in)); 
      while (line != null) { 
       line = reader.readLine(); 
       if (line != null) 
        lines2.add(line); 
       else 
        break; 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 


    public class Adaptor extends ArrayAdapter<String> { 

     private final Context context; 
     private final String[] tracks; 
     private final String[] names; 
     private final String[] infos; 
     Typeface type_face; 

     public Adaptor(Context context, String[] tracks, String[] names, String[] infos) { 
      super(context, R.layout.track, tracks); 
      this.context = context; 
      this.tracks = tracks; 
      this.names = names; 
      this.infos = infos; 
      type_face = Typeface.createFromAsset(context.getAssets(), "ARLRDBD.TTF"); 
     } 


     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      final View rowView = inflater.inflate(R.layout.track, parent, false); 
      // rowView.setBackgroundResource(R.drawable.btn_white_matte); 
      TextView name = (TextView) rowView.findViewById(R.id.track_name); 
      name.setText(names[position]); 
      name.setTypeface(type_face); 
      //TextView info = (TextView) rowView.findViewById(R.id.track_info); 
      //info.setText(infos[position]); 
      name.setTypeface(type_face); 
      AlphaAnimation fadeIn = new AlphaAnimation(0.0f, 1.0f); 
      rowView.setAnimation(fadeIn); 
      fadeIn.setDuration(500 * (position % 4)); 

      //// 
      final ImageView ringtone = (ImageView) rowView.findViewById(R.id.ringtone); 
      if (position == indexStore.getindex()) 
       ringtone.setImageResource(R.drawable.n_btn_ringtone_seted); 
      final ImageView play = (ImageView) rowView.findViewById(R.id.play_stop); 
      ringtone.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        //ringtone.setImageResource(R.drawable.n_btn_ringtone_selected); 
        setringtone(position, ringtone); 
        //mediaPlayer.release(); 
        //rowView .setBackgroundResource(R.drawable.btn_white_matte); 
        play.setImageResource(R.drawable.n_btn_play_unselected); 


       } 
      }); 

      play.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        if (rowView != lastview || mediaPlayer == null) { 
         play.setImageResource(R.drawable.n_btn_play_selected); 
         play(position); 
         indexStore.setindex(position); 
         if (lastview != null) 

          lastview = rowView; 
        } else { 
         indexStore.setindex(0); 
         play.setImageResource(R.drawable.n_btn_play_unselected); 
         mediaPlayer.release(); 
         //lastview.setBackgroundResource(R.drawable.btn_white_matte); 
         lastview = null; 
        } 
       } 
      }); 
      return rowView; 
     } 

     private void setringtone(final int position, final ImageView ringtone) { 
      /*Intent intent = new Intent (context, Ringtone.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      intent.putExtra("name", tracks[position]); 
      context.startActivity(intent);*/ 
      //ringtone.setImageResource(R.drawable.n_btn_ringtone_unselected); 
      final RelativeLayout dialog = (RelativeLayout) findViewById(R.id.config); 
      dialog.setVisibility(View.VISIBLE); 
      Button ok = (Button) findViewById(R.id.ok_button); 
      Button cancel = (Button) findViewById(R.id.cancel_button); 
      Typeface type_face = Typeface.createFromAsset(context.getAssets(), "ARLRDBD.TTF"); 
      ok.setTypeface(type_face); 
      cancel.setTypeface(type_face); 
      ok.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        indexStore.setindex(position); 
        set(tracks[position], 1, ringtone, position); 
       } 
      }); 
      cancel.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        dialog.setVisibility(View.INVISIBLE); 
        mediaPlayer.release(); 
       } 
      }); 
     } 

    } 


    public void set(String name, int type, ImageView ringtone, int position) { 
     // mohammd 
     //set ringtone 
     //adaptor.setNotifyOnChange(true); 
     listView.invalidateViews(); 
     Resources resources = getResources(); 

     //final int id = resources.getIdentifier(name , "raw", getPackageName());//name 5 


     String ringtoneuri = Environment.getExternalStorageDirectory().getAbsolutePath() + "/media/ringtone"; 
     File file1 = new File(ringtoneuri); 
     file1.mkdirs(); 

     File newSoundFile = new File(ringtoneuri, names[position] + ".mp3");//+ ".mp3" 

     ContentResolver mCr = this.getContentResolver(); 
     try { 
      byte[] readData = new byte[1024]; 

      InputStream fis = getAssets().open("mp3/a" + name + ".mp3"); 

      FileOutputStream fos = new FileOutputStream(newSoundFile); 

      int i = fis.read(readData); 

      while (i != -1) { 
       fos.write(readData, 0, i); 
       i = fis.read(readData); 
      } 

      fos.close(); 
     } catch (IOException io) { 
     } 

     ContentValues values = new ContentValues(); 
     values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath()); 
     values.put(MediaStore.MediaColumns.TITLE, names[position] + ".mp3"); 
     values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); 
     values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length()); 
     values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name); 
     values.put(MediaStore.Audio.Media.IS_RINGTONE, true); 
     values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true); 
     values.put(MediaStore.Audio.Media.IS_ALARM, true); 
     values.put(MediaStore.Audio.Media.IS_MUSIC, false); 

     Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath()); 
     Uri newUri = mCr.insert(uri, values); 
     try { 
      Uri rUri = RingtoneManager.getValidRingtoneUri(this); 
      RingtoneManager ringtoneManager = new RingtoneManager(getApplicationContext()); 
      if (rUri != null) 
       ringtoneManager.setStopPreviousRingtone(true); 
      switch (type) { 
       case 1: 
        //ring tone 
        ringtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri); 
        break; 
       case 2: 
        //Notification 
        RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_NOTIFICATION, newUri); 
        break; 
       case 3: 
        //Alarm 
        RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_ALARM, newUri); 
        break; 

       default: 
        //ringtone 
        RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri); 
        break; 
      } 

     } catch (Throwable t) { 
      Log.e("catch", "catch exception" + t.getMessage()); 
     } 
     RelativeLayout dialog = (RelativeLayout) findViewById(R.id.config); 
     dialog.setVisibility(View.INVISIBLE); 
     mediaPlayer.release(); 

     Toast.makeText(getApplicationContext(), getResources().getString(R.string.set_message), Toast.LENGTH_LONG).show(); 
    } 




    public void more(View view) { 
     applist.Display(); 
    } 

    @Override 
    public void onBackPressed() { 
     splash.Display(); 
     splash = null; 
     super.onBackPressed(); 
    } 



    public void video(View view) { 
     video_list.Display(); 
    } 


} 
+0

在您的適配器「selectedTrack」中添加一個變量,您可以在getView –

+0

中重用如何更改它? – user7908469

+0

使用onClickListener中的新位置更改它,然後在getView中檢查所選位置以設置行背景。 –

回答

0

使用布爾標誌識別Like按鈕狀態的例子:isPlaying並改變背景像一個完整的工作示例可在這裏獲得更多的無罪釋放。

Audio Recorder

乾杯。

+0

我只有一個按鈕不是兩個按鈕 – user7908469

+0

正確的相同概念可以應用於那一個按鈕狀態。 –

相關問題