简体   繁体   English

在Listview行中使用按钮

[英]Using buttons in Listview row android

In my android application, I am using Listview and each row of listview will have textview and play/pause button. 在我的Android应用程序中,我正在使用Listview并且listview的每一行都将具有textview和play / pause按钮。 Here I am displaying audio file in each list row.When user click play button, audio file will start and the icon of play button will be changed to pause .If i click same button again, it will stop that audio file and icon of pause button will be to play. 这里我在每个列表行中显示音频文件。当用户单击“播放”按钮时,音频文件将启动,并且“播放”按钮的图标将更改为暂停。如果再次单击同一按钮,它将停止该音频文件和“暂停”图标按钮将被播放。

The problem is that if i am accessing the play button of 1st row and i click on play button of another row, then icon of 1st row should change.I don't know how to achieve this functionality. 问题是,如果我正在访问第一行的播放按钮,然后单击另一行的播放按钮,则第一行的图标应该更改。我不知道如何实现此功能。 Please help me to solve this problem. 请帮我解决这个问题。

getView() method of customAdapter : customAdapter的getView()方法:

  public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        RowItem_ringtone rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.ringtone_row, null);
            holder = new ViewHolder();

            holder.txtTitle = (TextView) convertView
                    .findViewById(R.id.ringtoneTitle);
            holder.btnPlay = (ImageButton) convertView
                    .findViewById(R.id.btnPlay);
            holder.btnSet = (ImageButton) convertView.findViewById(R.id.btnSet);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.txtTitle.setText(rowItem.getRingTitle());
        holder.btnPlay.setTag(rowItem.getRingId());
        holder.btnSet.setTag(rowItem.getRingId());

        holder.btnPlay.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Toast.makeText(getContext(), v.getTag().toString(),
                        Toast.LENGTH_LONG).show();
                if (CustomListViewAdapter_ringtone.mp != null) {
                    if (CustomListViewAdapter_ringtone.mp.isPlaying()) {
                        CustomListViewAdapter_ringtone.mp.stop();
                        CustomListViewAdapter_ringtone.mp.release();
                    }
                }
                mp = MediaPlayer.create(getContext(),
                        Integer.parseInt(v.getTag().toString()));
                mp.start();
                v.setBackgroundResource(R.drawable.set_icon);
                btnId = Integer.parseInt(v.getTag().toString());
            }
        });
return convertView;
    }

Just store a reference to the last button that was clicked in your OnListItemClick method. 只需存储对OnListItemClick方法中单击的最后一个按钮的引用。

public class Something {
    private CustomListItem previouslyClickedListItem;

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {
        CustomListItem item = (CustomListItem) view;
        if (item != previouslyClickedListItem) {
            // Set previouslyClickedListItem to not clicked
            prevouslyClickedListItem = item;
            // Set item to clicked
        } else {
            // Set prevouslyClickListItem to not clicked
        }
    }
}

On List view item click listener you are changing icon right? 在“列表”视图项上,单击要更改的监听器,对吗? so there you get id for row also . 所以你也得到了行的ID。 So you can store that id in variable as currently playing row or file. 因此,您可以将该ID存储在当前正在播放的行或文件中的变量中。 And when you click on another row just check that id and change icon according to that. 当您单击另一行时,只需检查该ID并根据其更改图标即可。

Use like this 这样使用

yourlistview.setOnItemClickListener(new OnItemClickListener()
{
    @Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long         arg3)
{ 
    Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});

This sample code works for me: 此示例代码对我有用:

View view = null;

 private OnItemClickListener onItemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            if(view != null)
            {
                TextView count = (TextView) view.findViewById(R.id.downloadCount);
                count.setText("007");
            }
            view = arg1;

        }
    };

You must be using the custom adapter of listview and your code must be inside the getView method. 您必须使用listview的自定义适配器,并且您的代码必须在getView方法内。

In this method, after the findviewbyid, just assign tags to the buttons. 在这种方法中,在findviewbyid之后,只需将标签分配给按钮即可。

eg, btn.setTag(position); 例如btn.setTag(position);

and then when you click the play button, just get that tag. 然后,当您点击播放按钮时,只需获取该标签即可。 eg, int clickedposition = (Intger)btn.getTag(); 例如,int clickedposition =(Intger)btn.getTag();

You will now be able to change the icon of that particular row only. 现在,您将只能更改该特定行的图标。

Below is The Code Snippet for your salvation. 以下是您的救助代码段。 I used toggle button for eg you can change accordingly 我使用了切换按钮,例如您可以相应地进行更改

public class CustomListviewAdapter extends BaseAdapter {

  private View PreviousView=null;

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

  togglebutton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

        View CurrentView = v;
          if(PreviousView != null && PreviousView != CurrentView){
             ToggleButton tb=(ToggleButton)PreviousView.findViewById(R.id.togglebutton);
             tb.setChecked(false);
            }

          PreviousView=CurrentView;
         }
    });
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM