I am designing a application in which i am facing problem,
I have a String date; in MainActivity.class and there are two fragments.
InboundDate & OutboundDate
I designed it by TabLayout & ViewPager, I am getting issue when i sending date to fragments & have to call WebServices by POST request on both the fragments.
Below is the date i have in MainActivity.class, How i send this date to both the fragments & call jsonRequest and set Recyclerview to both the fragments Or if i call the jsonRequest to MainActivity itself then how can i display to both the fragments.
date = getIntent().getStringExtra("DATE");
Log.e("DATE",""+date);
ViewPager & TabLayout Code:
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new InBoundDate(),"INBOUND");
adapter.addFrag(new OutBoundDate(),"OUTBOUND");
viewPager.setAdapter(adapter);
}
//viewPager adapter ----- inbound & outbound
public 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 android.support.v4.app.Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(android.support.v4.app.Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
I got the answer of this question by searching over the internet.
In this question i have to send string to MainActivity.java to fragments present on same Activity.
Note : We have to create bundle object & have to send string(any dataType) by bundle.
I have two fragments InBoundDate & OutBoundDate.
In MainActivity.java in setupViewpager ()
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
//bundle
bundle =new Bundle();
bundle.putString("DATE","2017-10-19");
InBoundDate frag_in = new InBoundDate();
OutBoundDate frag_out = new OutBoundDate();
frag_in.setArguments(bundle);
frag_out.setArguments(bundle);
adapter.addFrag(frag_in,"INBOUND");
adapter.addFrag(frag_out,"OUTBOUND");
viewPager.setAdapter(adapter);
}
ViewPager Adapter :
//viewPager adapter ----- inbound & outbound
public 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 android.support.v4.app.Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(android.support.v4.app.Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
In your Fragments get String by this way :
String dateTxt;
private Bundle bundle;
//within OncreateView
bundle = this.getArguments();
dateTxt = bundle.getString("DATE");
Log.e("DATE_FRAG_INBOUND",""+dateTxt);
Related
I'm trying to do an activity with tabs and pagerview
I found this code in a blog but it does not come like making the tabs have icons instead of text ... I put the code:
ViewPagerAdapter.java
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 addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Tablayout_in_Android.java
public class Tablayout_in_Android extends Fragment {
public Tablayout_in_Android() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tablayout_in__android, container, false);
}
}
Method onCreate main class
viewPager = (ViewPager) findViewById(R.id.viewpager);
addTabs(viewPager);
tabLayout = (TabLayout) findViewById(R.id.pestañas);
tabLayout.setupWithViewPager(viewPager);
}
private void addTabs(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new Tablayout_in_Android(), "ONE");
adapter.addFrag(new Tablayout_in_Android(), "TWO");
adapter.addFrag(new Tablayout_in_Android(), "THREE");
viewPager.setAdapter(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 addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
how can i change text for icons? Thanks
Try this:
tabLayout.setupWithViewPager(mViewPager);
for (int i = 0; i < mTabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setIcon(R.drawable.your_icon);
}
To expand on Jonathan's answer, you can also have a list of icons to show in the tabs. Ensure the icons match the number of tabs you have.
tabLayout.setupWithViewPager(mViewPager);
int[] tabIcons = {
R.drawable.ic_icon_1,
R.drawable.ic_icon_2,
R.drawable.ic_icon_3
};
for(int i=0; i<tabLayout.getTabCount(); i++){
if(tabLayout.getTabAt(i) != null){
tabLayout.getTabAt(i).setIcon(tabIcons[i]);
}
}
Also, you may want to have the icon and tab label to be in a horizontal orientation, the use app:tabInlineLabel="true" in the TabLayout's xml layout
i'm using viewpager and FragmentPagerAdapter in my application . in the first activity , this is my code :
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setScrollPosition(2,0f,true);
viewPager.setCurrentItem(2);
private void setupViewPager(ViewPager viewPager) {
Bundle bundle = new Bundle();
bundle.putString("id",id);
Fragment_contacts contact=new Fragment_contacts();
contact.setArguments(bundle);
Fragment_info info=new Fragment_info();
info.setArguments(bundle);
Fragment_Menu menu=new Fragment_Menu() ;
menu.setArguments(bundle);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(contact, "تماس با ما");
adapter.addFragment(info, "درباره ما");
adapter.addFragment(menu, "منو");
viewPager.setAdapter(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);
}
}
I'm using setCurrentItem because my application is no in english and the default selected tab should be on the right .
When I use setCurrentItem , the Fragment_contacts is not loading and it's not showing any thing , but when I remove setCurrentItem , it loads perfectly.
what is the problem ? how can I solve this ?
Your ViewPager will take some time at initialization before you could use setCurrentItem. So set it after the intialization of your ViewPager by calling it in a thread, like this:
viewPager.post(new Runnable() {
#Override
public void run() {
viewPager.setCurrentItem(2);
}
});
Instead of keeping reference to your fragment in your activity, try to do the same in your ViewPager
The view pager is probably already initialized when you add your item in the list and there is a kind of "synchronization" issue between the two.
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager, Bundle bundle) {
super(manager);
Fragment_contacts contact=new Fragment_contacts();
contact.setArguments(bundle);
Fragment_info info=new Fragment_info();
info.setArguments(bundle);
Fragment_Menu menu=new Fragment_Menu() ;
menu.setArguments(bundle);
adapter.addFragment(contact, "تماس با ما");
adapter.addFragment(info, "درباره ما");
adapter.addFragment(menu, "منو");
}
#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);
}
}
I am using ViewPager,
I have to display more data in two fragments, but it does not show anything.
Is There an alternative layout to do it?
this is layout code:
<android.support.v4.view.ViewPager
android:id="#+id/mainContent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.8">
</android.support.v4.view.ViewPager>
this is adapter:
public class HitoeAdapter extends FragmentPagerAdapter {
private static HitoeAdapter adapter;
private final HeartRateFragment mHeartRateFragment;
private final ECGFragment mECGFragment;
public HitoeAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
mHeartRateFragment = new HeartRateFragment();
mECGFragment = new ECGFragment();
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return mHeartRateFragment;
case 1:
return mECGFragment;
default:
return null;
}
}
#Override
public int getCount() {
return 2;
}
}
Please help me.
Thanks.
Create the adapter class like below--
public 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);
}
}
Now add this method to set your fragments with viewpager -
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new HeartRateFragment(), "HeartRateFrag");
adapter.addFragment(new ECGFragment(), "ECGFragment");
viewPager.setAdapter(adapter);
}
I have Three different Fragments to show using View Pager. I have added the fragment to the adapter as follows. Title shown is correct but on click its picking the wrong fragment
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new AcceptsFragment(), "Accepts");
adapter.addFragment(new AskedFragment(), "Asked");
adapter.addFragment(new HelpedFragment(), "Helped");
viewPager.setAdapter(adapter);
}
Here is my adapter implementation.
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) {
Log.d("Fragment Clicked ",String.valueOf(position));
return mFragmentList.get(position);
}
#Override
public int getCount() {
//return mFragmentList.size();
return mFragmentist.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Problem: I was modifying a small app where three tabs are there in fragment but all refers to text string. I wanted to use layout xml so that I can make good graphics in it. How can I do it in this code:
Information_tab refers to a string where I can put text, but I want a layout.
public class AdapterInformation extends FragmentPagerAdapter {
public AdapterInformation(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
Bundle bundle = new Bundle();
int imgResId = 0;
int tab = 0;
int colorResId = 0;
switch (index) {
case 0:
tab = R.string.information_tab1;
break;
case 1:
tab = R.string.information_tab2;
break;
case 2:
tab = R.string.information_tab3;
break;
}
bundle.putInt("image", imgResId);
bundle.putInt("tab",tab);
bundle.putInt("color", colorResId);
SwipeTabFragment swipeTabFragment = new SwipeTabFragment();
swipeTabFragment.setArguments(bundle);
return swipeTabFragment;
}
#Override
public int getCount() {
return 3;
}}
you can add fragment layout like this.
private void setupViewPager(final ViewPager viewPager) {
final ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "Pending Upload");
adapter.addFragment(new TwoFragment(), "Complete Upload");
viewPager.setAdapter(adapter);
}
and your fragmentPagerAdapter code is like this:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager)
{
super(manager);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#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);
}
}
and also add your setupViewPager method in onCreate()
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
setupViewPager(viewPager)
tabLayout.setupWithViewPager(viewPager);
Note: Do not use getItemPosition() method if its possible.. it
will produce redundancy of your listItems or ArrayList.. bcz every
time you swipe to other layout it will refresh page automatically..
its good if want this facility.. but you have write some code
regarding to handle this method.. and its also going to slow your
swipe animation if you have large amount of data.
For more information about tab layout and fragment: you can refer this one:
http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/