I have a weird problem.
Achieved
I have 4 Fragments in a ViewPager attached with TabLayout. The first one is QR-code Scanner, which shows a camera. I wanted to start camera only when the Fragment is visible. For this, I override the Fragment's method setUserVisibleHint.
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser) {
checkPermissionForCamera(); //it checks permission and start camera
} else {
stopCamera();
}
}
And it's working absolutely fine.
Desired
Now, what I want to achieve is that when Fragment is not visible (or is being visible by scrolling), it shows a view with background over camera, so that instead of camera, that cover is visible. Like image below.
For it, I edited startCamera and stopCamera, now it looks like below,
public void startCamera() {
if(cameraCover != null)
cameraCover.setVisibility(View.GONE);
isCameraStarted = true;
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera();
}
public void stopCamera() {
if(cameraCover != null)
cameraCover.setVisibility(View.VISIBLE);
if(mScannerView != null) {
isCameraStarted = false;
mScannerView.stopCamera();// Stop camera on pause
mScannerView.stopCameraPreview();// Stop camera preview
}
}
But the gray cover is only visible for the first time, rest of the time I get the camera opened with view on which it's scrolled, like below.
I've also tried to override onPageScrolled of OnPageChangeListener, but with no luck. Here's what I've done.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if(position == 0) {
if(positionOffset > 0.7) {
fragment = adapter.getItemAtPosition(0); //fragment is at zero
if(fragment != null) {
if(fragment instanceof ScanQRFragment) {
((ScanQRFragment) fragment).stopCamera();
}
}
}
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Try either of the following ways :
1- override setMenuVisibility :
#Override
public void setMenuVisibility(final boolean visible) {
if (visible) {
//start camera preview
}
super.setMenuVisibility(visible);
}
2- Check yourFragment.isResumed() instead of isVisible()
Related
My question is quite similar to this one: fragment.getView() return null after backpressed
The problem is next: I have a special Fragment with two states: A and B. If Fragment is in the state B, Backpress should switch the state from B to A. (The difference between two states is in visibility of some elements on a layout) If Fragment is in state A, Backpress should close this Fragment. I overwrote the onBackPressed() method in my activity in next way:
#Override
public void onBackPressed() {
int count = getSupportFragmentManager().getBackStackEntryCount();
if (count > 1) {
if (isStateB) {
isStateB = false;
MessagesFragment messFragment = ((MessagesFragment) getSupportFragmentManager().findFragmentByTag("MessagesFragment"));
if (messFragment != null) {
Log.d(TAG, "Message Fragment Refresh");
messFragment.refreshFragWithoutMessChecked();
return;
} else {
Log.d(TAG, "Fail To Message Fragment Resfresh");
}
}
}
}
super.onBackPressed();
}
This code executes refreshFragWithoutMessChecked() but nothing happens. All elements from the layout which I am trying to close aren't null, but code doesn't affect them. Also I have a button in the MessageFragment which executes the similar method, and in case I press it, the code works well. In additional I found out that if I call getView() inside refreshFragWithoutMessChecked(). In case when it called from onBackPressed(), getView() returns NULL. In case when it called from onClick() it returns something isn't NULL.
So that is why I am asking, why Backpress makes my getView() returns NULL, and how can I solve my problem?
Fragment Code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag_messages, container, false);
/*
Here is a big amount of elements initizalizations and onClick bindings like this:
check_message = view.findViewById(R.id.check_message);
check_message.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {MainActivity.isStateB = true;}});
*/
return view;
}
public void refreshFragWithoutMessChecked() {
MainActivity.isStateB = false;
MainActivity.posMessChecksEnabled.clear();
refreshMessageFragment();
Log.d(TAG,"Message Fragment Refreshed");
}
// Refresh Message Fragment
public void refreshMessageFragment() {
if(getView() != null){
Log.d(TAG,"A");
} else {
Log.d(TAG,"B");
}
// Hide Message Check Show
// THIS CODE DOESN'T WORK ON BACK PRESS BUT IT'S EXECUTED
if (MainActivity.isStateB == true) {
((MainActivity) getActivity()).getSupportActionBar().hide();
write_message_layout.setVisibility(View.GONE);
reply_forward_bottom.setVisibility(View.VISIBLE);
up_message_selected_panel.setVisibility(View.VISIBLE);
toolbar.setVisibility(View.GONE);
appbar_layout.setVisibility(View.GONE);
} else {
if (fromOpen.equalsIgnoreCase(MainActivity.MESSAGES_FILTER_CONTACTS) == false) {
write_message_layout.setVisibility(View.VISIBLE);
}
reply_forward_bottom.setVisibility(View.GONE);
up_message_selected_panel.setVisibility(View.GONE);
toolbar.setVisibility(View.VISIBLE);
appbar_layout.setVisibility(View.VISIBLE);
}
}
I have fragment which includes a viewpager which includes many fragments. I am calling methods of fragments which are in viewpager from outer fragment. I can do this without any problem but I am facing an issue, when I go to another fragment from fragments which are in viewpager and then I come back, I cannot use that method because method-calling object viewed as null.
Problem phases in order
layout structure
1- In outer fragment, a method of one of viewpager's fragment is called without anyproblem.
2- Click on anyitem in recycler view in fragments which are settled in viewpager.
3- It directs user to another fragment.
4- Go back via popbackstackimmediate().
5- First phase causes problem with null pointer exception.
This is the method implemented in outer fragment.
private void setPagination() {
nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
#Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (v.getChildAt(v.getChildCount() - 1) != null) {
if ((scrollY >= (v.getChildAt(v.getChildCount() - 1).getMeasuredHeight() - v.getMeasuredHeight())) &&
scrollY > oldScrollY) {
if (currentPage.equalsIgnoreCase("A"))
AFragment.loadMore();
else if (currentPage.equalsIgnoreCase("B"))
BFragment.loadMore();
else if (currentPage.equalsIgnoreCase("C"))
CFragment.loadMore();
else if (currentPage.equalsIgnoreCase("D"))
DFragment.loadMore();
}
}
}
});
}
LoadMore() method
public void loadMore() {
if (list_a.size() % 4 == 0) {
ManagerAll.getInstance().fetchA(id_number, page).enqueue(new Callback<List<A>>() {
#Override
public void onResponse(Call<List<A>> call, Response<List<A>> response) {
if (response.isSuccessful()) {
int before_update = list_a.size();
list_a.addAll(response.body());
int after_update = list_a.size();
adapter.notifyItemRangeInserted(before_update, after_update);
page++;
}
}
#Override
public void onFailure(Call<List<A>> call, Throwable t) {
}
});
}
}
I cannot access list_a here because it throws null pointer exception. I guess it is not initialized yet after I come back from another fragment.
I have two questions about ViewPager.
I have in my activity 2 ViewPagers, one for each week (of date). For example, one for 14/9/14 - 20/9/14, and the the other one for the next week 21/9/14 - 27/9/14. When I slide the ViewPager to it's next one, it will change each on of the ViewPagers to the next week of itself (21/9/14 - 27/9/14, 28/9/14 - 4/10/14).
When I change the data f the second ViewPager and then slide the first ViewPager to it's next one (and this is the same week like the second view pager data that I changed), it won't show the changes that I made, unless I slide two more times and then return to the changed week (because of the memory of the ViewPager). I tried mPager1.setOffscreenPageLimit(0); , but it didn't helped. What can I do in order to solve that?
I want that when I slide one of my ViewPagers, so the other one will animate itself exactly like my finger is sliding the other ViewPager. Any ideas how to do that?
I hope it helps:
it won't show the changes that I made
use adapter.notifyDataSetChanged(); after you have made changes.
I want that when I slide one of my ViewPagers, so the other one will
animate itself exactly like my finger is sliding the other ViewPager.
Any ideas how to do that?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager1= (ViewPager) findViewById(R.id.pager1);
viewPager2= (ViewPager) findViewById(R.id.pager2);
viewPager1.setAdapter(new MyAdapter1(getSupportFragmentManager()));
viewPager2.setAdapter(new MyAdapter2(getSupportFragmentManager()));
viewPager1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
viewPager2.onTouchEvent(event);
return false;
}
});
viewPager2.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
viewPager1.onTouchEvent(event);
return false;
}
});
}
if you got
09-19 05:49:29.313: E/AndroidRuntime(1272): java.lang.IllegalArgumentException: pointerIndex out of range
try:
in your layout file you must use <package name of HackyViewPager.HackyViewPager instead of <android.support.v4.view.ViewPager
public class HackyViewPager extends ViewPager {
public HackyViewPager(Context context) {
super(context);
}
public HackyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
try {
return super.onInterceptTouchEvent(ev);
} catch (IllegalArgumentException e) {
e.printStackTrace();
return false;
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
try {
return super.onTouchEvent(event);
}catch (IllegalArgumentException e) {
e.printStackTrace();
return false;
}
}
}
Reference:
Controlling two ViewPager Together
I'm using 3 Fragments inside a Viewpager, the problem is that I am loading big data in an Asynctask and loaders. On devices like HTC one, it works fine, however, on low-end devices, it takes a lot of time. This is mainly because when I implement the pagerAdapter, I put the Fragments inside an ArrayList, this force the fragments instantiate when the main activity is loaded. What I need is that it just "load" the first fragment (main) and when the user Swype, load the other fragment. its any way to achieve this? this is my pageAdapater
public class PagerAdapter extends FragmentPagerAdapter {
private final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
// private final ArrayList<String> titulos = new ArrayList<String>();
// private int NUM_PAGES =0;
public PagerAdapter(FragmentManager manager,int num_pages) {
super(manager);
// this.NUM_PAGES = num_pages;
}
public void addFragment(Fragment fragment,String title) {
mFragments.add(fragment);
notifyDataSetChanged();
}
#Override
public int getCount() {
//return NUM_PAGES;
return mFragments.size();
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
}
The above method from Sun did not work for me (maybe it does for you), but I thought I would share my edit of his method also. Very good method by the way Sun!
private boolean _hasLoadedOnce= false; // your boolean field
#Override
public void setUserVisibleHint(boolean isFragmentVisible_) {
super.setUserVisibleHint(true);
if (this.isVisible()) {
// we check that the fragment is becoming visible
if (isFragmentVisible_ && !_hasLoadedOnce) {
new NetCheck().execute();
_hasLoadedOnce = true;
}
}
}
I'm gonna add my solution here since I faced a similar issue. My asynchronous task wasn't loading huge amounts of data, but it prevents unnecessary network calls. Here's what I added in my Fragment:
private boolean _hasLoadedOnce= false; // your boolean field
#Override
public void setUserVisibleHint(boolean isFragmentVisible_) {
super.setUserVisibleHint(isVisibleToUser);
if (this.isVisible()) {
// we check that the fragment is becoming visible
if (!isFragmentVisible_ && !_hasLoadedOnce) {
//run your async task here since the user has just focused on your fragment
_hasLoadedOnce = true;
}
}
}
With the above code, your Fragment will be loaded, but your async task will not run until the user actually scrolls to the Fragment for the first time. Once displayed, your async task will run for the first time automatically. Then you can provide a way to load more data via a button or pull to refresh. The above Fragment was in my ViewPager and seemed to work fine.
Slightly modified version to fix potential NPE caused by some views not fully initialised
private boolean _hasLoadedOnce= false; // your boolean field
#Override
public void setUserVisibleHint(boolean isFragmentVisible_) {
super.setUserVisibleHint(isVisibleToUser);
if (this.isVisible()) {
// we check that the fragment is becoming visible
if (!isFragmentVisible_ && !_hasLoadedOnce) {
new Handler().post(() -> {
makeAsyncRequest();//do your asyn stuffs
_hasLoadedOnce = true;
});
}
}
}
Use fragmentStatePageAdapter if you have a lot of pages and you want to destroy them when not visible.
It has implemented a setMenuVisibility(boolean menuVisible) when fragment becomes visible, so use that.
I might be late for the party but here's my solution and it works as expected. In all of your child fragments create a boolean variable:
private boolean loadFragmentExecuted = false;
in the child fragments create a generic method called loadFragment and move all of the logic you added in onCreateView to that method:
public void loadFragment()
{
if(!loadFragmentExecuted)
{
//Add your logic to manipulate the UI or load data etc...
loadFragmentExecuted = true;
}
}
in your pageview logic create the fragments dynamically like:
//add the fragment
String fragmentName = "com.something." + fragmentId;
//check if the class exists
try
{
Class myFragmentClass = Class.forName(fragmentName);
Fragment myFragment = (Fragment) myFragmentClass.newInstance();
mFragments.add(myFragment);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (InstantiationException e)
{
e.printStackTrace();
}
then set your pager adapter and attach a tablayout with it:
//set our pager adapter that contains different fragments
mPagerAdapter = new BasePagerAdapter(mFragmentManager, mFragments);
//link the adapter to the viewpager
mViewPager.setAdapter(mPagerAdapter);
//cache fragments
int limit = (mPagerAdapter.getCount() > 0 ? mPagerAdapter.getCount() : 1);
mViewPager.setOffscreenPageLimit(limit);
//add the page listner to the viewPager and link it to the tabLayout
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
//on tab selected select current viewpager item
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
#Override
public void onTabSelected(TabLayout.Tab tab)
{
mViewPager.setCurrentItem(tab.getPosition());
//get fragment for the selected tab
Fragment f = mPagerAdapter.getItem(tab.getPosition());
//load the content of the fragment
try
{
Class c = f.getClass();
Method loadFragment = c.getMethod("loadFragment");
loadFragment.invoke(f);
}
catch (IllegalAccessException e){}
catch (InvocationTargetException e){}
catch (NoSuchMethodException e){}
}
#Override
public void onTabUnselected(TabLayout.Tab tab)
{
}
#Override
public void onTabReselected(TabLayout.Tab tab)
{
}
});
I use ViewPager to implement my own image gallery. The views consists of ImageViews. The user can normally navigate between images using gestures (as in ViewPager).
What I want to add is the slideshow feature. When user chooses an option "slideshow" from the menu I want to be able to start the animation of ViewPager items - ideally the animation would be the fade-in/out effect between images/slides.
Is it possible to implement this with ViewPager?
Here's how I did it. First I used a lot of the code from the Android Displaying Bitmaps in Your UI tutorial.
Then, I added the following tweaks:
In the OnCreate of the activity that contains the ViewPager:
mPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
PhotoViewerFragment currentFragment = mAdapter
.getFragment(arg0);
if (currentFragment != null) {
if (mRunningSlideshow) {
currentFragment.addImageTransition();
}
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
});
Runnable that runs slideshow in the activity that contains the ViewPager:
private Runnable runSlideshow = new Runnable() {
public void run() {
// Second parameter of false turns ViewPager scroll animation off
mPager.setCurrentItem(mPager.getCurrentItem() + 1, false);
mSlideshowHandler.postDelayed(runSlideshow,
SLIDESHOW_IMAGE_DURATION);
}
};
In the fragment that displays the image:
private static final int FADE_IN_TIME = 200;
/**
* Adds fade-in transition when in slideshow mode. Called from PhotoViewerActivity.
*/
public void addImageTransition() {
// Transition drawable with a transparent drawable and the final bitmap
final TransitionDrawable td = new TransitionDrawable(new Drawable[] {
new ColorDrawable(android.R.color.transparent),
mImageView.getDrawable() });
mImageView.setImageDrawable(td);
td.startTransition(FADE_IN_TIME);
}