Gallery feature:
I am developing a feature similar to Gallery, where users can swipe to see image or video uploaded by them.
Problem :
Whenever there is video after imageview, Mediacontrols for videoview is getting displayed on imageview for about 2 seconds
Code :
item_pager.xml :
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/black_overlay"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/images_pager"
android:layout_width="match_parent"
android:layout_height="250dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:visibility="visible"
app:srcCompat="@drawable/cctv_icon" />
<VideoView
android:id="@+id/videoview"
android:layout_width="match_parent"
android:layout_height="250dp"
android:adjustViewBounds="true"
android:visibility="gone" />
</LinearLayout>
</layout>
PagerAdapter code :
public class ItemViewpager extends PagerAdapter implements MediaPlayer.OnPreparedListener,
View.OnClickListener, MediaPlayer.OnCompletionListener, View.OnFocusChangeListener {
private List<MediaInfo> mediaInfos;
private LayoutInflater layoutInflater;
private MediaController mediaController;
private HashMap<Integer, MediaController> mediaMap;
private VideoView videoView;
private ImageView imageView;
private int currPos = 0;
public ItemViewpager(List<MediaInfo> mediaInfos) {
this.mediaInfos = mediaInfos;
mediaMap = new HashMap<>(mediaInfos.size());
}
@Override
public int getCount() {
return mediaInfos.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == ((LinearLayout) object);
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
layoutInflater=
(LayoutInflater)container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = layoutInflater.inflate(R.layout.item_pager, container, false);
imageView = itemView.findViewById(R.id.images_pager);
videoView = itemView.findViewById(R.id.videoview);
videoView.setOnPreparedListener(this);
videoView.setOnCompletionListener(this);
videoView.setOnFocusChangeListener(this);
boolean video = mediaInfos.get(position).video;
String uri = mediaInfos.get(position).uri;
mediaController = new MediaController(container.getContext());
mediaMap.put(position, mediaController);
if (video) {
imageView.setVisibility(View.GONE);
videoView.setVisibility(View.VISIBLE);
videoView.setVideoURI(Uri.parse(uri));
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.requestFocus();
videoView.seekTo(1);
} else {
imageView.setVisibility(View.VISIBLE);
videoView.setVisibility(View.GONE);
Picasso.get().load(uri).fit().placeholder(R.mipmap.ic_launcher).into(imageView);
}
if (mediaMap.size() > 0)
mediaMap.get(position).hide();
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
}
public void onPageSelectionListener(int pos) {
currPos = pos;
boolean video = mediaInfos.get(currPos).video;
/*if (mediaMap.size() > 0) {
if (video)
mediaMap.get(currPos).show(0);
else
mediaMap.get(currPos).hide();
}*/
}
@Override
public void onClick(View view) {
}
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
boolean video = mediaInfos.get(currPos).video;
/*if (!video)
videoView.stopPlayback();
else
mediaMap.get(currPos).show();*/
}
@Override
public void onFocusChange(View view, boolean focus) {
}
}
Thanks in advance :
Finally my issue got resolved by answer given by Ronak Thakkar
The CustomViewpager used had tried to load only single page by various ways.
You can always see the source code in the class.