Changing ImageView depends on Tab - android

I am having 2 Tab fragment in an activity.An activity has a circle imageview at the center. While I swipe 1st Fragment,I need to show 1st image & same as for 2nd tab swipe with different image.
UPDATE:
static class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
Bundle bundle = new Bundle();
bundle.putInt("position", position);
switch (position) {
case 0:
imgProfile.setImageResource(R.drawable.login_logo);
return new LogInFragment();
case 1:
imgProfile.setImageResource(R.drawable.add_image);
return new SignUpFragment();
}
return null;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "LOGIN";
case 1:
return "SIGNUP";
}
return null;
}
}

ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
if (i == 0) {
ImageView.setBackgroundResource(R.drawable.image1);
}
else if (i ==1) {
ImageView.setBackgroundResource(R.drawable.image2);
}
}
});

Ok now I understand what you want to do:
First delete all the code where you change the image and try this:
static class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
Bundle bundle = new Bundle();
switch (position) {
case 0:
yourImageView.setImageResource(R.id.yourResource1);
fragment = new LogInFragment();
break;
case 1:
yourImageView.setImageResource(R.id.yourResource2);
fragment = new SignUpFragment();
break;
}
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
String str = "";
switch (position) {
case 0:
str = "LOGIN";
break;
case 1:
str = "SIGNUP";
break;
}
return str;
}
}
Thats all you need

Finally Got it by using the following code by made some modification in #sasikumar answer.
Instead of put onPageScrollStateChanged I used to get position in onPageSelected
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
int i = 0;
if (position == 0) {
Log.e("Tab Position", String.valueOf(position));
imgProfile.setImageResource(R.drawable.login_logo);
} else if (position == 1) {
Log.e("Tab Position", String.valueOf(position));
imgProfile.setImageResource(R.drawable.add_image);
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});

Related

Fragment onCreateView getting called multiple time so that new instance of fragment is created every time

I am trying to add the fragments on page change of view pager. but every time when i scroll the view pager, new instant of fragment is created.
public void initializeViewPager(int currentPage, String selectedCardNumber) {
currentPosition = currentPage;
FragmentPagerAdapter journeyViewPagerAdapter =
new ViewPagerAdapter(getSupportFragmentManager());
ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//Default Implementation
}
#Override
public void onPageSelected(int position) {
currentPosition = position;
addFragments(position, selectedCardNumber);
handleScrollState();
}
#Override
public void onPageScrollStateChanged(int state) {
//Default Implementation
}
};
viewPager.addOnPageChangeListener(onPageChangeListener);
initCarousalAdapter();
}
addFragments() method
private void addFragments(int position, final String selectedCardNumber) {
FragmentTransaction fragTransaction = getSupportFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putInt(ApplicationConstants.SELECTED__TYPE, position);
bundle.putString(DataConstants.SELECTED__NUMBER, selectedCardNumber);
switch (position) {
case 1:
OffersFragmentOne fragment = OffersFragmentOne.getInstance();
fragment.setArguments(bundle);
fragTransaction.replace(R.id.btmt_fragment_container, fragment);
break;
case 2:
OffersFragmentTwo mtFragment = OffersFragmentTwo.getInstance();
mtFragment.setArguments(bundle);
fragTransaction.replace(R.id.btmt_fragment_container, mtFragment);
break;
case 3:
OffersFragmentThree poFragment = OffersFragmentThree.getInstance();
poFragment.setArguments(bundle);
fragTransaction.replace(R.id.btmt_fragment_container, poFragment);
break;
default:
//Default Implementation
}
fragTransaction.commit();
}
getInstance() method
private static OffersFragmentOne offersFragmentOne;
public static OffersFragmentOne getInstance() {
if (OffersFragmentOne == null) {
offersFragmentOne = new OffersFragmentOne();
}
return offersFragmentOne;
}
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final int[] carousalImages = {R.drawable.ic_po_carousal_banner,
R.drawable.ic_bt_carousal_banner,
R.drawable.ic_mt_carousal_banner, R.drawable.ic_po_carousal_banner,
R.drawable.ic_bt_carousal_banner};
public ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public Fragment getItem(int position) {
CarousalFragment frag = new CarousalFragment();
CarousalFragmentPresenter.createAndAttach(frag, carousalImages[position]);
return frag;
}
#Override
public int getCount() {
return carousalImages != null ? carousalImages.length : 0;
}
}
You should write your switch case in getItem and don't required bundle and fragment transaction because getItem itself returns selected Fragment and will bind with ViewPager
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
CarousalFragment();
break;
case 1:
OffersFragmentOne.getInstance();
break;
case 2:
OffersFragmentTwo.getInstance();
break;
case 3:
OffersFragmentThree.getInstance();
break;
default:
//Default Implementation
}
}
OnPageChangeListener just give you the position of selected and scrolled item or fragment, so don't required to add fragment from onPageSelected
ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//Default Implementation
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
//Default Implementation
}
};
How about creating the fragment instances in the container activity or so of the view pager? That way they should be created only when the container is created.
EDIT: Adding example code.
Example:
public class ContainerActivity extends AppCompatActivity {
private FragmentOne fragmentOne;
private FragmentTwo fragmentTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.container_with_view_pager);
fragmentOne = FragmentOne.getInstance();
fragmentTwo = FragmentTwo.getInstance();
ViewPager viewPager = findViewById(R.id.viewpager);
setupViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(fragmentOne, "Fragment One");
adapter.addFragment(fragmentTwo, "Fragment Two");
viewPager.setAdapter(adapter);
}
}

