为什么只在单击图像时才播放音频,而在单击textVIew时才播放音频?

时间:2018-10-20 18:52:59

标签: java android android-studio listview android-mediaplayer

我正在关注一些在线教程,它是关于如何使用onItemCLickLIstener在listview中播放音频的,但是问题是我已经完成了确切的工作,但无法正常工作。 当我单击图像时,音频会播放,但在屏幕的其余部分上,音频却无能为力。

     public class NmbersActivity extends AppCompatActivity{

private MediaPlayer mediaPlayer;

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


    final ArrayList<Word> word = new ArrayList<Word>();

    word.add(new Word("One", "Lutti",R.drawable.number_one,R.raw.number_one));
    word.add(new Word("Two", "Otlika",R.drawable.number_two,R.raw.number_two));
    word.add(new Word("Three", "Tolookosu",R.drawable.number_three,R.raw.number_three));
    word.add(new Word("Four", "Oyyisa",R.drawable.number_four,R.raw.number_four));
    word.add(new Word("Five", "Massokka",R.drawable.number_five,R.raw.number_five));
    word.add(new Word("Six", "Temmoka",R.drawable.number_six,R.raw.number_six));
    word.add(new Word("Seven", "Kenekaku",R.drawable.number_seven,R.raw.number_seven));
    word.add(new Word("Eight", "Kawinta",R.drawable.number_eight,R.raw.number_eight));
    word.add(new Word("Nine", "Wo'e",R.drawable.number_nine,R.raw.number_nine));
    word.add(new Word("Ten", "Na'aacha",R.drawable.number_ten,R.raw.number_ten));



    WordAdapter Adapter = new WordAdapter(this,word,R.color.category_number);

    ListView activityText = findViewById(R.id.commanIntent);

    activityText.setAdapter(Adapter);

    activityText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Word words = word.get(position);

            mediaPlayer = MediaPlayer.create(NmbersActivity.this,words.getmAudioResource());

            mediaPlayer.start();

        }
    });

}

}

这是LIstView文件

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />

这是单个列表项的自定义布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/tan_background"
android:minHeight="@dimen/list_item_height"
android:orientation="horizontal">

<ImageView
    android:id="@+id/image"
    android:layout_width="@dimen/list_item_height"
    android:layout_height="@dimen/list_item_height" />

<LinearLayout
    android:id="@+id/text_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="16dp">

    <TextView
        android:id="@+id/miwok_text_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="bottom"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        tools:text="lutti" />

    <TextView
        android:id="@+id/default_text_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        tools:text="one" />
</LinearLayout>

这是wordAdapter文件。

public class WordAdapter extends ArrayAdapter<Word> {

private int mColorPrimary;

public WordAdapter(Context ActivityName, ArrayList<Word> word, int 
background) {
    super(ActivityName,0,word);
    mColorPrimary = background;
 }
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {


    View listItemView = convertView;
    if (listItemView == null)

    {
        listItemView = LayoutInflater.from(getContext()).inflate
                (R.layout.list_item, parent, false);
    }

    Word defaultView = getItem(position);

    final TextView miwokTextView = 
listItemView.findViewById(R.id.mivokTrans);

    miwokTextView.setText(defaultView.getMovik());


    final TextView defaultTextView = 
listItemView.findViewById(R.id.defaultTrans);

    defaultTextView.setText(defaultView.getDefault());

    ImageView imageView = listItemView.findViewById(R.id.image);
   if(defaultView.hasImage()) {
       imageView.setImageResource(defaultView.getmImageResourseId());
   }

   else{
       imageView.setVisibility(View.GONE);
   }

   View colorView = listItemView.findViewById(R.id.linear_item);

   colorView.setBackgroundResource(mColorPrimary);

    return listItemView;

 }

这是Word类。

public class Word{

    private String mDeafaultLanguage;

    private String mMovikLanguage;

    private int mImageResourseId = NO_IMAGE;

    public static final int NO_IMAGE = -1;



    public Word(String defaultLanguage,String movikLanguage){

        mDeafaultLanguage = defaultLanguage;
        mMovikLanguage = movikLanguage;

    }

    public Word(String defaultLanguage,String movikLanguage, int 
 imageResource){

    mDeafaultLanguage = defaultLanguage;
    mMovikLanguage = movikLanguage;
    mImageResourseId = imageResource;

    }

    public String getDefault(){
        return mDeafaultLanguage;
    }

    public String getMovik() {
        return mMovikLanguage;
    }

public int getmImageResourseId() {
    return mImageResourseId;
}

public boolean hasImage(){
        return NO_IMAGE != mImageResourseId;
}

}

如果您想了解更多详细信息,请访问确切的应用程序链接  https://github.com/udacity/ud839_Miwok/tree/43a1596a48521beee160f937988a5c564519dbb5

1 个答案:

答案 0 :(得分:0)

请尝试为textview设置clickable true。

android:clickable="true"