在列表视图中更新播放,暂停按钮

时间:2013-06-04 06:10:44

标签: android-listview

我有一个列表视图,其中包含图像,标题,选项和播放/暂停按钮。当我单击一行中的播放按钮然后单击另一行中的播放按钮时,两个按钮的背景将保持暂停状态。所以我希望当我单击连续播放按钮然后单击另一行中的播放按钮时,第一个按钮的背景应更改为播放,第二个按钮的背景应保持暂停。提前致谢

enter image description here

这是代码

公共类MyAdapter扩展了BaseAdapter {

    int itemnum; 
    Boolean playing = false;
    Context context; 
    int layoutResourceId;    

    // RingTone data[] = null;
    private LayoutInflater mInflater;

    String[] Title;
    int[] Img;

    ListView listview;






    public MyAdapter(Context context, int layoutResourceId, String[] Title,int[] Img, int[] Tone, ListView listview) {
        mInflater = LayoutInflater.from(context);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.Title=Title;
        this.Img=Img;
        ListActivity.Tone=Tone;
        this.listview=listview;



    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        RingToneHolder holder = null;


        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listrow, null);


        }


        holder = new RingToneHolder();
        // RingTone aa = data[position];
        holder.imgIcon = (ImageView)convertView.findViewById(R.id.list_image);
        holder.txtTitle = (TextView)convertView.findViewById(R.id.title);
        holder.playbtn = (Button)convertView.findViewById(R.id.playbtn);
        holder.btn_alert_dialog = (Button)convertView.findViewById(R.id.alert_dialog_btn);
        holder.pausebtn = (Button)convertView.findViewById(R.id.pausebtn);


        holder.txtTitle.setText(Title[position]);
        // holder.txtTitle.setText(aa.title);
        holder.imgIcon.setImageResource(Img[position]);
        //holder.playbtn.setInputType(aa.btn);S
        holder.playbtn.setTag(position);
        holder.btn_alert_dialog.setTag(position);



        holder.playbtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // TODO Auto-generated method stub
                if(mp == null){

                    listview.invalidateViews();
                    mp = MediaPlayer.create(context,Tone[Integer.parseInt(arg0.getTag().toString())]);

                    mp.start();
                    playing = true;
                    itemnum = Integer.parseInt(arg0.getTag().toString());
                    //Toast.makeText(context, "ist playing", Toast.LENGTH_LONG).show();
                    arg0.setBackgroundResource(R.drawable.pause);

                }
                else {
                    if(mp.isPlaying() && itemnum == Integer.parseInt(arg0.getTag().toString())){
                        listview.invalidateViews();
                        mp.pause();
                        playing = false;
                        //Toast.makeText(context, " paused", Toast.LENGTH_LONG).show();
                        arg0.setBackgroundResource(R.drawable.play_btn);
                    }
                    else{
                        if(playing == false  && itemnum == Integer.parseInt(arg0.getTag().toString())){
                            listview.invalidateViews();
                            mp.start();
                            playing = true;
                            arg0.setBackgroundResource(R.drawable.pause);
                        }
                        else {
                            //.invalidate();
                            listview.invalidateViews();
                            mp.stop();
                            mp.release();
                            mp = MediaPlayer.create(context,Tone[Integer.parseInt(arg0.getTag().toString())]);
                            mp.start();
                            playing = true;
                            itemnum = Integer.parseInt(arg0.getTag().toString());
                            //Toast.makeText(context, "playing", Toast.LENGTH_LONG).show();
                            arg0.setBackgroundResource(R.drawable.pause);
                        }
                    }
                }

            }


        });

1 个答案:

答案 0 :(得分:1)

您只是更改所点击行的drawable。

将指针/索引保留到当前正在播放的行,以便下次触发onClick()时,您知道之前正在播放哪一行。所以去改变那一行的可绘制性。