我是新来的机器人,甚至编程它只有3周 我想通过显示图片和播放为我的德语课建立一个应用程序音频。 现在我的问题,我有一个列表视图,其中显示一个小的图片和一个字,然后我做了一个onItemClickListener这个列表视图,它显示项目形式和arraylist类型字(一个类的构造和获取方法对于图像资源和音频资源和单词它自己的字符串)如何迭代通过自定义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
}
});
}
});.
}
}
你可以关闭当前的弹出窗口,并使用来自该窗口的位置参数为下一个/上一个项目创建一个新的弹出窗口onItemClickListener。 –
请发表您试用过的内容,因为它听起来像是获得(位置+ 1)和设置新图像的简单电话应该可以工作。 – lionscribe
@lionscribe我试过这个词word1 = lessone1Words.get(position + 1); imageBig.setImageResource(word1.getImageResourceId()); //但它只用于下一个图像,但它不会继续 –