I'm developing an application in which I have to use viewpager and after all items in viewpager is finished I have to call an activity. I'm not able to get event listener for this. Here is what I have been refering too:
https://github.com/chiuki/android-swipe-image-viewer/blob/master/src/com/sqisland/android/swipe_image_viewer/MainActivity.java
Here is what I have done so far:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
OnPageChangeListener mListener = new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
Log.i("Doing something here", "On Scroll state changed");
}
};
viewPager.setOnPageChangeListener(mListener);
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] { R.drawable.libin1,
R.drawable.libin2 };
#Override
public int getCount() {
return mImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = MainActivity.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}
My question is how to get event listener if all the items in viewpager is finished.
private OnPageChangeListener mListener = new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
selectedIndex = arg0;
}
boolean callHappened;
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
if( mPageEnd && arg0 == selectedIndex && !callHappened)
{
Log.d(getClass().getName(), "Okay");
mPageEnd = false;//To avoid multiple calls.
callHappened = true;
}else
{
mPageEnd = false;
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
if(selectedIndex == adapter.getCount() - 1)
{
mPageEnd = true;
}
}
};
ViewPager.setOnPageChangeListener(mListener);
onPageScrolled or onPageSelected any of these you can use here and also check the selected page is equals to the number of items in the ViewPager.
these three callbacks work like this:
onPageScrollStateChanged is called with state ViewPager.SCROLL_STATE_DRAGGING
onPageScrolled will be called many times, its parameter positionOffset and positionOffsetPixels are keep increasing
onPageScrolled's parameter positionOffset more than 0.5, onPageScrollStateChanged is called with state SCROLL_STATE_SETTLING
onPageSelected is called with next page index as position
onPageScrolled will be called many times, its parameter positionOffset and positionOffsetPixels are keep increasing
onPageScrolled's parameter positionOffset is 1, onPageScrollStateChanged is called with state SCROLL_STATE_IDLE
The logic should not put in onPageSelected and onPageScrollStateChanged, because with them you only know state is changed. only in onPageScrolled you can get the direction. So my implementation is like this:
private int selectedPageIndex = -1;
private boolean exitWhenScrollNextPage = false;
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (exitWhenScrollNextPage && position == PAGE_COUNT - 1) {
exitWhenScrollNextPage = false; // avoid call more times
AndroidLog.error("-------- YEAH!");
}
}
#Override
public void onPageSelected(int position) {
selectedPageIndex = position;
}
#Override
public void onPageScrollStateChanged(int state) {
if (state == SCROLL_STATE_IDLE) {
exitWhenScrollNextPage = selectedPageIndex == PAGE_COUNT - 1;
}
}
Thanks Triode for great answer.
I used the code you posted for my project. In my case, I am finishing the activity if a user swipes to the right on last fragment. It worked fine, but it was still finishing the activity when you swipe to left instead of swiping right.
I made a change in your code and put adapter.getCount() - 1 in place of selectedIndex. In this case it will not finish the activity if user swipes to left.
if( mPageEnd && arg0 == adapter.getCount()-1 && !callHappened){
Log.d(getClass().getName(), "Okay");
mPageEnd = false;//To avoid multiple calls.
callHappened = true;
}else{
mPageEnd = false;
}
Suggested answers are complex than I thought.So I created a simple one.
I used OnPageChangeListener in ViewPager class. Here we have 3 methods.
void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
void onPageSelected(int position);
void onPageScrollStateChanged(int state);
It is important to know what are the states in onPageScrollStateChanged() and their sequence of execution.There are 3 states as SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING and SCROLL_STATE_SETTLING.
Here is the execution order.
Scroll_state_dragging --> Scroll_state_settling --> onPageSelected() --> Scroll_state_idle
The idea is keeping a flag inside onPageSelected() to record current page number. Then if user in the last page and swipe left scroll_state_dragging called and launch the next view.
private int pagePosition; // keep a class variable
private int[] layouts= new int[]{
R.layout.welcome_slide1,
R.layout.welcome_slide2,
R.layout.welcome_slide3};
ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
addBottomDots(position);
pagePosition = position;
}
#Override
public void onPageScrolled(int position, float positionOffset, int arg2) {
}
#Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_DRAGGING) {
if (pagePosition == layouts.length - 1) {
launchHomeScreen();
}
}
}
};
private void launchHomeScreen() {
startActivity(new Intent(IntroductionActivity.this, MainDockerActivity.class));
finish();
}
I have created a complete example here.
#Override
public void onPageScrolled(int pageScrolledOn, float positionOffset, int positionOffsetPixels) {
if(positionOffset == 0F){
// meaning you cannot scroll further in current scroll direction
int lastPage = viewPagerAdapter.getCount - 1;
if (pageScrolledOn == lastPage){
//scroll on last page has occured
scrolledOnLastPage = true;
}
}
}
When you are at the last page and you slide to finish the activity the state SCROLL_STATE_SETTLING is skipped and the pager goes directly to SCROLL_STATE_IDLE
private val viewPagerPageChangeListener = object : ViewPager.OnPageChangeListener{
var blnPageTriesToSettle = false
override fun onPageScrollStateChanged(state: Int) {
when(state){
SCROLL_STATE_IDLE ->{
if (!blnPageTriesToSettle){
println("You are at the last page and you slide to finish")
finish()
}
}
SCROLL_STATE_DRAGGING ->{ blnPageTriesToSettle = false }
SCROLL_STATE_SETTLING ->{ blnPageTriesToSettle = true }
}
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
}
override fun onPageSelected(position: Int) {
}
}
Related
I have an activity with a RecyclerView and a button that is disabled at first. The elements of the RV are selectable, and I want the button to be enabled only after an element was selected. I tried to set addOnItemTouchListener on the recyclerview, the problem is that it is also called when scrolling through the list.
Here is my Adapter:
public class CLusterListAdapter extends RecyclerView.Adapter<CLusterListAdapter.ClusterListViewHolder> {
private ArrayList<ClusterItem> mListOfClusters;
private OnItemClickListener mListener;
//index
int row_index = -1; //Default no row chosen
public interface OnItemClickListener{
//delete
void onDeleteCluster(int position);
//edit
void onEditCluster(int position);
//select
void onClick(View view, int position);
}
public void setOnItemClickListener(OnItemClickListener listener){mListener = listener;}
public static class ClusterListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView mClusterName;
public TextView mClusterMembers;
public ImageView mDeleteCluster;
public ImageView mEditCluster;
//This
private OnItemClickListener itemClickListener;
public void setItemClickListener(OnItemClickListener listener){
this.itemClickListener = listener;
}
public ClusterListViewHolder(#NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
mClusterName = itemView.findViewById(R.id.tv_clusters_name_cluster_list);
mClusterMembers = itemView.findViewById(R.id.tv_clusters_members_cluster_list);
mDeleteCluster = itemView.findViewById(R.id.iv_delete_cluster);
mEditCluster = itemView.findViewById(R.id.iv_edit_cluster);
mDeleteCluster.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(listener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION){
listener.onDeleteCluster(position);
}
}
}
});
mEditCluster.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(listener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION){
listener.onEditCluster(position);
}
}
}
});
//used for highlighting item
itemView.setOnClickListener(this);
}
//This
#Override
public void onClick(View v) {
itemClickListener.onClick(v, getAdapterPosition());
}
}
public CLusterListAdapter(ArrayList<ClusterItem> listOfClusters ){ mListOfClusters = listOfClusters;}
#NonNull
#Override
public ClusterListViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.chose_your_cluster_list_item, viewGroup, false);
ClusterListViewHolder clusterListViewHolder = new ClusterListViewHolder(view, mListener);
return clusterListViewHolder;
}
#Override
public void onBindViewHolder(#NonNull ClusterListViewHolder clusterListViewHolder, int i) {
ClusterItem currentCluster = mListOfClusters.get(i);
clusterListViewHolder.mClusterName.setText(currentCluster.getClustersName());
clusterListViewHolder.mClusterMembers.setText(currentCluster.getClustersMembers());
clusterListViewHolder.mClusterMembers.setSelected(true);
clusterListViewHolder.setItemClickListener(new OnItemClickListener() {
#Override
public void onDeleteCluster(int position) {
//nothing here
}
#Override
public void onEditCluster(int position) {
//nothing here
}
#Override
public void onClick(View view, int position) {
//Select a certain item
row_index = position; //Set row index to selected position
SpTAGs.currentItem = mListOfClusters.get(position); //Set current item is item selection
notifyDataSetChanged(); //Made effect on RecyclerView's Adapter
SpTAGs.selectedClusterPosition = position;
}
});
//Set highlight color
if(row_index == i) {
clusterListViewHolder.itemView.setBackgroundColor(Color.parseColor("#E64A19"));
}else {
clusterListViewHolder.itemView.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
}
#Override
public int getItemCount() {
return mListOfClusters.size();
}
}
And here is how I applied the addOnItemTouch
clusterRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(#NonNull RecyclerView recyclerView, #NonNull MotionEvent motionEvent) {
selectButton.setEnabled(true);
Log.v("MyTAG", "Touch");
return false;
}
#Override
public void onTouchEvent(#NonNull RecyclerView recyclerView, #NonNull MotionEvent motionEvent) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean b) {
}
});
Any idea how to obtain this? Thank you!
Have a callback from the Adapter to the activity. Enable your button there.
public class CLusterListAdapter extends RecyclerView.Adapter<CLusterListAdapter.ClusterListViewHolder> {
...
interface Listener {
void onItemClick(int position);
}
}
Invoke this on item click
clusterListViewHolder.setItemClickListener(new OnItemClickListener() {
#Override
public void onDeleteCluster(int position) {
//nothing here
}
#Override
public void onEditCluster(int position) {
//nothing here
}
#Override
public void onClick(View view, int position) {
//Select a certain item
row_index = position; //Set row index to selected position
SpTAGs.currentItem = mListOfClusters.get(position); //Set current item is item selection
notifyDataSetChanged(); //Made effect on RecyclerView's Adapter
listener.onItemClick(position); // notify the listener (activity) of the click
SpTAGs.selectedClusterPosition = position;
}
});
In activity:
clusterRecyclerView.addOnItemTouchListener( ... );
clusterRecyclerView.setListener((int position) -> {
// logic to enable button goes here
});
you don't need to use the addOnItemTouchListener here.
just create a methode inside the activity like this
public void VisibleButton(){
yourButton.setVisibility(View.VISIBLE);
/**....*/
}
and just call the methode
VisibleButton()
from your adapter,from where you want your button to be visible, just like this
((YourActivityName)your_ACtivity_Context).VisibleButton();
Here I clicked on the item to change item background and color. I've stored the clicked item value in the database and change the layout color and text color and recreating the adapter and showing the list again while refreshing.
But layout colors not changed when I get its position. Please show the right path to handle the set of background item color always.
public class LoadVehicleTypeAdapter extends RecyclerView.Adapter<LoadVehicleTypeAdapter.CarTypesHolder> {
private List<TaxiTypeResponse.Message> CarTypesModelsList;
private Context mContext;
VehicleTypeView vehicleTypeView;
int I = -1;
int idd = 0;
int II = 0;
Activity activity;
GoogleMap map;
List<VehicleClick> list;
private SparseBooleanArray selectedItems;
public class CarTypesHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public CustomTextView mCarType;
public CircleImageView mCarTypeImage;
LinearLayout llRoot;
CardView cardView;
setOnitemclick listener;
public void setOnItemClickListner(setOnitemclick listener) {
this.listener = listener;
}
public CarTypesHolder(View view) {
super(view);
mCarType = (CustomTextView) view.findViewById(R.id.frag_cartypes_inflated_name);
mCarTypeImage = (CircleImageView) view.findViewById(R.id.frag_cartype_inflated_frameImage);
llRoot = (LinearLayout) view.findViewById(R.id.root1);
selectedItems = new SparseBooleanArray();
view.setOnClickListener(this);
}
#Override
public void onClick(View v) {
listener.ImageClick(v, getAdapterPosition());
}
}
public LoadVehicleTypeAdapter(Context context, List<TaxiTypeResponse.Message> CarTypesModelsList, VehicleTypeView vehicleTypeView, Activity activity, GoogleMap map, List<VehicleClick> lists) {
this.CarTypesModelsList = CarTypesModelsList;
mContext = context;
this.vehicleTypeView = vehicleTypeView;
this.activity = activity;
this.map = map;
this.list = lists;
}
#Override
public CarTypesHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.frag_cartype_inflated_view, parent, false);
return new CarTypesHolder(itemView);
}
#SuppressLint("ResourceType")
#Override
public void onBindViewHolder(final CarTypesHolder holder, int position) {
if (list.size() != 0) {
II = Integer.parseInt(list.get(0).RideId);
//setSelection(Integer.parseInt(list.get(0).RideId));
}
if (II == position) {
holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
holder.mCarType.setTextColor(Color.parseColor("#FFFFFF"));
} else {
holder.llRoot.setBackgroundColor(Color.parseColor("#f3f3f3"));
holder.mCarType.setTextColor(Color.parseColor("#2196F3"));
}
SharedPreferences sharedPreferences = activity.getSharedPreferences("mSelected", Context.MODE_PRIVATE);
TaxiTypeResponse.Message carTypesModel = CarTypesModelsList.get(position);
holder.mCarType.setText(carTypesModel.getName());
holder.mCarTypeImage.setBackgroundResource(R.drawable.wait);
int color = Color.parseColor(PreferenceHandler.readString(mContext, PreferenceHandler.SECONDRY_COLOR, "#006fb6"));
holder.mCarType.setTextColor(color);
holder.setOnItemClickListner(new setOnitemclick() {
#Override
public void ImageClick(View v, int position1) {
I = position1;
notifyDataSetChanged();
try {
if (list.size() != 0) {
VehicleTypeFragment.myAppRoomDataBase.userDao().delete();
list.clear();
}
VehicleClick vehicleClick = new VehicleClick();
vehicleClick.setRideId(String.valueOf(position1));
VehicleTypeFragment.myAppRoomDataBase.userDao().insert(vehicleClick);
list.add(vehicleClick);
} catch (Exception e) {
}
}
});
if (I == position) {
holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
holder.mCarType.setTextColor(Color.parseColor("#ffffff"));
Animation bounce = AnimationUtils.loadAnimation(mContext, R.anim.bounce);
holder.llRoot.startAnimation(bounce);
} else {
holder.llRoot.setBackgroundColor(Color.parseColor("#f3f3f3"));
holder.mCarType.setTextColor(Color.parseColor("#2196F3"));
}
Picasso.with(mContext).load(carTypesModel.getImagePath()).into(holder.mCarTypeImage);
}
#Override
public long getItemId(int position) {
return CarTypesModelsList.get(position).getID();
}
#Override
public int getItemCount() {
return CarTypesModelsList.size();
}
public void setSelection(int position) {
II = position;
//notifyDataSetChanged();
}
public interface setOnitemclick {
void ImageClick(View view, int position);
}
#Override
public int getItemViewType(int position) {
return position;
}
}
I am not sure what did you mean by refreshing your list. I am guessing that you are recreating the adapter and showing the list again while you are refreshing. Hence the value of I is initialized with -1 each time you are creating the adapter.
You need to do the initialization as follows. Please consider the following is a pseudo code and you need to implement this of your own.
// While declaring your I
// int I = -1;
int I = getTheStoredValueFromDatabase(); // If there is no entry in database, getTheStoredValueFromDatabase function will return -1
I hope you get the idea. You might consider doing the same for other stored values.
for keep track record you need to add Boolean variable in TaxiTypeResponse.Message boolean isClick=false; and toggle this in
holder.setOnItemClickListner(new setOnitemclick() {
#Override
public void ImageClick(View v, int position) {
CarTypesModelsList.get(position).isClick=!CarTypesModelsList.get(position).isClick;
notifyDataSetChanged();
}
}
and modify below code as follow
if (CarTypesModelsList.get(position).isClick) {
holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
holder.mCarType.setTextColor(Color.parseColor("#ffffff"));
Animation bounce = AnimationUtils.loadAnimation(mContext, R.anim.bounce);
holder.llRoot.startAnimation(bounce);
}
else{
holder.llRoot.setBackgroundColor(Color.parseColor("#f3f3f3"));
holder.mCarType.setTextColor(Color.parseColor("#2196F3"));
}
Note: onBindViewHolder() is not a place to implement the click
listeners, but I am just providing you the logic for how to implement
single selection in recyclerView.
Now lets jump to the solution,
simply follow the below tutorial and change the variable, fields, and background according to your need, you have to implement the below method in onBindViewHolder() method of RecyclerView
First, initialize the lastClickedPosition and isclicked
int lastPositionClicked = -1;
boolean isClicked = false;
#Override
public void onBindViewHolder(#NonNull final MyViewHolder holder, final int position) {
holder.YOUR_VIEW.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// store the position which you have just clicked and you will change the background of this clicked view
lastPositionClicked = position;
isClicked = true;
// need to refresh the adapter
SlabAdapter.this.notifyDataSetChanged();
}
});
// only change the background of last clicked item
if (isClicked && position == lastPositionClicked) {
// change clicked view background color
} else {
// otherwise set the default color for all positions
}
}
let me know if this works.
on BindViewHolder method you'll use this code and set I=0 on globally
#SuppressLint("ResourceType")
#Override
public void onBindViewHolder(final CarTypesHolder holder, int position) {
SharedPreferences sharedPreferences = activity.getSharedPreferences("mSelected", Context.MODE_PRIVATE);
TaxiTypeResponse.Message carTypesModel = CarTypesModelsList.get(position);
holder.mCarType.setText(carTypesModel.getName());
holder.mCarTypeImage.setBackgroundResource(R.drawable.wait);
int color = Color.parseColor(PreferenceHandler.readString(mContext, PreferenceHandler.SECONDRY_COLOR, "#006fb6"));
holder.mCarType.setTextColor(color);
holder.setOnItemClickListner(new setOnitemclick() {
#Override
public void ImageClick(View v, int position1) {
I = position1;
notifyDataSetChanged();
try {
if (list.size() != 0) {
VehicleTypeFragment.myAppRoomDataBase.userDao().delete();
list.clear();
}
VehicleClick vehicleClick = new VehicleClick();
vehicleClick.setRideId(String.valueOf(position1));
VehicleTypeFragment.myAppRoomDataBase.userDao().insert(vehicleClick);
list.add(vehicleClick);
} catch (Exception e) {
}
}
});
if (I == position) {
holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
holder.mCarType.setTextColor(Color.parseColor("#ffffff"));
Animation bounce = AnimationUtils.loadAnimation(mContext, R.anim.bounce);
holder.llRoot.startAnimation(bounce);
} else {
holder.llRoot.setBackgroundColor(Color.parseColor("#f3f3f3"));
holder.mCarType.setTextColor(Color.parseColor("#2196F3"));
}
Picasso.with(mContext).load(carTypesModel.getImagePath()).into(holder.mCarTypeImage);
}
I am using a ViewPager with single fragment instance in which I am showing Media files like Images, Videos, Audio.
I have implemented ExoPlayer for handling Video & Audio files. And Glide for images.
To avoid the memory leaks I am releasing the ExoPlayer object like this in ItemViewerFragment.java :
private void releasePlayer() {
if (player != null) {
player.release();
player = null;
trackSelector = null;
simpleExoPlayerView.setPlayer(null);
}
}
#Override
public void onStart() {
super.onStart();
if (player == null && currentGalleryModel != null) {
initializePlayer();
}
}
#Override
public void onResume() {
super.onResume();
if (player == null && currentGalleryModel != null) {
initializePlayer();
}
}
#Override
public void onPause() {
super.onPause();
releasePlayer();
}
#Override
public void onStop() {
super.onStop();
releasePlayer();
}
And in onViewCreated() I am initializing view like this :
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (currentGalleryModel.isVideo() || currentGalleryModel.isAudio()) {
simpleExoPlayerView.setVisibility(View.VISIBLE);
imageView.setVisibility(View.GONE);
initializePlayer();
} else if (currentGalleryModel.isImage() || currentGalleryModel.isGif()) {
simpleExoPlayerView.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);
Glide.with(this)
.load(currentGalleryModel.getFilePath())
.placeholder(android.R.color.black)
.fitCenter()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(imageView);
}
}
I am using FragmentStatePagerAdapeter. This is the getItem method :
#Override
public Fragment getItem(int position) {
return ItemViewerFragment.newInstance(mItems.get(position));
}
I am not able to detect the onPause of the fragment on first swipe of Viewpager. On second swipe video/audio files are stopped playing.
In activity I have tried adding .addOnPageChangeListener :
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
try {
((ItemViewerFragment)mAdapter.getItem(mPreviousPos)).imHiddenNow();
} catch (Exception e) {
e.printStackTrace();
}
mPreviousPos = position;
}
And in ItemViewerFragment.java :
public void imHiddenNow(){
releasePlayer();
}
Still video/audio keeps on playing.
Here is a Video Link to the Screencast.
Demo project GitHub link.
I followed an approach of maintaining HashMap of fragment objects inside PagerAdapter
Declare an interface :
interface FragmentLifecycle {
void onPauseFragment()
}
Implement an interface in Fragment.
public void onPauseFragment() {
if (simpleExoPlayer != null){
simpleExoPlayer.setPlayWhenReady(false);
}
}
Store all the fragment objects in a HashMap<Integer,Fragment> with there respective positions as a key. Declare hashmap inside PagerAdapter. Also declare one getter method for accessing fragment objects from hashmap. e.g.
#Override
public Fragment getItem(int position) {
ItemViewerFragment fragment = ItemViewerFragment.newInstance(mItems.get(position));
mFragments.put(position,fragment);
return fragment;
}
public ItemViewerFragment getMapItem(int position) {
return mFragments.get(position);
}
In activity where you have declared viewPager keep one variable currentPosition and implement ViewPager.OnPageChangeListener.
Inside onPageSelected method,
#Override
public void onPageSelected(int position) {
if(mAdapter.getMapItem(currentPosition) != null) (mAdapter.getMapItem(currentPosition)).onPauseFragment();
currentPosition = position;
}
Edit from the future: you should never hold a reference to Fragment instances directly inside a FragmentPagerAdapter, because it can cause crashes after process death.
Here is the code for the pager adapter:
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Here is the scroll Listener:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//Stop media here.
}
#Override
public void onPageSelected(int position) {
//Save your previous position here.
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
For the media you can use a for Loop and add all the fragments to the list at once and then use this for efficiency :
viewPager.setOffscreenPageLimit(3);
This will make sure only 3 instances of your fragment are available which is enough.
For using single fragment i would suggest you to do it like this:
public MyFragment() {
}
//This is to send a file to the fragment if you need it.
public static MyFragment newInstance(File file) {
MyFragment fragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putSerializable("file", file);
fragment.setArguments(bundle);
return fragment;
}
Then in the onCreate of Fragment you can retrieve your file like this:
File file;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
file = getArguments().getSerializable("file");
}
Now add your fragments to pager like this:
for (int i = 0; i < totalFiles; i++) {
viewPagerAdapter.addFragment(MyFragment.newInstance(fileList.get(i));
}
Hope this helps.
It's been more then a year since I used Exoplayer & I kinda tackled a similar problem. Please note that the APIs have changed a little bit so take the following code just to get an idea on how to implement a potential solution. Please let me know if it doesn't work, I'll look into the APIs further and get back to you.
Coming to the solution:
private int mPlayerCurrentPosition;
private int getCurrentPlayerPosition() {
return mExoPlayer.getCurrentPosition();
}
// call this from onPause
private void releaseExoplayer() {
mPlayerCurrentPosition = getPlayerCurrentPosition();
mExoPlayer.setPlayWhenReady(false);
mExoPlayer.release(); // this will make the player object eligible for GC
}
private void resumePlaybackFromPreviousPosition(int prevPosition) {
mExoPlayer.seekTo(mPlayerCurrentPosition );
}
The problem is that onPause and onResume are not called when the fragment visibility changed in ViewPager.
The solution is to add 2 visibility events: losingVisibility and gainVisibility.
Why is it a great solution?
Because you keep the framework managing Fragment cache and lifecycle.
We just add the callbacks needed to pause and resume our media in our fragment.
Step-by-step:
The below code is just an explanation for my code. Check Step*.java classes to see full implementation.
Create losingVisibility and gainVisibility methods in YourFragment.java:
public class YourFragment extends Fragment {
/**
* This method is only used by viewpager because the viewpager doesn't call onPause after
* changing the fragment
*/
public void losingVisibility() {
// IMPLEMENT YOUR PAUSE CODE HERE
savePlayerState();
releasePlayer();
}
/**
* This method is only used by viewpager because the viewpager doesn't call onPause after
* changing the fragment
*/
public void gainVisibility() {
// IMPLEMENT YOUR RESUME CODE HERE
loadVideo();
}
}
Call losingVisibility and gainVisibility every time a new page is selected (onPageSelected) in YourActivity.java:
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
YourFragment cachedFragmentLeaving = mYourPagerAdapter.getCachedItem(mCurrentItem);
if (cachedFragmentLeaving != null) {
cachedFragmentLeaving.losingVisibility();
}
mCurrentItem = position;
YourFragment cachedFragmentEntering = mYourPagerAdapter.getCachedItem(mCurrentItem);
if (cachedFragmentEntering != null) {
cachedFragmentEntering.gainVisibility();
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Add getCachedItem to YourPagerAdapter.java:
The 3rd step is adding a method to retrieve cached fragments. To do it we must cache a reference to a fragment created (overriding instantiateItem) and release the same reference (overriding destroyItem).
public class YourPagerAdapter extends FragmentStatePagerAdapter {
private SparseArray<YourFragment> mFragmentsHolded = new SparseArray<>();
#NonNull
#Override
public Object instantiateItem(ViewGroup container, int position) {
Object fragment = super.instantiateItem(container, position);
if(fragment instanceof StepFragment) {
mFragmentsHolded.append(position, (StepFragment) fragment);
}
return fragment;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
mFragmentsHolded.delete(position);
super.destroyItem(container, position, object);
}
public YourFragment getCachedItem(int position) {
return mFragmentsHolded.get(position, null);
}
}
i know its so late but i hope it helps anyone ...
viewPager keeps offscreen fragment started ..
so you have:
prev offscreen fragment (start) --> visible fragment (working)--> next offscreen fragment (start)
solution is you can override Fragment.setUserVisibleHint() and handle play/pause video..
I have a RecyclerView that has a grid of items. Upon clicking on an item, it highlights.
I also want that when the user swipes right the a 'next' method is called, and when the user swipes left, a 'previous' method is called.
However, the two don't work out with each other, as each intercepts the other's events.
How do I get them to work together?
This is my code:
RecyclerView Adapter
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
myHolder = holder as MyView;
myHolder.mMainView.SetOnClickListener(this);
if (selected_position == position)
{
holder.ItemView.SetBackgroundColor(Color.LightGray);
}
else
{
holder.ItemView.SetBackgroundColor(Color.Transparent);
}
}
public void OnClick(View v)
{
int position = mRecyclerView.GetChildLayoutPosition((View)sender);
// Updating old as well as new positions
NotifyItemChanged(selected_position);
selected_position = position;
NotifyItemChanged(selected_position);
}
Fragment that contains the RecyclerView
calendarRecyclerView.SetOnTouchListener(this);
public bool OnTouch(View v, MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Down:
x1 = e.GetX();
break;
case MotionEventActions.Up:
x2 = e.GetX();
float deltaX = x2 - x1;
if (Math.Abs(deltaX) > MIN_DISTANCE)
{
// Left to Right swipe action
if (x2 > x1)
{
NextMonth();
}
// Right to left swipe action
else
{
PreviousMonth();
}
}
break;
}
return false;
}
Because I put return false in the OnTouch event, the item's click event is fired. However, the MouseDown event doesn't fire in OnTouch, preventing swiping back detection (beacuse x1 is always 0).
OnTouch event gets called on the first click, and the OnClick gets called only on the second click
Because MotionEventActions.Down and OnClickconflict. As a workaround I suggest you to change the background color at the MotionEventActions.Down event.
Create your own click listener
Call the listener when you touch down your items.
The listener will callback to MainActivity to notify the item changed.
At the same time the touch event will called.
I have set the OnTouchListener in the viewholder :
public class MyViewHolder:RecyclerView.ViewHolder,IOnTouchListener
{
private TextView textView;
private MyItemClickListener mListener;
private Context myContext;
float x1 = 0;
float x2 = 0;
public TextView TextView { get { return textView; } }
public MyViewHolder(View v, MyItemClickListener mItemClickListener) : base(v)
{
textView = v.FindViewById<TextView>(Resource.Id.itemText);
mListener = mItemClickListener;
v.SetOnTouchListener(this);
}
public MyViewHolder(View v, MyItemClickListener mItemClickListener, Context myContext) : this(v, mItemClickListener)
{
this.myContext = myContext;
}
public bool OnTouch(View v, MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Down:
x1 = e.GetX();
if (mListener != null)
{
mListener.OnItemClick(v, Position);
}
break;
case MotionEventActions.Up:
x2 = e.GetX();
float deltaX = x2 - x1;
if (Math.Abs(deltaX) > 5)
{
// Left to Right swipe action
if (x2 > x1)
{
NextMonth(v);
}
// Right to left swipe action
else
{
PreviousMonth(v);
}
}
break;
}
return true;
}
public Boolean NextMonth(View v)
{
Toast.MakeText(myContext, "NextMonth called", ToastLength.Short).Show();
return true;
}
public Boolean PreviousMonth(View v)
{
Toast.MakeText(myContext, "PreviousMonth called", ToastLength.Short).Show();
return true;
}
}
Defined the click interface :
public interface MyItemClickListener
{
void OnItemClick(View view, int postion);
}
set the click callback in the MainActivity to change the background color:
public class MainActivity : Activity,MyItemClickListener
{
RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
CustomAdapter mAdapter;
string[] dataSet;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
InitDataSet();
SetContentView(Resource.Layout.Main);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
mRecyclerView.SetLayoutManager(mLayoutManager);
mAdapter = new CustomAdapter(dataSet,this);
mAdapter.setOnItemClickListener(this);
mRecyclerView.SetAdapter(mAdapter);
//mRecyclerView.SetOnTouchListener(this);
}
public void InitDataSet()
{
dataSet = new string[60];
for (int i = 0; i < 60; i++)
{
dataSet[i] = "This is element #" + i;
}
}
public void OnItemClick(View view, int postion)
{
mAdapter.NotifyItemChanged(CustomAdapter.selected_position);
CustomAdapter.selected_position = postion;
mAdapter.NotifyItemChanged(postion);
}
}
}
Note: Keep your finger move fast, if the speed is slow enough the MotionEventActions.Down will not be called.
Github souce code
Screen shot:
Try this in your onClick
public void OnClick(View v)
{
int position = mRecyclerView.GetChildLayoutPosition((View)sender);
int oldPosition = selectedPosition;
selected_position = position;
// Updating old as well as new positions
NotifyItemChanged(oldPosition);
NotifyItemChanged(selected_position);
}
Notice that you have to change the selected position before updating both items
I have an Activity that uses a ViewPager, within this ViewPager is a TouchImageView (an ImageView that detects gestures and zooms/moves accordingly).
Now I want to change some things in the UI whenever the image is clicked, but because the TouchImageView is declared in the PagerAdapter I can't reach it and if I would setup something on the side of the PagerAdapter I couldn't reach the UI components I want to change (I would also like to use the same adapter somewhere else in my app when I got this working). I've tried setting up an onClickListener on the ViewPager itself, but that doesn't trigger. Now I really don't know what to do.
Here is the adapter:
public class FullScreenImageAdapter extends PagerAdapter {
private ArrayList<MediaFile> images;
public FullScreenImageAdapter( ArrayList<MediaFile> images) {
this.images = images;
}
#Override
public int getCount() {
return images.size();
}
#Override
public View instantiateItem(ViewGroup container, int position) {
TouchImageView img = new TouchImageView(container.getContext());
Bitmap bitmap = BitmapFactory.decodeFile(images.get(position).getPath());
img.setImageBitmap(bitmap);
container.addView(img, ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.MATCH_PARENT);
return img;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
And here is the method that interacts with it (in the ProjectOverviewActivity class):
private void viewPicture(final int position) {
View decorView = getWindow().getDecorView();
statusBarVisibility = decorView.getSystemUiVisibility();
// Hide the status bar.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
} else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
FullScreenImageAdapter imageAdapter = new FullScreenImageAdapter(mediaFiles);
final ViewPager viewPager = (ViewPager) findViewById(R.id.image_pager);
viewPager.setAdapter(imageAdapter);
viewPager.setCurrentItem(position);
View fancyBox = findViewById(R.id.fancyBox);
fancyBox.setVisibility(View.VISIBLE);
viewPager.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View btnClose = findViewById(R.id.fancyBoxBtnClose);
View btnPrev = findViewById(R.id.fancyBoxBtnPrev);
View btnNext = findViewById(R.id.fancyBoxBtnNext);
if (overlayButtonsVisible) {
btnClose.setVisibility(View.GONE);
btnPrev.setVisibility(View.GONE);
btnNext.setVisibility(View.GONE);
} else {
btnClose.setVisibility(View.VISIBLE);
if (viewPager.getCurrentItem() > 0) {
btnPrev.setVisibility(View.VISIBLE);
}
if (viewPager.getCurrentItem() < (mediaFiles.size() - 1)) {
btnNext.setVisibility(View.VISIBLE);
}
}
overlayButtonsVisible = !overlayButtonsVisible;
}
});
View fancyBoxBtnClose = findViewById(R.id.fancyBoxBtnClose);
final View fancyBoxBtnPrev = findViewById(R.id.fancyBoxBtnPrev);
final View fancyBoxBtnNext = findViewById(R.id.fancyBoxBtnNext);
fancyBoxBtnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View decorView = getWindow().getDecorView();
// Show the status bar.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
decorView.setSystemUiVisibility(statusBarVisibility);
} else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, statusBarVisibility);
}
findViewById(R.id.fancyBox).setVisibility(View.GONE);
setViewAndChildrenEnabled(findViewById(R.id.layoutProjectOverview), true);
overlayButtonsVisible = false;
}
});
fancyBoxBtnPrev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
if (viewPager.getCurrentItem() == 0) {
fancyBoxBtnPrev.setVisibility(View.GONE);
}
}
});
fancyBoxBtnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
if (viewPager.getCurrentItem() == (mediaFiles.size() - 1)) {
fancyBoxBtnNext.setVisibility(View.GONE);
}
}
});
}
You can try this
Update:
I think you should create your custom ViewPager with GestureDetector
Example:
public class ClickableViewPager extends ViewPager {
private OnItemClickListener mOnItemClickListener;
public ClickableViewPager(Context context) {
super(context);
setup();
}
public ClickableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
setup();
}
private void setup() {
final GestureDetector tapGestureDetector = new GestureDetector(getContext(), new TapGestureListener());
setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
tapGestureDetector.onTouchEvent(event);
return false;
}
});
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
mOnItemClickListener = onItemClickListener;
}
public interface OnItemClickListener {
void onItemClick(int position);
}
private class TapGestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if(mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(getCurrentItem());
}
return true;
}
}
}
and then in your activity
ClickableViewPager viewPager = (ClickableViewPager) findViewById(R.id.viewPager);
viewPager.setOnItemClickListener(new ClickableViewPager.OnItemClickListener() {
#Override
public void onItemClick(int position) {
// HADNLE YOUR CLICKS
}
});
SOURCE