Center Logo Between Two Tabs

Here the vector of what i'm trying to do
There Will be 2 tabs and one imageview or something between those two tabs, and the logo should be unclickable.
I've tried almost every approach but failed.
Anyone who knows how to do it?
Code :
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs2);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.getTabAt(1).setIcon(R.drawable.logo);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new LeaderBoard();
case 1:
return new Logo();
case 2:
return new Recent_Post();
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "LeaderBoard";
case 1:
return "";
case 2:
return "Recent Posts";
}
return null;
}
}
I've Solved my problem by switching the tabs between each other, Some Calculations.
int currentScreen = 0;
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (position == 1) {
if(currentScreen == 0) {
mViewPager.setCurrentItem(2);
currentScreen = 2;
}else{
mViewPager.setCurrentItem(0);
currentScreen = 0;
}
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});

ViewPager does not scroll fragments

ViewPager does not scroll fragments:
I include ViewPager inside activity.
And create two fragments, but when i scroll view pager (horizintal) only 1rst Framgent selects:
_pageAdapter=new PagerAdapter(getSupportFragmentManager());
_viewPager= (ViewPager) findViewById(R.id.viewPager);
_viewPager.setAdapter(_pageAdapter);
_viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Log.d(LOG_TAG,"!Page #"+position);
}
}
#Override
public void onPageScrollStateChanged(int state) {
if(state==0)
Log.d(LOG_TAG,"Page Iddle");
else if(state==1)
Log.d(LOG_TAG,"Page Dragging");
else if(state==2)
Log.d(LOG_TAG,"Page Settling");
}
});
Here code of PageAdapter:
public class PagerAdapter extends FragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment=null;
switch (position) {
case 0: {
fragment = new FirstFramgent();
Bundle bundle = new Bundle();
bundle.putInt("num", 0);
fragment.setArguments(bundle);
break;
}
case 1: {
fragment = new SecondFragment();
Bundle bundle = new Bundle();
bundle.putInt("num", 1);
fragment.setArguments(bundle);
break;
}
}
return fragment;
}
#Override
public int getCount() {
return 1;
}
}
Tell me, how to make ViewPager scrolls two fragments.
Try this change:
#Override
public int getCount() {
return 2;
}

Viewpager inside a fragment is blank when a listiview item in it is clicked and redirected to another fragment

