setVisibility调整自定义Listview中的错误按钮

时间:2017-08-25 21:04:44

标签: android listview onclick android-custom-view android-button

我有一个自定义列表视图,其中包含文本和播放暂停按钮。当点击播放按钮时,我试图使播放按钮不可见。但是当我在onClick中执行此操作时,错误位置的按钮正在listview中消失。喜欢;按钮:A - B - C - D - E,当我点击C时,E变为不可见。我不知道如何处理,请帮助。

public class MyListAdapter extends ArrayAdapter<String> {

private String[] gelenurl;
List<String> gelen_ad;
MediaPlayer mPlayer= new MediaPlayer();

private int[] IMAGE_ID = {R.drawable.police};
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects, String[] arr) {
    super(context, resource, objects);
    layout=resource;
    gelenurl=arr;
    gelen_ad=objects;
}


ViewHolder viewHolder = new ViewHolder();
@NonNull
@Override

public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder mainViewholder = null;
    convertView=null;

    if(convertView==null) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView=inflater.inflate(layout, parent, false);

        viewHolder.img = (ImageView) convertView.findViewById(R.id.list_item_thumbnail);
        viewHolder.img.setImageResource(IMAGE_ID[0]);
        viewHolder.title = (TextView) convertView.findViewById(R.id.list_item_text);
        viewHolder.title.setText(getItem(position));
        viewHolder.button = (Button) convertView.findViewById(R.id.list_item_btn);
        viewHolder.stop_button = (Button) convertView.findViewById(R.id.list_item_stop_btn);

        convertView.setTag(viewHolder);

    }

    else {

        mainViewholder = (ViewHolder) convertView.getTag();
        mainViewholder.title.setText(getItem(position));

    }

    viewHolder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {

                viewHolder.button.setVisibility(View.INVISIBLE);
                radioLinks(gelenurl[position]);


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


        public void radioLinks(String city) throws IOException {

            if(mPlayer.isPlaying()) {

                mPlayer.stop();
                mPlayer.reset();


            }

            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mPlayer.setDataSource(city);
            mPlayer.prepareAsync();


            mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {

                    mPlayer.start();

                }
            });

        }

    });

    viewHolder.stop_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mPlayer.stop();
            mPlayer.reset();
        }
    });
    return convertView;
}

}

2 个答案:

答案 0 :(得分:0)

viewHolder是一个类成员变量。 (ViewHolder viewHolder = new ViewHolder()),因此,每次您输入getView()convertViewnull时,您都会设置相同的viewHolder。因此,getView()中最近设置的值是onClick()中的操作。您可能希望在convertView为null时创建新的视图持有者,而不是使用相同的成员变量。

看看this tutorial。查看班级getView()中的PersonsAdapter。每次创建新视图时都会创建一个新的视图持有者。您需要将onClickListener设置为此新创建的视图中的按钮。

这是您重做的getView()。这是未经测试的,但应该给你正确的想法。祝你好运!

    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder mainViewholder;

//        convertView = null; you don't need this if you reuse the view properly
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(layout, parent, false);
            mainViewholder = new ViewHolder();
            convertView.setTag(mainViewholder);
        } else {
            // If you are reusing a view, you may need to reset values in its view holder.
            mainViewholder = (ViewHolder) convertView.getTag();
        }

        // You always need to set you viewholder member variables whether creating a new
        // view or converting an old view.
        mainViewholder.img = (ImageView) convertView.findViewById(R.id.list_item_thumbnail);
        mainViewholder.img.setImageResource(IMAGE_ID[0]);
        mainViewholder.title = (TextView) convertView.findViewById(R.id.list_item_text);
        mainViewholder.title.setText(getItem(position));
        mainViewholder.button = (Button) convertView.findViewById(R.id.list_item_btn);
        mainViewholder.stop_button = (Button) convertView.findViewById(R.id.list_item_stop_btn);

        final ViewHolder vh = mainViewholder; // final to use in the click listener
        mainViewholder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    vh.button.setVisibility(View.INVISIBLE);
                    radioLinks(gelenurl[position]);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

答案 1 :(得分:0)

您每次都会获得最后一个ID或最后一行位置,因为最后一个视图最后呈现

要在按钮上单击行位置,可以将位置设置为按钮的标签,并在onclickListner中检查标签

例如在你的

getview()

方法将位置作为标记放在viewholder.btn

viewHolder.btn.setTag(position);

和OnClickListner

检查标记

int position = (int)v.getTag();

供参考see this post