Inflating a view inside the viewpager - java

This is what I'm hoping to achieve:
Step 1: Click on the Green Button to open View1.xml on the ViewPager. (Works)
Step 2: Click on the Orange Button to open SubView.xml on the ViewPager. (Doesn't work)
So, I have set the onClick event for the orange button the following:
public void openForm(View view) {
ViewGroup item = (ViewGroup)findViewById(R.id.vp_horizontal_ntb); //this is the view pager
View child = getLayoutInflater().inflate(R.layout.activity_post_form, null);
item.addView(child);
}
Results:
Nothing. Nothing happens.
If I switch viewpager for linearlayout it opens (and breaks tab navigation). What I wanna do is open it on the viewpager, so it looks and behaves as if it's on a tab, until it's removed by clicking on another tab.
I also thought about putting a linear layout there and make viewPager gone while the linearLayout is set to visible. But I feel like there's a better solution than this.

Related

TabLayout Fragment Issue

Current layout
The above is the app layout in Android app that I am working on. There is an activity, which has a BottomNavigationView. The navigation is managed using:
this.getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();
Now, on this fragment, I have a TabLayout and a ViewPager as you can see in the image. The ViewPager listens to the TabLayout selector and it can be changed by either sliding the ViewPager or clicking on the tab. Works fine..
Once a tab is selected, I am loading another Fragment that has a RecyclerView on it. The contents in the RecyclerView is coming from a URL so its being updated on Post() as below:
CompletableFuture.runAsync(this::prepare);
recyclerView.post(() ->
{
adapter.list = newList;
adapter.notifyDataSetChanged();
progress.setVisibility(View.INVISIBLE);
});
Now the issue that I am facing:
Everything works fine for the first time when the Menu 1 in the layout image is clicked. The recycler view is able to call the service and load the content. Now if I switch the navigation to Menu 2 or any other and try to come back to Menu 1, nothing progresses from the ViewPager / TabLayout. Up to the TabLayout I can see Logs, and after that nothing. Basically setting the currentItem to the TabLayout is called but its not calling the create of the next Fragment and hence its not loading the RecyclerView again. The ViewPager is using FragmentStatePagerAdapter.
TabViewerAdapter extends FragmentStatePagerAdapter
The adapter is initialised using ChildFragmentAdapter as below:
TabViewerAdapter adapter = new TabViewerAdapter(getChildFragmentManager(), 0, tabs.getTabCount());
I know behaviour "0" is deprecated so tried with "1" as well as below:
TabViewerAdapter adapter = new TabViewerAdapter(getChildFragmentManager(), 1, tabs.getTabCount());
Tried invalidating views, notifying adapter and many other.. No use.
I also noticed that when I switch app (while its running by tapping the phone square button to go to another app) and come back (resume) then its loading the recycler view again.
This means, I am missing something and tried the onResume method. But didn't work.
Am I doing something stupid or against design patterns or conventions? No idea what to do..
Please help, thank you.
I resolved this issue by doing the things below:
Used a SharedViewModel to handle the list
Used ListAdapter for RecyclerView
All good now.

Hamburger to arrow animation not working programmatically

I have made a server directory browsing app which would change contents within the Activity itself. I have been adding a feature: Navigation Drawer and handling the Hamburger and Back icon on the Toolbar as follows:
Home directory:
Hamburger icon as the default state.
Would slide the navigation drawer on clicking the hamburger or at a sliding gesture.
No state-change or animation of the hamburger when the drawer is slided.
Animation of Hamburger to Back icon when a directory is chosen.
Any child directory:
Back button from the previous animation whose sole purpose is to go to a parent directory.
Would slide the navigation drawer at a sliding gesture.
No state-change or animation of the Back icon when the drawer is slided using gesture or when it goes into an another child directory of this directory.
Animation of Back Arrow to Hamburger icon when it comes back to Home directory using back icon or onBackPressed.
I am able to get the animation of Hamburger to Back icon using this answer (Code is used verbatim is as below) but wasn't able to get the Hamburger icon again when coming back into the home directory (didn't included that code and went for another approach which is the next part):
ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
mDrawerToggle.onDrawerSlide(drawerLayout, slideOffset);
}
});
anim.setInterpolator(new DecelerateInterpolator());
// You can change this duration to more closely match that of the default animation.
anim.setDuration(500);
anim.start();
For appropriate switching between Hamburger and Back icons when browsing to and fro from home and child directories, I have used this answer (Code verbatim as below) as a reference and was able to successfully implement it for 1, 2 and 3 features of home and child directories.
private void enableViews(boolean enable) {
if(enable) {
mDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true); // comment this line of code
if(!mToolBarNavigationListenerIsRegistered) {
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Doesn't have to be onBackPressed
onBackPressed();
}
});
mToolBarNavigationListenerIsRegistered = true;
}
}
else {
// Remove back button
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false); // comment this line of code
// Show hamburger
mDrawerToggle.setDrawerIndicatorEnabled(true);
// Remove the/any drawer toggle listener
mDrawerToggle.setToolbarNavigationClickListener(null);
mToolBarNavigationListenerIsRegistered = false;
}
}
Coming to the issue at hand which is: While browsing directories, of the switching to and fro from Hamburger to Back icon, the animation part is not working at all. But the states of both the icons are successfully changed along with their functionalities. Let me know if you need some more information for troubleshooting.
You can see a working example of navigation drawer activity if you just create a new project, add an activity and use template NavigationDrawer (if you use Android Studio. Otherwise download this repo)
When I want to learn a new layout I just load up the template and then change individual pieces of code until I have what I wanted. This way you can see what does what, what stops working when you delete some line and how things should be done.
I was finally able to solve it, came to know about the behaviour of the ActionBarDrawerToggle in a much deeper way after tinkering it with the default NavigationBarActivity from Android studio.
The overriding of onDrawerSlide of the mDrawerToggle to block the animation of hamburger by the sliding drawer was the cause of the same blocking of animation of hamburger to arrow in the animatior function in the first place. Notice these two lines from the two different pieces of code (didn't include it earlier, but you get the idea):
#Override
public void onDrawerSlide(View view, float slideOffset) {
// blocks the animation
super.onDrawerSlide(view, 0);
}
// from the animator function above
mDrawerToggle.onDrawerSlide(drawerLayout, slideOffset);
Solution: I removed the overrided onDrawerSlide function, but then, the sliding drawer hamburger to arrow animation would come back as well.
Counter-Solution: I also found out the sliding drawer animation of hamburger to arrow happens due to this line: mDrawerLayout.setDrawerListener(mDrawerToggle) which again is a deprecated function. So I just commented out this line and everything is working as expected.

Why my back button does nothing?

I have a AppCompatActivity activity named MainActivity with the following code placed on onCreate method to show/hide back and menu button
getSupportFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
toggle.setDrawerIndicatorEnabled(
getSupportFragmentManager().getBackStackEntryCount() == 0);
getSupportActionBar().setDisplayHomeAsUpEnabled(
getSupportFragmentManager().getBackStackEntryCount() > 0);
}
});
This is my only activity, I use fragments for the different views. The back button shows perfectly when appropiated but does nothing when I click on it.
Do I have to put some code on fragments? I have checked many other similar questions but I'm not able to detect what's missed
EDIT
Many solutions ask to override onOptionsItemSelected on Fragment or Activity but this method is not called when I click on the back button on toolbar.
EDIT 2
If I comment line
toggle.setDrawerIndicatorEnabled(getSupportFragmentManager().getBackStackEntryCount() == 0);
then back button click opens navigation menu.
You'll have to manually handle the home button as shown here :
catch toolbar home button click event
then load the previous fragment from backstack:
Get Fragment from backstack

