我是Android新手,甚至编程只用了3周 我正在尝试通过显示图片和播放音频来为我的德语课程构建应用程序。 我的问题现在,我有一个列表视图,显示一个小图片和一个单词,然后我为这个列表视图创建了一个onItemClickListener,它显示项目形式和 Word 类型的arraylist(一个类为构造函数和获取图像资源和音频资源的方法以及单词的字符串(单词)
无论如何,当项目点击项目列表时我管理它打开弹出布局,显示图像和播放按钮。
然后我想添加next和prev按钮来获取下一个图像和音频而不是关闭弹出窗口并再次单击想要的项目但是没有运气。
我会尝试发布最重要的代码
public class Lesson1Words extends AppCompatActivity {
private MediaPlayer mMediaPlayer;
private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
// Now that the sound file has finished playing, release the media player resources.
releaseMediaPlayer();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ArrayList<Word> lessone1Words = new ArrayList<>();
lessone1Words.add(new Word("vernünftig", R.drawable.vernunftig, R.raw.l01_vernunftig));
lessone1Words.add(new Word("großzügig", R.drawable.groszugig, R.raw.l01_grosszugig));
lessone1Words.add(new Word("fair", R.drawable.fair, R.raw.l01_fair));
lessone1Words.add(new Word("intelligent/klug", R.drawable.intelligent, R.raw.l01_intelligent));
lessone1Words.add(new Word("frech", R.drawable.frech, R.raw.l01_frech));
lessone1Words.add(new Word("kreativ", R.drawable.kreaiv, R.raw.l01_kreativ));
lessone1Words.add(new Word("sparsam", R.drawable.sparsam, R.raw.l01_sparsam));
lessone1Words.add(new Word("realistisch", R.drawable.realistisch, R.raw.l01_realistisch));
lessone1Words.add(new Word("hübsch", R.drawable.hubsch, R.raw.l01_hubsch));
lessone1Words.add(new Word("aufmerksam", R.drawable.aufmerksam, R.raw.l01_aufmerksam));
final WordAdapter adapter = new WordAdapter(this, lessone1Words, R.color.lesson_color);
final ListView listView = (ListView) findViewById(R.id.word_list);
assert listView != null;
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, final View view, final int position, long l) {
final Word word = lessone1Words.get(position);
//open Popup layout for the image but still can't iterate through the arraylist
LayoutInflater layoutInflater
= (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
AppBarLayout.LayoutParams.WRAP_CONTENT,
AppBarLayout.LayoutParams.WRAP_CONTENT);
// when touch outside the Popup layout it dismiss it
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
//change image in ImageView of Popup.xml to current image in the array
final ImageView imageBig = (ImageView) popupView.findViewById(R.id.popimage);
imageBig.setImageResource(word.getImageResourceId());
ImageView playSound = (ImageView) popupView.findViewById(R.id.play_image);
playSound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//responsible for the playback of the audio according to the arraylist index
releaseMediaPlayer();
mMediaPlayer = MediaPlayer.create(Lesson1Words.this, word.getAudioResourceId());
mMediaPlayer.setVolume(0.5f, 0.5f);
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(mCompletionListener);
}
});
popupWindow.showAtLocation(listView, Gravity.CENTER, 0, 0);
ImageView nextImage = (ImageView) popupView.findViewById(R.id.next_image);
nextImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// cant go for the nextimage or audio
}
});
}
});.
}
}
答案 0 :(得分:0)
创建弹出窗口时,设置位置值为
的标签imageBig.setTag(Integer.valueOf(position));
在onClick函数调用中
int pos = imageBig.getTag()+1;
imageBig.setTag(Integer.valueOf(pos));
WordNext = getPosition(position);
...