Swipe Between Two Fragments with Re-Creating it - android

I have Two Fragments one will Send integers to the Sql database, and the other will display them both fragments are working fine. However, when I send an integer to the database and I swipe to the display fragment it will not refresh until push a button, I would like to make it automatically once I move to the fragment please:
Here is my Main Activity:
public class MainActivity extends AppCompatActivity {
int index;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
ViewPager mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.getAdapter().notifyDataSetChanged();
mViewPager.setOffscreenPageLimit(0);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
//------------- Intlize Fragment ----//
Dash dash = new Dash();
getSupportFragmentManager().beginTransaction().add(R.id.dashfragment, dash).commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent i = new Intent(this, AboutMe.class );
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
Dash dash = new Dash();
return dash;
case 1:
Caluclate caluclate = new Caluclate();
return caluclate;
case 2:
AskMe askMe = new AskMe();
return askMe;
case 3:
TeamPicker teamPicker = new TeamPicker();
return teamPicker;
}
return null;
}
i read many solutions but i couldn't figure it out.

You could do something like
if (fragment.isVisible()) {
refreshItFirst;
}
BTW why take a fragment for insertions of Db - there is a helper class?
The method isVisible() shoud help you.
Look at this post Replace Fragment inside a ViewPager

Related

Moving EditText input from an Activity to a Fragment of a swipe view

This is the MainActivity upon launch. The user puts in a number then clicks the button and the Intent brings them to a swipe view with the number they entered.
public class MainActivity extends AppCompatActivity{
String number;
Button continueButton;
EditText NumberET;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
continueButton = (Button) findViewById(R.id.ContinueButton);
NumberET = (EditText) findViewById(R.id.etTotalAmount);
continueButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
number = NumberET.getText().toString();
if (number.equals("")) {
Toast.makeText(getApplicationContext(), "Enter Number", Toast.LENGTH_SHORT).show();
} else {
Bundle bundle = new Bundle();
bundle.putString("AMOUNT_KEY", number);
FixedCosts fragment = new FixedCosts();
fragment.setArguments(bundle);
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.replace(R.id.container, fragment).commit();
Intent intent = new Intent(MainActivity.this, FragmentActivity.class);
intent.putExtra("NUMBER", number);
startActivity(intent);
}
}
});
}
In the fragment, I would like to set the text of a textview to display that number. I cannot get this number to send from this Activity class to the Fragment. How can I fix this?
Fragment:
ublic class FixedCosts extends Fragment {
String amount;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fixed_costs_fragment, container, false);
amount = this.getArguments().getString("AMOUNT_KEY");
TextView TitletextView = (TextView) view.findViewById(R.id.amountTitle);
TitletextView.setText(amount);
return view;}}
Here is the FragmentActivity that sets the swipe view:
public class FragmentActivity extends AppCompatActivity {
public static final String MY_CUSTOM_FRAGMENT_KEY = "I_GOT_THE_KEYS";
String MoneyAmount;
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment);
/*MoneyAmount = getIntent().getExtras().getString("AmountMoney");*/
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
private Fragment createCustomFragment(){
Bundle bundle = new Bundle();
bundle.putString(MY_CUSTOM_FRAGMENT_KEY, MoneyAmount);
PlaceholderFragment placeholderFragment = new PlaceholderFragment();
placeholderFragment.setArguments(bundle);
return placeholderFragment;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber, String MoneyAmount) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
args.putString(MY_CUSTOM_FRAGMENT_KEY, MoneyAmount);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Bundle bundle = getArguments();
/*String amount = bundle.getString(MY_CUSTOM_FRAGMENT_KEY);
TextView TitletextView = (TextView) FixedCosts.class.findViewById(R.id.amountTitle);
TitletextView.setText(amount);*/
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
FixedCosts fixedCosts = new FixedCosts();
return fixedCosts;
}
return PlaceholderFragment.newInstance(position + 1, MoneyAmount);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Fixed Costs";
case 1:
return "Long term investments";
case 2:
return "SECTION 3";
}
return null;
}
}
}
Remove activity start code in continueButton.setOnClickListener listener and add view container in same layout for parent activity not in fragment activity:
public class MainActivity extends AppCompatActivity{
String number;
Button continueButton;
EditText NumberET;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
continueButton = (Button) findViewById(R.id.ContinueButton);
NumberET = (EditText) findViewById(R.id.etTotalAmount);
continueButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
number = NumberET.getText().toString();
if (amount.equals("")) {
Toast.makeText(getApplicationContext(), "Enter Number", Toast.LENGTH_SHORT).show();
} else {
Bundle bundle = new Bundle();
bundle.putString("AMOUNT_KEY", number);
FixedCosts fragment = new FixedCosts();
fragment.setArguments(bundle);
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.replace(R.id.container, fragment).commit();
}
}
});
}
Hope it will help you.
I fixed the issue by saving the value to SharedPreferences.
In the MainActivity.class:
SharedPreferences saveAmount = getSharedPreferences("MyPrefs", 0);
final SharedPreferences.Editor editor = saveAmount.edit();
editor.putString("AMOUNT", amount);
editor.commit();
In the Fragment class:
SharedPreferences sharedPrefs = this.getActivity().getSharedPreferences("MyPrefs", 0);
String amount = (sharedPrefs.getString("AMOUNT", "Ummmmm"));

