0

我需要使用 ListView 制作一个 Activity,它可能有 50 多个 ImageButtons,每个 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>

这是列表视图每一行的自定义布局:

<?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>

每个按钮将通过 onClick "play_btn" 播放不同的声音,并根据带有字符串 "list_item_string" 的声音播放文本。

例子:


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


4

2 回答 2

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();
    }
}

这应该就是你所需要的!

于 2015-01-15T23:33:01.897 回答
0

解决方案是创建一个自定义ListView对象,该对象具有TextView用于显示按钮名称的 a 和ImageButton. ListView然后,您可以使用自定义将这些对象添加到您的Adapter. 另一种方法ImageButton是简单地使用常规图像并制作它Clickable。可以在此处找到ListView创建可以显示自定义对象(包括如何创建自定义对象)的一个很好的示例: http ://www.androidinterview.com/android-custom-listview-with-image-and-text-using-阵列适配器/Adapter

于 2015-01-15T23:24:17.257 回答