I have a parent fragment which has a header view with tab layout & view pager.
I have 2 different views to be shown
view1 = headerView1 & TabLayout1 with ViewPager1
view2 = headerView2 & TabLayout2 with ViewPager2
when the fragment is launched I want to show view1 by default. On item click of a button in tabLayout of view1 I have to show the view2 and hide view1.
I am not able to figure out how to handle this. Any ideas?
Thanks
What have you tried so far? Can you post any code snippet? If I understand correctly, you have your TabLayout and ViewPager in your parent fragment. Attach a OnTabSelectedListener to your TabLayout, and override onTabSelected(TabLayout.Tab tab) method.
#Override
public void onTabSelected(TabLayout.Tab tab) {
//Change your viewpager pages here.
}
If you want to update your parent fragment from your View1 or View2 fragment, use the getParentFragment() method on your child fragments.
((YourParentFragment) getParentFragment()).doTask();
In your parent fragment,
public void doTask()
{
//Do your refreshing here.
//Or switch your pages in Viewpager.
}
Please post some code. That will get you better answers.
Related
I have used multiple tab layout activities (ex.5) & multiple image view (ex:5 ).i click (Ex:3) image then open particular tab (Ex:3). how to write code anybody help.Image used in main activity image click enter tab layout particular tab.
Use Android TabHost and its method setCurrentTab() in your onCreate()-method.Pass your required tab index to this onCreate()
On using ViewPager pass the selected imageview position
viewPager.setCurrentItem(position, true);
Using TabLayout, add onTabSelectedListener to swipe views
tabLayout.setOnTabSelectedListener(this);
When it comes to slide
viewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
viewPager.setCurrentItem(position);
tabLayout.setScrollPosition(position, 0f, true);
}
});
I have a main activity with 3 tabs,when i swipe the tabbed indicator doesn't move but the layout changes while selecting a tab the indicator moves and the layout changes too.
Make sure you have a OnPageChangeListener attached to your ViewPager
mViewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
mTabs.getTabAt(position).select();
}
});
You can also do like
tabLayout.setupWithViewPager(viewPager);
I currently have a view pager in a tab layout
//Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SettingsFragmentPagerAdapter(getSupportFragmentManager()));
//Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
And then the fragments in the view pager have TextViews with OnClickListeners attached
final TextView zSemTxt = (TextView) getActivity().findViewById(R.id.zSemTxt);
zSemTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mSelectedTextView = zSemTxt;
showDialogToSelectClass();
}
});
My issue is the OnClickListeners are never called, it appears that all touches go to the ViewPager and never goes through to the TextViews. I've tried things like viewPager.requestDisallowInterceptTouchEvent(true); but none of it change anything.
Ideas?
Show more code.
Probably onClick dosen't work because your textview = null.
final TextView zSemTxt = (TextView) getActivity().findViewById(R.id.zSemTxt);
Are You sure about this?
I dont see your code but try
final TextView zSemTxt = (TextView) view.findViewById(R.id.zSemTxt);
where view is your fragment inflated view.
Turns out neither the onActivityCreated or onCreateView were being called on the fragment.
The solution was to go to the PageFragment class I was using to manage which fragment was displayed by the ViewPager and add the onClickListeners to there. The fragments were being inflated there which is why the other classes were never called.
Suppose I am having 5 fragments in viewpager. Lets say Fragment A, B, C, D, E.
I have a button on fragment A to go to fragment B. When I press this button, viewpager scrolls to fragment B. Somehow I controlled the duration of fragment A to B. Now the thing is, I have an scrollable container in Fragment B. I want to scroll that container during the Scroll between A to B happens. Now its scrolling after I am going from Fragment A to B. I want this happen between this transition.
Inside first fragment:
#Override
public void onClick(View view) {
if(view.getId() == bottomLayout.getId())
{
MainActivity.pager.setCurrentItem(1);
}
}
It will change my pager position 0 to 1. Now when its changing. I controlled the scroll speed of viewpager. On top of second fragment, I have a ScrollView, I need to perform scroll on this scrollview as fragment is changing,not after its changed.
To animate the view of the FragmentB, you need to tell it to start animation as soon as you call viewPager.setCurrentItem().
Inside FragmentA's click listener, you will ask the parent Activity to change the ViewPager item (see Communicating with the Activity to know how this should be done) :
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
activityCallback.setViewPagerItem(POSITION_OF_FRAGMENT_B);
}
});
In your Activity in which the ViewPager resides, you will write:
public void setViewPagerItem(int position) {
viewPager.setCurrentItem(position);
// following asks the fragmentB to start its animation on its view.
if (position == POSITION_OF_FRAGMENT_B) {
fragmentB.startYourAnimation();
}
}
In FragmentB, you add a method:
public void startYourAnimation() {
// write your code to animate your ListView or whatever you want to
// animate inside the FragmentB
}
Notes:
Unless you've called viewPager.setOffscreenPageLimit(), an instance of FragmentB will already exist if you're doing it from FragmentA or FragmentC,
a Fragment should not communicate with other fragments directly, instead they should use their parent Activity as a middle person. See Communicating with Other Fragments.
No it is not possible to do so because any scrollable view like listview or recyclerview, the items are created only when it is visible to user and only when scrolled, so if that fragment is not visible, you cannot scroll any of its child scrollable view.
I Tried putting a Fragment into a FrameLayout inside another Fragment via FragmentManager and FragmentTransaction (from android.support.v4.app). The container fragment has a button and a TextView on top and a FrameLayout at the bottom (I create the layout programmatically and i don't want to hurt your eyes with all of that). The CreateView() works just fine and i cann access the FrameLayout at the bottom of the container and add or remove View dynamically as I please via
#Override
public void onClick(View button) {
FrameLayout frame = (FrameLayout)findViewById(DETAIL_CONTENT_FRAME);
ImageView im = new ImageView(this);
im.setImageResource(R.drawable.test);
frame.addView(im);
}
but when I try to add a fragment instead of an ImageView to the frameLayout the code compiles perfectly but the desired fragment doesn't appear after the onClickListener() method is called. I checked the onCreateView() method of the fragment and it returns a proper view...
#Override
public void onClick(View button) {
ServerDialogFragment serverDialog = new ServerDialogFragment();
FragmentTransaction addDialog = getSupportFragmentManager().beginTransaction();
addDialog.add(DETAIL_CONTENT_FRAME, serverDialog);
addDialog.commit();
}
Do you have an answer to this ?
PS: I once tried adding fragments into other fragments and it worked, but they were simple fragments only holding ImageViews.
Fragments inside of other fragments is not supported at this time. See:
Fragment Inside Fragment
Fragments within Fragments
Android: Can you nest Fragments?