Seekbar在自定义列表视图上使用位置运行

时间:2016-12-08 10:04:33

标签: android listview android-seekbar

我正在开发一个音乐播放器,其中歌曲在我的列表行的列表视图中膨胀。我玩过暂停按钮和搜索栏。当我点击列表视图中的第一首歌曲的播放按钮时,第一首歌曲将开始播放但是所有歌曲都会寻找条形图更改其位置开始运行。

我想在单击时只进行单一搜寻。

这是我的适配器代码:

 public abstract class CustomAdapter extends BaseAdapter implements  SeekBar.OnSeekBarChangeListener {
    Context context;
    ArrayList<HashMap<String, String>> countryList;
    ArrayList<HashMap<String, String>> mStringFilterList;
    LayoutInflater inflter;
    public ImageView img2,img3;
    Handler mHandler = new Handler();
    SeekBar songProgressBar;

  public CustomAdapter(Context applicationContext, ArrayList<HashMap<String, String>> countryList) {
        this.context = applicationContext;
        this.countryList = countryList;
        mStringFilterList =  countryList;
        inflter = (LayoutInflater.from(applicationContext));
    }

    @Override
    public int getCount() {
        return countryList.size();
    }

    public void updateData(ArrayList<HashMap<String, String>> countryList) {
        this.countryList = countryList;
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        view = inflter.inflate(R.layout.list_itemss, null);

        String hello = String.valueOf(countryList.get(position));
        String s = hello;
        int s1 = s.lastIndexOf("=");
        int s2 = s.lastIndexOf("}");

        String strSub = s.substring(s1+1,s2/*s.lastIndexOf("=")*/);
        Log.d("Hello",hello);       
        String henno1 = String.valueOf(hello.length());
        Log.d("hellya",henno1);
        songProgressBar = (SeekBar) view.findViewById(R.id.songProgressBar);

        TextView country = (TextView) view.findViewById(R.id.textView);
        country.setText(strSub);

        songProgressBar.setOnSeekBarChangeListener(this);

        songCurrentDurationLabel = (TextView)view.findViewById(R.id.songCurrentDurationLabel);

        songTotalDurationLabel = (TextView)view.findViewById(R.id.songTotalDurationLabel);

        img2 = (ImageView)view.findViewById(R.id.button3);
        img3 = (ImageView)view.findViewById(R.id.button2);
        img3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(mp.isPlaying()){
                    if(mp!=null){
                        mp.pause();
                        Toast.makeText(Main.this,"Pause --",Toast.LENGTH_LONG).show();       
                   }
                }else{
                    Toast.makeText(Main.this,"Resume --",Toast.LENGTH_LONG).show();
                    // Resume song
                    if(mp!=null){
                       mp.start();                       
                  }
                }
            }
        });
        img2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                img2.setVisibility(View.GONE);
                img3.setVisibility(View.VISIBLE);
                View parentRow = (View) view.getParent();
                Log.d("parentrow", String.valueOf(parentRow));
                //  Toast.makeText(Main.this,"Position is --"+position,Toast.LENGTH_LONG).show();
                int songIndex = position;

                currentSongIndex=songIndex;

                playSong(currentSongIndex);




                songProgressBar.setFocusable(true);
                songProgressBar.setEnabled(true);
                if (mp.isPlaying()) {
                    ((ImageView) view).setImageResource(R.drawable.pausebutton);

                }        
            }
        });


        ImageView img =(ImageView)view.findViewById(R.id.button);

        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(Main.this,"Added to Playlist",Toast.LENGTH_LONG).show();
                file = new File(path.get(position));
                listItems.add(String.valueOf(file));     
                customAdapter.notifyDataSetChanged();
            }
        });
        return view;
    }
    public void  playSong(int songIndex){
        // Play song
        Toast.makeText(Main.this,"Hellooo",Toast.LENGTH_LONG).show();
        try {
            mp.reset();
            mp.setDataSource(songsList.get(songIndex).get("songPath"));
            mp.prepare();
            mp.start();         

            img2.setImageResource(R.drawable.pausebutton);

            // set Progress bar values
            songProgressBar.setProgress(0);
            songProgressBar.setMax(100);

            // Updating progress bar
            updateProgressBar();

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

    public void updateProgressBar() {
        mHandler.postDelayed(mUpdateTimeTask, 100);
    }
    public Runnable mUpdateTimeTask = new Runnable() {
        public void run() {
            long totalDuration = mp.getDuration();
            long currentDuration = mp.getCurrentPosition();

            // Displaying Total Duration time

            songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));
            // Displaying time completed playing
            songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration));

            // Updating progress bar
            int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
            //Log.d("Progress", ""+progress);
            songProgressBar.setProgress(progress);
            songProgressBar.setProgress(progress(.get(j));

            // Running this thread after 100 milliseconds
            mHandler.postDelayed(this, 100);
        }
    };
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
    }    
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // remove message Handler from updating progress bar
        mHandler.removeCallbacks(mUpdateTimeTask);
    }
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        mHandler.removeCallbacks(mUpdateTimeTask);
        int totalDuration = mp.getDuration();
        int currentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration);

        // forward or backward to certain seconds
        mp.seekTo(currentPosition);

        // update timer progress again
        updateProgressBar();
    }
}

0 个答案:

没有答案