如何创建按钮的ListView,在单击时播放不同的声音

时间:2015-01-15 23:12:50

标签: android android-listview textview android-button android-imagebutton

我需要使用ListView创建一个Activity,它可以有超过50个ImageButton,每个ImageButtons播放不同的声音。

这是主要的活动(可以有按钮):



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"

    tools:context="com.werewolve.freaksound.sounds"
    android:background="@drawable/f_background_fit">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:src="@drawable/f_logo" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/soundList"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp" />
</RelativeLayout>
&#13;
&#13;
&#13;

这是我对listview的每一行的自定义布局:

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/list_item_string"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:textSize="18sp"
        android:textStyle="bold" />

    <ImageButton
        android:id="@+id/play_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="#00000000"
        android:src="@drawable/f_button_s"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp" />

</RelativeLayout>
&#13;
&#13;
&#13;

每个按钮将与onClick&#34; play_btn&#34;播放不同的声音。和一个根据声音的文字,字符串&#34; list_item_string&#34;。

示例:


*(笑声)* (播放按钮)***


2 个答案:

答案 0 :(得分:2)

你应该像这样创建一个自定义适配器:

public class PlaySoundsAdapter extends BaseAdapter implements View.OnClickListener {

    SoundExample[] sounds;
    Activity context;
    PlaySoundAlert soundPlayerAlert;

    public PlaySoundsAdapter(Activity context, SoundExample[] soundsArray) {
        this.context = context;
        this.sounds = soundsArray;

        // Hooks up the PlaySoundAlert.PlaySound in MainActivity
        this.soundPlayerAlert = (PlaySoundAlert)context;
    }

    @Override
    public int getCount() {
        return sounds == null ? 0 : sounds.length;
    }

    @Override
    public Object getItem(int i) {
        return sounds[i];
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        SoundExample item = (SoundExample)getItem(i);

        if (view == null) // reuse existing view
            view = context.getLayoutInflater().inflate(R.layout.custom_sound_layout,
                    viewGroup, false);

        // Set the TextView to the name of the sound
        TextView t = (TextView)view.findViewById(R.id.txtName);
        t.setText(item.getSoundName());

        // Set the tag of the button to the sound resource id (uri)
        Button b = (Button)view.findViewById(R.id.play_btn);
        b.setTag(item.getSoundUri());

        // When the button is clicked, play the associated sound
        b.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View view) {
        Button b = (Button) view;
        if (b != null) {
            int soundUri = (int)b.getTag();

            // Notify listener (MainActivity) to play the required sound
            if (soundPlayerAlert != null) {
                soundPlayerAlert.playSound(soundUri);
            }
        }
    }
}

接下来,创建一个可在活动中实现的界面,播放如下声音:

public interface PlaySoundAlert {
    public void playSound(int uri);
}

正如您所看到的,我在上面创建的Adapter使用此界面来触发事件以播放所需的声音。

您的SoundExample课可能会喜欢这样的内容:

public class SoundExample {

    private int soundUri;
    private String soundName;

    public String getSoundName() {
        return soundName;
    }

    public void setSoundName(String soundName) {
        this.soundName = soundName;
    }

    public int getSoundUri() {
        return soundUri;
    }

    public void setSoundUri(int soundUri) {
        this.soundUri = soundUri;
    }
}

要在ActivityFragment中使用此功能,请使用以下内容:

public class MainActivity extends Activity implements PlaySoundAlert {

    ListView lstSounds;
    PlaySoundsAdapter soundsAdapter;
    SoundExample[] mySounds;

    // Media player for playing sounds
    MediaPlayer mp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lstSounds = (ListView) findViewById(R.id.soundList);

        // Create sound list & add two SoundExample objects
        mySounds = new SoundExample[2];
        SoundExample s1 = new SoundExample();
        SoundExample s2 = new SoundExample();

        // Set sound one to a beep
        s1.setSoundName("Beep Sound");
        s1.setSoundUri(R.raw.beep);

        // Set sound two to an applause sound
        s2.setSoundName("Applause Sound");
        // NOTE: I am using a sound titled applause.mp3 inside a folder called "raw"
        s2.setSoundUri(R.raw.applause);

        // Add sounds to the list
        mySounds[0] = s1;
        mySounds[1] = s2;

        // Instantiate the adapter and apply to the ListView
        soundsAdapter = new PlaySoundsAdapter(this, mySounds);
        lstSounds.setAdapter(soundsAdapter);
    }

    @Override
    public void playSound(int uri) {
        // Play sound
        mp = MediaPlayer.create(this, uri);
        if (!mp.isPlaying())
            mp.start();
    }
}

这应该就是你所需要的!

答案 1 :(得分:0)

解决方案是创建一个自定义ListView对象,其中TextView用于显示按钮的名称和ImageButton。然后,您可以使用自定义ListView将这些对象添加到AdapterImageButton的替代方法是简单地使用常规图像并将其设为Clickable。可以在此处找到创建可显示自定义对象的ListView的一个很好的示例(包括如何创建自定义Adapter):http://www.androidinterview.com/android-custom-listview-with-image-and-text-using-arrayadapter/