1
這是在whatsapp中實現的,如何將視圖(對於音頻)動態添加到視圖分頁器中,如下所示。 我已經實現了圖像的工作示例,但如何做到這一點,當圖像和音頻文件都在那裏? 任何代碼或工作示例將不勝感激。 預先感謝您。如何實現像下面的屏幕截圖一樣的viewpager?
public class download_data_adapter extends PagerAdapter {
private Context context;
private List<Bitmap> data;
private List<String> path;
ImageButton btnplaypause;
private SeekBar seekBarProgress;
Button btnCancel;
private MediaPlayer mediaPlayer;
private PopupWindow pwindo;
private int mediaFileLengthInMilliseconds;
private final Handler handler = new Handler();
public download_data_adapter(Context c, List<Bitmap> b,List<String> s) {
context = c;
data = b;
path = s;
}
@Override
public int getCount() {
if (data.size() <= 0)
return 1;
return data.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(final ViewGroup container, int position) {
final TouchImageView imageView = new TouchImageView(context);
int padding = 5;
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageBitmap(data.get(position));
imageView.setTag(path.get(position));
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (imageView.getTag().toString().endsWith(".mp3")) {
initiatePopupWindow(imageView.getTag().toString());
}
}
private void initiatePopupWindow(final String tag) {
try {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.audio_play_layout,(ViewGroup) container. findViewById(R.id.rlAudioLayout));
pwindo = new PopupWindow(layout,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);
pwindo.showAtLocation(layout, Gravity.CENTER,0, 0);
btnCancel = (Button) layout.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mediaPlayer.pause();
pwindo.dismiss();
}
});
seekBarProgress = (SeekBar) layout.findViewById(R.id.SeekBarTestPlay);
seekBarProgress.setMax(99);
seekBarProgress.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v,
MotionEvent event) {
if (mediaPlayer.isPlaying()) {
SeekBar sb = (SeekBar) v;
int playPositionInMillisecconds = (mediaFileLengthInMilliseconds/100)
* sb.getProgress();
mediaPlayer
.seekTo(playPositionInMillisecconds);
}
return false;
}
});
mediaPlayer = new MediaPlayer();
mediaPlayer .setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(
MediaPlayer arg0,
int percent) {
seekBarProgress.setSecondaryProgress(percent);
}
});
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(
MediaPlayer arg0) {
btnplaypause
.setImageResource(R.drawable.button_play);
}
});
btnplaypause = (ImageButton) layout.findViewById(R.id.btnPlayPause);
btnplaypause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mediaPlayer
.setDataSource(tag);
mediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mediaFileLengthInMilliseconds = mediaPlayer
.getDuration();
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
btnplaypause
.setImageResource(R.drawable.button_pause);
} else {
mediaPlayer.pause();
btnplaypause.setImageResource(R.drawable.button_play);
}
primarySeekBarProgressUpdater();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private void primarySeekBarProgressUpdater() {
seekBarProgress.setProgress((int) (((float) mediaPlayer
.getCurrentPosition()/mediaFileLengthInMilliseconds) * 100));
if (mediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
primarySeekBarProgressUpdater();
}
};
handler.postDelayed(notification, 1000);
}
}
});
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
活動放在這裏: 在OnCreate中我打電話異步任務從服務器獲取圖像/記錄的絕對路徑,並將這些列表添加到列表中,您將看到「位圖」和「bi」 tmapUrl」上後執行IM添加以下代碼
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
adapter = new download_data_adapter(context,bitmaps,bitmapUrl);
viewPager.setAdapter(adapter);
註釋不是延長討論;這個談話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/74349/discussion-on-answer-by-karthika-pb-how-to-implement-a-viewpager-like-below- SCRE)。 – Taryn 2015-04-02 13:54:08