Can't access to a spinner in Java

I've got an activity where there is a button which opens an AlertDialog.
My dialog works and I decided to add a layout to it which contains a spinner.
So I have 3 documents :
mainActivity.java : Its role is to open a Dialog
activity_main.xml
dialog_main.xml : The dialog's layout containing the spinner
I try to retrieve in mainActivity.java the spinner declared in dialog_main.xml (in order to add it an adapter) :
Spinner mySpinner = (Spinner)findViewById(R.id.mySpinner);
However mySpinner = NULL, I can't find mySpinner. What is the problem ?
findViewById
as a method of the Activity class finds Views in your Activity's layout. When you are showing a Dialog, it is not part of your layout, so you have to findViewById in the Dialog's layout.
You are probably inflating a View for the Dialog that you show, you cou can use this view to find your Spinnner.
It probably looks like
View view = layoutInflater.inflate (...);
then you do
view.findViewById(...);
Post the code of how how show the Dialog and I'll show you if you can't do it.

Android Add image to listviewitem after click on button

In my application I have a listview with products. Above the listview in the actionbar I have a button "edit list". When you click on the button I want to add an image to each listviewitem in the listview. When you click on the image you delete it from the list. Now I added an imageview to the listviewitem and set die visibility to gone.
But I don't know how to change the visibility with a click on a button.
So the question: How do you add an imageview in listviewitem with click on button? Or how do you change the visibility property of an imageview in an Listviewitem?
You can use a static parameter like int uiUpdate=0 and use an adapter to add data to list view and check uiUpdate in getView of your adapter. When user click on button set uiUpdate=1 and reffresh your list with this : yourlist.setListAdapter(new EfficientAdapter);
in getView of EfficientAdapter check value of uiUpdate if it's 1 set visibility VISIBLE : yourimageview.setVisibility(View.VISIBLE); efficientAdapter is something like this : http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

Categories

Resources