滚动时重置图标

时间:2015-03-29 20:17:42

标签: listview scroll

我有以下适配器:

package studios.verso.instrumentalringtones;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MyBaseAdapter extends BaseAdapter {

    ArrayList<ListData> myList = new ArrayList<ListData>();
    LayoutInflater inflater;
    Context context;
    static boolean playing = false;
    static int pause_button_position = -1;



    public MyBaseAdapter(Context context, ArrayList<ListData> myList) {
        this.myList = myList;
        this.context = context;
        inflater = LayoutInflater.from(this.context);    // only context can also be used
    }

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

    @Override
    public ListData getItem(int position) {
        return myList.get(position);
    }

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

    MediaPlayer mp = new MediaPlayer();
    @Override

    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder mViewHolder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.listview_item_row, null);
            mViewHolder = new MyViewHolder();
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (MyViewHolder) convertView.getTag();
        }


        mViewHolder.tvTitle = detail(convertView, R.id.txtTitle, myList.get(position).getTitle());
        mViewHolder.tvDesc = detail(convertView, R.id.textView1, myList.get(position).getDescription());
        //mViewHolder.ivIcon = detail(convertView, R.id.imgIcon, myList.get(position).getImgResId());

        TextView tv = (TextView) convertView.findViewById(R.id.txtTitle);
        tv.setText("Testtt" + position);
        final int pos = position;


        final ImageView iv = (ImageView) convertView.findViewById(R.id.imgIcon);
        iv.setTag(position); //attach the position to your view
        iv.setImageResource(R.drawable.play_ic);



        iv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                    try {
                        if (playing) {
                            Toast.makeText(context,
                                    "mMediaPlayer.isPlaying()",
                                    Toast.LENGTH_LONG).show();

                            try {
                                mp.stop();
                                mp.release();
                                playing = false;

                                iv.setImageResource(R.drawable.play_ic);
                                notifyDataSetChanged();
                            } catch (Exception e) {
                                // TODO: handle exception
                                Toast.makeText(context, e + "",
                                        Toast.LENGTH_LONG).show();
                            }

                        } else {

                            iv.setImageResource(R.drawable.pause_ic);
                            pause_button_position = pos; //add
                            playing = true;

                            mp = MediaPlayer.create(context, R.raw.accordion_golden1);
                            //mp.prepare();
                            mp.start();
                            pause_button_position = pos;
                            Toast.makeText(context,
                                    "mMediaPlayer.start " + playing, Toast.LENGTH_LONG)
                                    .show();
                            notifyDataSetChanged();

                        }
                    } catch (Exception e) {
                        Toast.makeText(context, e + "" + ".....",
                                Toast.LENGTH_LONG).show();
                    }
                }
        });

        return convertView;
    }

    // or you can try better way
    private TextView detail(View v, int resId, String text) {
        TextView tv = (TextView) v.findViewById(resId);
        tv.setText(text);
        return tv;
    }

    private ImageView detail(View v, int resId, int icon) {
        ImageView iv = (ImageView) v.findViewById(R.id.imgIcon);
        iv.setImageResource(icon); //

        return iv;
    }

    private class MyViewHolder {
        TextView tvTitle, tvDesc;
        ImageView ivIcon;
    }

}

例如,如果我点击播放img,声音将播放正常,再次点击img将停止,图标正在改变。

如果我点击第一行的播放图标,然后向上或向下滚动,播放ico将从播放重置为暂停,并且音乐仍在正常播放。

我可以做什么来取消滚动更新?

1 个答案:

答案 0 :(得分:0)

所以我找到了答案! 对于初学者来说很难理解一些简单的事情! 我这样做的方式是: 我创建了数组,在这个数组中我添加了每次点击的位置。 所以在创建行之后,在viewholder中evend我检查是否在数组中添加了行。