I am trying to create an intro slider screen using fragments and ViewPager. I want to display multiple layouts in onCreateView method but don't know how to do it.
Any help please?
Main activity
public class MainActivity extends AppCompatActivity {
public ViewPager pager;
public The_fragment_adapter the_fragment_adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager) findViewById(R.id.pager);
the_fragment_adapter = new The_fragment_adapter(getSupportFragmentManager());
pager.setAdapter(the_fragment_adapter);
}
}
Fragment Class
public class The_fragment extends Fragment {
public The_fragment(){
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View my_view=inflater.inflate(R.layout.theui,container,false);
return my_view;
}
}
Adapter View
public class The_fragment_adapter extends FragmentStatePagerAdapter {
public int[] the_layouts = {R.layout.page1,R.layout.page2};
public The_fragment_adapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new The_fragment();
}
#Override
public int getCount() {
return the_layouts.length;
}
}
A ViewPager is for paging multiple fragments, not layouts. The getItem() method should return the fragment you want for the position you want.
Create a second fragment with the layout you want:
public Fragment The_second_fragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View my_view=inflater.inflate(R.layout.page2,container,false);
return my_view;
}
}
Then change your adapter methods:
#Override
public Fragment getItem(int position) {
switch 0:
return new The_fragment();
switch 1:
return new The_second_fragment();
}
#Override
public int getCount() {
return 2;
}
See the excellent tutorial in the Android Developer's Guide. And, in the future if you follow the Java naming conventions you are more likely to get a better answer as other programmers will be able to understand your code more easily.
Related
I am trying to refresh the current fragment in the tabbed layout of my viewpager. I have one fragment (UserProgressFragment) that creates the ViewPager and sets the adapter (UserProgressAdapter) along with creating tabbed layout.
In my UserProgressAdapter, in the getItem() method I am returning two other fragments, (UserWeightTrackerFragment and UserCalorieCounterFragment) based on which tab i am on.
My issue is how do i refresh the fragment and update its content/view from the UserCalorieCounterFragment on a button click, because access to the viewpager and adapter are set in the UserProgressFragment? I have considered notifyDataChange for the adapter but i dont know how to call this from this class as it is set up on the UserProgressFragment class. The purpose of wanting to refresh this page is to update a specific view which is a chart, if context is needed.
I have attached the code below:
UserProgressFragment
public class UserProgressFragment extends Fragment{
public static UserProgressFragment newInstance() {
return new UserProgressFragment();
}
public UserProgressFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_user_progress, container, false);
ViewPager viewPager = view.findViewById(R.id.progress_viewpager);
viewPager.setAdapter(new UserProgressTabsAdapter(getChildFragmentManager()));
TabLayout tabLayout = view.findViewById(R.id.progress_sliding_tabs);
tabLayout.setupWithViewPager(viewPager, true);
return view;
}
}
UserProgressTabsAdapter
public class UserProgressTabsAdapter extends FragmentStatePagerAdapter {
private static final int PAGE_COUNT = 2;
private String tabTitles[] = new String[]{"Weight Tracker", "Calorie Counter"};
public UserProgressTabsAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return UserWeightTrackerFragment.newInstance(position);
case 1:
return UserCalorieCounterFragment.newInstance(position + 1);
}
return null;
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
UserCalorieCounterFragment (need to refresh this one)
public class UserCalorieCounterFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemSelectedListener {
public static final String ARG_PAGE = "ARG_PAGE";
public static UserCalorieCounterFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
UserCalorieCounterFragment fragment = new UserCalorieCounterFragment();
fragment.setArguments(args);
return fragment;
}
public UserCalorieCounterFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int mPage = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_user_calorie_counter, container, false);
Button mAddCalories = view.findViewById(R.id.btn_add_calorie);
mAddCalories.setOnClickListener(this);
return view;
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_add_calorie:
//REFRESH/UPDATE HERE
break;
}
}
You can create a public method in your fragment which contains the view pager which will contain the notifyDataSetChanged() function. And in the view pager fragment/item, you can call getParentFragment() and type cast it to your parent/first fragment and access that public method to notifyDataSetChanged().
Below is my Code.
Using ViewPager I have made 2 XML file for two Pages and their Class file.
Now I need if I click on First Screen of ViewPager, a new activity should launch.
I got 2 pages, so If I Click First Screen, A.class intent Called. If I click on Second Screen, B.class intent should be Called.
Codes:-
MainActvity:
public class MainActivity extends ActionBarActivity {
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.pager);
/** set the adapter for ViewPager */
mViewPager.setAdapter(new SamplePagerAdapter(
getSupportFragmentManager()));
}
/** Defining a FragmentPagerAdapter class for controlling the fragments to
be shown when user swipes on the screen. */
public class SamplePagerAdapter extends FragmentPagerAdapter {
public SamplePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
/** Show a Fragment based on the position of the current screen */
if (position == 0) {
return new SampleFragment();
} else
return new SampleFragmentTwo();
}
#Override
public int getCount() {
// Show 2 total pages.
return 2;
}
}
}
SampleFragment.java
public class SampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one, null);
return rootView;
}
}
SampleFragmentTwo.java
public class SampleFragmentTwo extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_two, container,
false);
return rootView;
}
}
CustomSwipeAdapter:
public class CustomSwipeAdapter extends PagerAdapter {
#Override
public int getCount() {
return 0;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
}
As mentioned in one of the comments, In your fragment classes A and B put onClick listener on views and launch intent from there like below:
public class SampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one, null);
rootView.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// do something when the button is clicked
Intent activityA = new Intent(getActivity(),ActivityA.class);
startActivity(activityA);
}
return rootView;
}
}
similarlly goes for your second fragment.
I'm having an issue with the support library ViewPager. That ViewPager lives inside a fragment and it's composed of 3 tabs: one that will show some information, the second and third show a list of elements (so they both are beeing generated from the same fragment). When I scroll from the first to the second one everything works fine but if I try to scroll from the second one to the third something happens to the fragments and they don't show up again even the PagerTabStrip disappears when this happens. Also I tryed using the same type of fragment for the 3 tabs (the one that disappeared with the list)and everything seems to work fine, so I'm quite bugged about this. Also, the only log on the console related to the issue is this one:
W/FragmentManager: moveToState: Fragment state for StoreListFragment{3fb980e7 #2 id=0x7f0d0080 android:switcher:2131558528:2} not updated inline; expected state 3 found 2
This is the code for my parent Fragment:
public class StoreFragment extends Fragment {
#Bind(R.id.pager) ViewPager mPager;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_store, container, false);
ButterKnife.bind(this, v);
setupViewPager();
return v;
}
private void setupViewPager() {
String[] titles = {getString(R.string.store_information),
getString(R.string.store_offers),
getString(R.string.store_products)
};
mPager.setAdapter(new StoresPagerAdapter(getChildFragmentManager(), titles));
}
}
This is the code for the PagerAdapter:
public class StoresPagerAdapter extends FragmentPagerAdapter {
private String[] mPageTitles;
public StoresPagerAdapter(FragmentManager fm, String[] titles) {
super(fm);
mPageTitles = titles;
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return StoreInfoFragment.newInstance();
//return StoreListFragment.newInstance(position);
case 1:
return StoreListFragment.newInstance(position);
case 2:
return StoreListFragment.newInstance(position);
default:
return null;
}
}
#Override
public int getCount() {
return mPageTitles != null ? mPageTitles.length : 0;
}
#Override
public CharSequence getPageTitle(int position) {
return mPageTitles[position];
}
}
And the one for the Fragment:
public class StoreListFragment extends Fragment implements MyListListener {
#Bind(R.id.store_list) RecyclerView mStoreContentView;
private ArrayList<StoreModel> mStoreContents = new ArrayList<>();
public static StoreListFragment newInstance(int page) {
return new StoreListFragment();
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mStoreContents.add(new Schedule());
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_store_list, container, false);
ButterKnife.bind(this, v);
return v;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupList();
}
protected void setupList() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
mStoreContentView.setHasFixedSize(true);
mStoreContentView.setLayoutManager(layoutManager);
mStoreContentView.setAdapter(new ArrayAdapter(this, mStoreContents, R.layout.list_elem_locations));
}
#Override
public void onClickElement(int elementId, String elementName) {
Intent i = new Intent(this.getActivity(), DetailActivity.class);
startActivity(i);
}
}
I think your problem is here:
public static StoreListFragment newInstance(int page) {
return new StoreListFragment();
}
You're not using this page anywhere and you're getting two completely same StoreListFragments. You should add:
Bundle args = new Bundle();
args.putInt("key", page);
fragment.setArguments(args);
And then use this differentiation somewhere.
I have been looking and having a hard time finding a clear cut example? I am trying to understand how to create a viewpager within a fragment that is open as a drawer item from my mainActivity...
This is my attempt but I think I am doing something wrong whether it be that I am including it incorrectly or have it in the wrong spot...
public class RandomFragment extends android.app.Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_random, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.random_pager);
mViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 4;
}
#Override
public Fragment getItem(int position) {
Bundle args = new Bundle();
args.putInt(TextViewFragment.POSITION_KEY, position);
return TextViewFragment.newInstance(args);
}
}
I use this one hope it help you too : https://github.com/thecodepath/android_guides/wiki/ViewPager-with-FragmentPagerAdapter
So you can use this one: https://github.com/astuetz/PagerSlidingTabStrip
I am using View pager to create a swipe view effect
Now i want to implement different fragments on same activity fragments are different layout which are displayed according to the condition.
I am stuck on how to create pagerAdapter and how to create fragment class.
Code:
public class mFragment extends Fragment{
Context mContext;
public mFragment(Context context){
mContext = context;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main, ActivityScreen.mViewPager,false);
}
}
public class mPagerAdapter extends FragmentPagerAdapter
{
int i=-1;
public mPagerAdapter(FragmentManager fm)
{
super(fm);
mViewPager.removeAllViews();
}
#Override
public int getCount()
{
return data.size();
}
#Override
public Fragment getItem(int pos) {
return new mFragment(Context ctx);
}
}
Please help me on this
Override #instantiateItem method and return the fragments to update in the view
use this link
will be helpful m i think
in your FragmentActivity add an adapter for FragmentPagerAdapter then
#Override Fragment getItem and add Fragments to it accourding to requirement