How i can update data in my case
public class SpaFragment extends Fragment {
public SpaFragment(){}
private SpaList mSpa;
private TabHost tabs;
private View rootView;
private ViewPager pager;
private PagerAdapter pagerAdapter;
public void setSpa(SpaList s)
{
mSpa=s;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.spa_tab_fragment, container, false);
FragmentManager f = ((FragmentActivity) getActivity()).getSupportFragmentManager();
pagerAdapter = new MyPagerAdapter( f );
pager = (ViewPager) rootView.findViewById(R.id.pager);
pager.setAdapter(pagerAdapter);
pagerAdapter.notifyDataSetChanged();
Log.d(""+mSpa.getSpaName(),"");
return rootView;
}
private class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public CharSequence getPageTitle(int position) {
Resources res = getResources();
String[] title = res.getStringArray(R.array.title_spa_viewpager_array);
return title[position];
}
#Override
public android.support.v4.app.Fragment getItem(int pos) {
switch(pos) {
case 0: return FirstFragment.newInstance(mSpa.getSpaName());
case 1: return SecondFragment.newInstance(mSpa.getSpaAddr());
case 2: return SecondFragment.newInstance(mSpa.getCityEngName());
default: return FirstFragment.newInstance(mSpa.getSpaPhone());
}
}
#Override
public int getCount() {
return 3;
}
}
}
and fragment code
public class FirstFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.viewpager_tab_text, container, false);
TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
tv.setText(getArguments().getString("msg"));
return v;
}
public static FirstFragment newInstance(String text) {
FirstFragment f = new FirstFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}
Related
I have 3 tabs each are sharing a single fragment. But this fragment contains a viewPager. So all the three tabs will have a UI with a viewPager and viewPager have three different fragments. After following the examples available I managed to implement Tab layout with single fragment. But when I tried to implement a viewPager inside this fragment I am facing issue. Tab is not scrolling smoothly and its not moving to third tab. I am also not able to see my viewPager fragment.
Main Fragment with tablayout implementation:
public class DashboardFragment extends android.support.v4.app.Fragment {
Activity mActivity;
private TabLayout tabLayout;
private ViewPager viewPager;
private static final String TAG = "DashboardFragment";
public DashboardFragment() {
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = getActivity();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dashboard_fragment, container, false);
tabLayout = (TabLayout)view.findViewById(R.id.tabs);
viewPager = (ViewPager)view.findViewById(R.id.viewpager);
tabLayout.addTab(tabLayout.newTab().setText("Yesterday"));
tabLayout.addTab(tabLayout.newTab().setText("Last Week"));
tabLayout.addTab(tabLayout.newTab().setText("This Month"));
setupViewPager(viewPager);
return view;
}
private void setupViewPager(ViewPager viewPager) {
DashboardTabPagerAdapter adapter = new DashboardTabPagerAdapter(((AppCompatActivity)getActivity()).getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
DashboardPagerAdapter:
public class DashboardTabPagerAdapter extends FragmentStatePagerAdapter {
private int mNoOfTabs;
public DashboardTabPagerAdapter(FragmentManager fm, int tabCount) {
super(fm);
mNoOfTabs=tabCount;
}
#Override
public Fragment getItem(int position) {
DashboardYesterdayFragment fragment = new DashboardYesterdayFragment();
Bundle args = new Bundle();
switch (position) {
case 0:
args.putSerializable(AppConstants.TAB_DASHBOARD_TYPE, AppConstants.TAB_DASHBOARD_YESTERDAY);
break;
case 1:
args.putSerializable(AppConstants.TAB_DASHBOARD_TYPE, AppConstants.TAB_DASHBOARD_LAST_WEEK);
break;
case 2:
args.putSerializable(AppConstants.TAB_DASHBOARD_TYPE, AppConstants.TAB_DASHBOARD_THIS_MONTH);
break;
default:
return null;
}
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return mNoOfTabs;
}
}
TabFragment which contains a childViewPager:
public class DashboardYesterdayFragment extends android.support.v4.app.Fragment {
private String title;
private Bundle bundle;
ViewPager mViewPager;
public DashboardYesterdayFragment(){
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
bundle = getArguments();
}
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dashboard_yesterday_layout, container, false);
mViewPager = view.findViewById(R.id.vpPager);
setupViewPager(mViewPager);
return view;
}
private void setupViewPager(ViewPager viewPager) {
DashboardViewPagerAdapter adapter = new DashboardViewPagerAdapter( getChildFragmentManager(),bundle);
mViewPager.setAdapter(adapter);
mViewPager.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) {
}
});
mViewPager.setPageMargin(24);
}
ChildViewPagerAdapter:
public class DashboardViewPagerAdapter extends FragmentStatePagerAdapter {
private static int NUM_ITEMS = 3;
private Bundle bundle;
public DashboardViewPagerAdapter(FragmentManager fm, Bundle bundle) {
super(fm);
this.bundle=bundle;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return IncidentsFragment.newInstance(bundle);
case 1:
return VehicleUtilizationFragment.newInstance(bundle);
case 2:
return FleetPerformanceFragment.newInstance(bundle);
default:
return null;
}
}
#Override
public int getCount() {
return NUM_ITEMS;
}
}
ChildViewPager first fragment:
public class IncidentsFragment extends Fragment {
private TextView mOverSpeeding,mGeoFence,mIdlingCase,mFuelFilling,mMosetIncidences, mLeastIncidences;
public static IncidentsFragment newInstance(Bundle bundle) {
IncidentsFragment fragmentFirst = new IncidentsFragment();
Bundle args = new Bundle();
args.putBundle("bundle", bundle);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.incident_layout, container, false);
initViews(view);
return super.onCreateView(inflater, container, savedInstanceState);
}
private void initViews(View view) {
mOverSpeeding = (TextView)view.findViewById(R.id.over_speeding_value);
mGeoFence = (TextView)view.findViewById(R.id.geofence_value);
mIdlingCase = (TextView)view.findViewById(R.id.idling_value);
mFuelFilling = (TextView)view.findViewById(R.id.fuel_value);
mMosetIncidences = (TextView)view.findViewById(R.id.vehicle_reg_no);
mLeastIncidences = (TextView)view.findViewById(R.id.least_incidences_value);
}
}
I am confused what is going wrong.
Change this:
DashboardTabPagerAdapter adapter = new DashboardTabPagerAdapter(((AppCompatActivity)getActivity()).getSupportFragmentManager(),tabLayout.getTabCount());
to this:
DashboardTabPagerAdapter adapter = new DashboardTabPagerAdapter(((AppCompatActivity)getActivity()).getChildFragmentManager(),tabLayout.getTabCount());
I have a Viewpager contains Recylerview which has 2 different holders. One of them is also view pager. Hence I have nested viewpager nad nested fragments. When I tap the second tab and tap back the first tab it is fine .But when I tap the third tab and tap back the first tab. It gives following error:
java.lang.IllegalArgumentException: No view found for id 0x7f0f00ce (:id/viewPagerComments) for fragment CommentFragment
MainAcitivty
viewPager.setAdapter(new MainViewPagerAdapter(getSupportFragmentManager(),models.size()));
navigationTabBar.setModels(models);
navigationTabBar.setViewPager(viewPager, 0);
MainViewPagerAdapter
private int size;
public MainViewPagerAdapter(FragmentManager fm, int size) {
super(fm);
this.size = size;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return DashboardtFragment.newInstance(size, position);
default:
return TempFragment.newInstance(size, position);
}
}
DashboardtFragment
public static DashboardtFragment newInstance(int size, int index) {
Bundle args = new Bundle();
args.putInt(ARG_I,index);
args.putInt(ARG_S,size);
DashboardtFragment fragment = new DashboardtFragment();
fragment.setArguments(args);
return fragment;
}
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
final View v = inflater.inflate(R.layout.dashboard_fragment, null);
return v;
}
#Override
public void showRecyleview(DashboardResponseModel model) {
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
DashboardRecyleViewAdapter adapter = new DashboardRecyleViewAdapter(getActivity(),getChildFragmentManager(),model);
recyclerView.setAdapter(adapter);
}
DashboardRecyleViewAdapter
public static class ViewHolder1 extends RecyclerView.ViewHolder {
public ViewHolder1(View itemView) {
super(itemView);
viewPager = itemView.findViewById(R.id.viewPagerComments);
}
}
case 1 :
return new ViewHolder1( layoutInflater.inflate(R.layout.dashboard_comments, parent,
false));
case 1:
ViewHolder1 viewHolder1 = (ViewHolder1) holder;
viewPager.setAdapter(new CommentViewPagerAdapter(
childFragmentManager,
model.getCommentList().size(),
model.getCommentList()
));
break;
CommentViewPagerAdapter
public CommentViewPagerAdapter(FragmentManager fm, int size,
List<CommentList> list) {
super(fm);
this.size = size;
this.list=list;
}
#Override
public Fragment getItem(int position) {
return CommentFragment.newInstance(size,position,list.get(position));
}
CommentFragment
public static CommentFragment newInstance(int size, int index, CommentList
comment) {
Bundle args = new Bundle();
args.putString(ARG_C,new Gson().toJson(comment));
args.putInt(ARG_I,index);
args.putInt(ARG_S,size);
CommentFragment fragment = new CommentFragment();
fragment.setArguments(args);
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState)
{
final View v = inflater.inflate(R.layout.comment_content, null);
}
PagerAdapter mPagerAdapter;
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
super.setAdapter(null);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (mPagerAdapter != null) {
super.setAdapter(mPagerAdapter);
}
}
#Override
public void setAdapter(PagerAdapter adapter) {
}
public void storeAdapter(PagerAdapter pagerAdapter) {
mPagerAdapter = pagerAdapter;
}
first create a customviewpager than add the above code. Use storeAdapter instead of setAdapter.
I have a fragment that Contains view pager.there are three fragments in that Main fragment they are Fragment A,Fragment B,Fragment C. Upto this code runs properly.
Then i added a new Fragment and called via Fragment A then i got the error in the Log
No view found for id 0x7f0d0083 (com.app.sanyog:id/frame) for fragment SearchPlaces{289bf4e #0 id=0x7f0d0083}
Main Fragment:
/**
* Created by Admin on 04-06-2015.
*/
public class ContentFragment_Flight_Bkg extends Fragment {
private View view;
public ViewPager pager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_fragment_flight,container,false);
TabsPagerAdapter adapter = new TabsPagerAdapter(getFragmentManager());
pager = (ViewPager)view.findViewById(R.id.pager);
pager.setAdapter(adapter);
return view;
}
}
Pager Adapter
public class TabsPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
// Tab Titles
private String tabtitles[] = new String[] { "One Way", "Round Trip", "Multicity" };
Context context;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
switch (position) {
// Open FragmentTab1.java
case 0:
OneWayTab fragmenttab1 = new OneWayTab();
return fragmenttab1;
// Open FragmentTab2.java
case 1:
RoundTripTab fragmenttab2 = new RoundTripTab();
return fragmenttab2;
// Open FragmentTab3.java
case 2:
MultiCityTab fragmenttab3 = new MultiCityTab();
return fragmenttab3;
}
return null;
}
#Override
public CharSequence getPageTitle(int position) {
return tabtitles[position];
}
}
Fragment A
public class OneWayTab extends Fragment {
private LinearLayout oneway_source,oneway_destiantion;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab3.xml
View view = inflater.inflate(R.layout.content_one_way_tab, container, false);
oneway_source = (LinearLayout)view.findViewById(R.id.oneway_source_layout);
oneway_destiantion = (LinearLayout)view.findViewById(R.id.oneway_destination_layout);
oneway_source.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SearchPlaces fragment_Flight= new SearchPlaces();
android.support.v4.app.FragmentTransaction fragmentTransaction_Flight = getChildFragmentManager().beginTransaction();
fragmentTransaction_Flight.replace(R.id.frame,fragment_Flight,"FLight_booking");
fragmentTransaction_Flight.commit();
}
});
oneway_destiantion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Utils.showToast(getActivity(),"Destiantion");
}
});
return view;
}
}
New Fragment (Called from Fragment A)
public class SearchPlaces extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab1.xml
View view = inflater.inflate(R.layout.content_search_places, container, false);
return view;
}
}
I have a ViewPager that I want to use same Fragment in the each page with different variables. But when I do this, only one page of ViewPager is viewing and other pages are blank. How can i do this? I can't create different layout for each tab because I want to add them dynamically.
ViewPagerAdapter:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
class FragmentStoreTabPageAdapter extends FragmentPagerAdapter {
String[] pageTitles = {"Y","Z","X"};
public FragmentStoreTabPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return FragmentStorePages.newInstance(pageTitles[position],position);
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return pageTitles[position];
}
}
Main Fragment that contains the ViewPager:
public class FragmentStore extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_store,container,false);
ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager_store);
viewPager.setAdapter(new FragmentStoreTabPageAdapter(getChildFragmentManager()));
TabPageIndicator tpi = (TabPageIndicator) view.findViewById(R.id.indicator_store);
tpi.setViewPager(viewPager);
return view;
}
}
The Fragment that uses in ViewPager:
public class FragmentStorePages extends Fragment implements ReplaceListener {
String categoryName;
int categoryId;
public static FragmentStorePages newInstance(String categoryName, int categoryId){
FragmentStorePages fsp = new FragmentStorePages();
Bundle args = new Bundle();
args.putString("categoryName", categoryName);
args.putInt("categoryId", categoryId);
fsp.setArguments(args);
return fsp;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_store_pages,container,false);
Bundle args = getArguments();
categoryName = args.getString("categoryName");
categoryId = args.getInt("categoryId");
getFragmentManager().beginTransaction().replace(R.id.fragment_store_pages,FragmentStoreSubCategories.newInstance(categoryName),"SubCategoryFragment").commit();
return view;
}
#Override
public void onReplace(Bundle args) {
}
}
Use This PagerAdapter
class FragmentStoreTabPageAdapter extends FragmentPagerAdapter {
String[] pageTitles = {"Y","Z","X"};
public FragmentStoreTabPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
FragmentStorePages fsp = new FragmentStorePages();
Bundle args = new Bundle();
args.putString("categoryName", categoryName);
args.putInt("categoryId", categoryId);
fsp.setArguments(args);
return fsp;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return pageTitles[position];
}
}
Use This Fragment
public class FragmentStorePages extends Fragment implements ReplaceListener {
String categoryName;
int categoryId;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_store_pages,container,false);
Bundle args = getArguments();
if(args!=null && args.containsString("categoryName")&& args.containsString("categoryId")){
categoryName = args.getString("categoryName");
categoryId = args.getInt("categoryId");
}
return view;
}
#Override
public void onReplace(Bundle args) {
}
}
I have an application where I'm using a ViewPager to slide between two Fragments. I can slide between two Fragments perfectly; but now I need to slide only half of the fragment. How to do that? Here is what I've tried:
This my MainActivity
public class MainActivity extends FragmentActivity
{
MyPageAdapter pageAdapter;
Context c;
int[] pics = {R.drawable.ic_launcher};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c = MainActivity.this;
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), c, pics);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
}
}
PageAdapter.java
class MyPageAdapter extends FragmentPagerAdapter {
Context c;
int[] pics;
Fragment fragment;
public MyPageAdapter(android.support.v4.app.FragmentManager fm, Context c, int[] pics) {
super(fm);
this.c =c;
this.pics = pics;
}
#Override
public Fragment getItem(int position) {
switch(position){
case 0:return new MyFragment();
case 1: return new NewFragment();
default: return new MyFragment();
}
}
#Override
public int getCount() {
return 2;
}
}
MyFragment.java
public class MyFragment extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static final MyFragment newInstance(String message,int a)
{
MyFragment f = new MyFragment();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment_layout, container, false);
TextView messageTextView = (TextView)v.findViewById(R.id.textView);
messageTextView.setText("hi");
v.setBackgroundColor(Color.parseColor("#ff0000"));
return v;
}
}
NewFragment.java
public class NewFragment extends Fragment {
public static final MyFragment newInstance(String message,int a)
{
MyFragment f = new MyFragment();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.new_fragment, container, false);
Button b1 = (Button) v.findViewById(R.id.b1);
v.setBackgroundColor(Color.parseColor("#0000ff"));
return v;
}
}
Override the getPageWidth method of PagerAdapter and make it return 0.5f;