I am using a Viewpager inside a fragment and the viewpager is having a listview within its fragment.So when a list item is clicked it is redirected to another fragment in the same activity. So when i come back to my previous fragment(viewpager) the viewpager is blank! why is it so
I am desperate for the solution. Any suggestion would be appreciated.
//tabs inside the fragment
public class TabsPagerAdapter extends FragmentStatePagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
/* #Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
return PageFragment.newInstance(position + 1);
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return "TAB " + (position + 1);
}*/
#Override
public Fragment getItem(int position) {
Fragment frag = null;
switch (position) {
case 0:
frag = new Full_Roaster_Fragment();
break;
case 1:
frag = new By_Sport_Fragment();
break;
case 2:
frag = new By_Team_Fragment();
break;
case 3:
frag = new Video_Critique_Fragment();
break;
}
return frag;
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
String title=" ";
switch (position){
case 0:
title="Full Roaster";
break;
case 1:
title="By Sport";
break;
case 2:
title="By Team";
break;
case 3:
title="For Video Critique";
break;
}
return title;
}
}
//Child Fragment of Viewpager
public class Full_Roaster_Fragment extends Fragment implements AdapterView.OnItemClickListener {
private View rootView;
public static ListView Analystslist;
public static FullRoasterAdapter full_Roaster_adapter;
public static ArrayList<ics.com.ics_app.Beans.AnalystSearchBean.Teams> analysts_search_arraylist1 = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_full_roaster, container, false);
initialization();
full_Roaster_adapter = new FullRoasterAdapter(getActivity(), ApplicationGlobalClass.analysts_search_arraylist);
full_Roaster_adapter.notifyDataSetChanged();
Analystslist.setAdapter(full_Roaster_adapter);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
full_Roaster_adapter = new FullRoasterAdapter(getActivity(), ApplicationGlobalClass.analysts_search_arraylist);
full_Roaster_adapter.notifyDataSetChanged();
Analystslist.setAdapter(full_Roaster_adapter);
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "Full Roaster on Activity Created", Toast.LENGTH_SHORT).show();
// Populates list with our static array
full_Roaster_adapter = new FullRoasterAdapter(getActivity(), ApplicationGlobalClass.analysts_search_arraylist);
full_Roaster_adapter.notifyDataSetChanged();
Analystslist.setAdapter(full_Roaster_adapter);
Log.e("sizeee",String.valueOf(ApplicationGlobalClass.analysts_search_arraylist.size()));
}
private void initialization() {
Analystslist = (ListView)rootView.findViewById(R.id.Analysts_listview);
Analystslist.setOnItemClickListener(this);
}
#Override
public void onResume() {
super.onResume();
// if (ApplicationGlobalClass.flag) {
Toast.makeText(getActivity(), "full Roaster on Resume", Toast.LENGTH_SHORT).show();
full_Roaster_adapter = new
..
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ApplicationGlobalClass.user_id=ApplicationGlobalClass.analysts_search_arraylist.get(i).get(i).getId();
Log.e("userid",String.valueOf(ApplicationGlobalClass.user_id));
Log.e("userid2",String.valueOf(ApplicationGlobalClass.analysts_search_arraylist.get(i).get(i).getId()));
//here new fragment is calling from this fragment
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new FlexibleSpaceWithImageWithViewPagerTab2Activity()).addToBackStack("profile1").commit();
}
You need to override onBackPressed() in the activity that's hosting the fragments like this:
public void onBackPressed() {
int = count = getFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
//additional code
} else {
getFragmentManager().popBackStack();
}

How can I set fragment Id in some of the override method of FragmentPagerAdapter?

I have a FragmentPagerAdapter, and in the fragment class I have a public method that I need to use from main activity.
In this point I don't know how to access the fragment that contains that method. Can I modify something on the class below to set id or something to fragments?
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Fragment_1();
case 1:
return new Fragment_2();
case 2:
return new Fragment_3();
}
return null;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.fg_1).toUpperCase(l);
case 1:
return getString(R.string.fg_2).toUpperCase(l);
case 2:
return getString(R.string.fg_3).toUpperCase(l);
}
}
return null;
}
}
How can I set a Fragment id, or Fragment tag or something to acces the fragment later?
Any other way to access fragment methods could be good.
EDITED WITH MORE INFORMATION:
I'm really lost with this. But, I have a ViewPager too, these fragments are associated to actionBar Tabs.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
}
Is there any way to communicate with the fragments?
Like so :
switch (position) {
case 0:
Fragment f = new Fragment_1();
f.setId(...);
return f;
case 1:
...
}
This assumes you have a setId method in your fragment.
What I do however is define a static string TAG per fragment, I can then access it with Fragment_1.TAG.

Categories

Resources