Changing tabs without swiping

I would like to switch between tabs without swiping left or right. Using the class below, that is one of the Android Studio activity, I'm trying, by using external events, to move from one tab to another.
How can I do it? What methods should I implement/override?
public class Tabbed extends Activity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_tabbed, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tabbed, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}
}
Maybe I can do it with:
#Override public void changeTab(int where) {
mViewPager.setCurrentItem(mViewPager.getCurrentItem()+where)‌​;
}
where changeTab() method is an interface defined in the mainActivity.
Also I have in my mainActivity:
intent=new Intent(this, Tabbed.class);
startActivity(intent);
How can I call changeTab() method from the mainActivity?
You can use the setCurrentItem() function of the ViewPager object for this.
Example:
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setCurrentItem(2); // set number of desired tab
EDIT: For your second question, this has been asked a lot on StackOverflow. You cannot just call a method from another activity. You can add extra data to an intent to force the activity to start a function. But this does not apply to you, since you want to call the function after the intent is already created. I think you will have to take a look at listeners for your problem and for your communication between activities. Here is a question which is closely related to your problem, which can put you on your way.

Android - Tab items are not being created

I' am following a tutorial learning how to do a tab layout. I don't know what I did in the following code that it doesn't output the tabs. When the app runs, I don't see the tab item. Everything else works.
Acitvity
public class dashboard extends AppCompatActivity {
TabLayout tabs;
ViewPager container;
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(container);
tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
#Override
public void onTabSelected(TabLayout.Tab tab) {
container.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
container.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
container.setCurrentItem(tab.getPosition());
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_dashboard, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private String fragments [] = {"Completed", "In Progress"};
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position){
case 0:
return new dashboard_completed();
case 1:
return new dashboard_in_progress();
default:
return null;
}
}
#Override
public int getCount() {
return fragments.length;
}
#Override
public CharSequence getPageTitle(int position) {
return fragments[position];
}
}
}
dashboard_in_progress
public class dashboard_in_progress extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.dashboard_in_progress, container, false);
}
}
dashboard_completed
public class dashboard_completed extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.dashboard_completed, container, false);
}
}
XML
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabs">
</android.support.design.widget.TabLayout>
<view
android:layout_height="match_parent"
class="android.support.v4.view.ViewPager"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_marginTop="48dp"/>
</android.support.design.widget.CoordinatorLayout>
This is your problem.
ViewPager container;
...
private ViewPager mViewPager;
...
tabs.setupWithViewPager(container);
You're declaring two ViewPagers, but you're only initializing one, and trying to setup the TabLayout with the null one.
Remove the ViewPager container; declaration, and change the setup call to:
tabs.setupWithViewPager(mViewPager);
Also, you don't need to set the OnTabSelectedListener on the TabLayout yourself. The setupWithViewPager() method will handle that for you.
there is no tab to show. use this :
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));

activity with Viewpager android

I'm writing application with fragments and Viewpager.
I've got a problem because on each fragment is the same text.
I don't known how to refer to only one fragment.
Could someone give me any advice how to do it?
public class Main2Activity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main2, menu);
menu.add(200, 200, 200, "Fragment 1");
menu.add(300, 300, 300, "Fragment 2");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == 200) {
Intent intent = new Intent(this, Main2Activity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main2, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
// textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
String message;
message="Example text";
textView.setText(message);
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}
}
By creating instance of your fragment, you put arguments in Bundle
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
You can read them in onCreateView
sectionNumber = bundle.getInt(ARG_SECTION_NUMBER);
Then, for instance, change your text
message="Example text" + sectionNumber;

the first fragment in tabbed activity is always blank

I'm using tabbed activity to show three tabs
the problem is, the first tab is always blank, while the second and third works. and if i click the third one and after that click on the first one, the first loads the data
public class Main2Activity extends AppCompatActivity {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public static class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a Movies_Fragment (defined as a static inner class below).
Movies_Fragment fragment = Movies_Fragment.newInstance(position );
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Most Popular";
case 1:
return "Top Rated";
case 2:
return "Top Rated 2";
}
return null;
}
}
}
public class Movies_Fragment extends Fragment {
public static ImageAdapter imageAdapter;
public static Movies_Fragment newInstance(int sectionNumber) {
Movies_Fragment fragment = new Movies_Fragment();
Bundle args = new Bundle();
args.putInt("index", sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onStart() {
super.onStart();
updateMovies();
//imageAdapter.notifyDataSetChanged();
}
public void updateMovies() {
FetchMovieTask movieTask = new FetchMovieTask();
Bundle bundle = getArguments();
int index = bundle.getInt("index");
movieTask.execute(index);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies_, container, false);
imageAdapter = new ImageAdapter(getActivity());
GridView gridView = (GridView)rootView.findViewById(R.id.gridView);
gridView.setAdapter(imageAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
MDMovie movie = (MDMovie) imageAdapter.getItem(i);
Intent intent = new Intent(getActivity(),movieDetailsActivity.class);
intent.putExtra("movie", movie);
startActivity(intent);
}
});
return rootView;
}
}
This happened to one of my projects also and I was unable to diagnose it then too. I ended up using this [dirty] hack. Its not a solution by far but, it worked for us. We basically simulate a page change on start:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
mViewPager.setCurrentItem(1);
mViewPager.setCurrentItem(0);
}

Categories

Resources