Reputation: 59
This is my first time with ViewPager and after some time it now works, that I can switch between three different Activities (Main, Scan, and Options). My problem is to get access to the listViews, EditText and the FloatingActionButton inside the Main- and ScanActivity.
What I want to achieve, should be similar to this:
private FloatingActionButton fab;
public static void setupContent (View view, String title) {
......
fab = view.findViewById (R.id.fab);
....
}
public void someMethod () {
fab.setOnClickListener....
}
For references beyond the code below, check my github: https://github.com/ChristopherPS96/SmartFridge/tree/master/app/src/main/java/com/example/christopher/smartfridge
Beware that this project is work in progress, so not everything is perfect
This is the method inside the MainActivity.java, which works. Now I want to expand it, that I can set OnClickTimeListener on my ListView or do anything with anything outside of this method for that matter. Always I get NullpointerExceptions or nothing happens at all.
public static void setupContent(View view, String title) {
if(title.equals("Hauptseite")) {
FloatingActionButton fab = view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Test", Toast.LENGTH_LONG).show();
}
});
}
if(title.equals("Scanseite")) {
FloatingActionButton fab = view.findViewById(R.id.fabScan);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Alternativer Test", Toast.LENGTH_LONG).show();
}
});
}
}
What I have tried so far:
-making the ListView, EditText static (which causes NullpointerException and a MemoryLeak)
-storing the view and title inside of an static ArrayList (ArrayList is empty)
-calling a new method with MainActivity mainactivity = new MainActivity()
mainactivity.someMethod() (NullpointerException)
Upvotes: 2
Views: 83
Reputation: 2789
I agree with @yupi. It will be much simpler to have 3 fragments instead of 3 activities. "You will be able to maintain code in future".
After setting this code in your Home activity, you will have to create fragment classes for (SettingFragment, ScanItemFragment, and MainContentFragment). You can move your existing layout from the already existing activities to your fragment's layout. You will have to make few modifications due to the differences of lifecycle between activities and fragments, but it should not take you too much time.
This code should totally get you started with what you trying to achieve. Good Luck !
public class HomeActivity extends AppCompatActivity {
private SectionPageAdapter mSectionPageAdapter;
private ViewPager mViewPager;
private TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// Create the adapter that returns a fragment for each tab
mSectionPageAdapter = new SectionPageAdapter(getSupportFragmentManager());
//Set up the view pager with different section adapters
mViewPager = (ViewPager) findViewById(R.id.contentFrameLayout);
mViewPager.setOffscreenPageLimit(3);
mViewPager.setAdapter(mSectionPageAdapter);
mViewPager.setPagingEnabled(false);
//Set up the tab layout to display tabs
tabLayout = (TabLayout) findViewById(R.id.homeTabs);
tabLayout.setupWithViewPager(mViewPager);
//add icons to tabs and change the text behavior
for (int i = 0; i< tabLayout.getTabCount(); i++) {
TabLayout.Tab mTab = tabLayout.getTabAt(i);
if (mTab != null) {
switch (i){
case 0:
mTab.setIcon(R.drawable.icon_zero);
break;
case 1:
mTab.setIcon(R.drawable.icon_one);
break;
case 2:
mTab.setIcon(R.drawable.icon_two);
break;
default:
break;
}
}
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the tabs.
*/
private class SectionPageAdapter extends FragmentPagerAdapter {
SectionPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return (MainContentFragment.newInstance());
case 1:
return (ScanItemFragment.newInstance());
case 2:
return (SettingFragment.newInstance());
default:
break;
}
return (null);
}
@Override
public int getCount() {
return 3;
}
}
}
Upvotes: 1
Reputation: 4470
What you are actually doing right now is switching between different layouts. Probably what you want is to create three Fragments
with each layout you have right now. Than create adapter extending FragmentStatePagerAdapter
and inside getItem
according to position
return corresponding Fragment
. Than set your adapter to ViewPager
and now you will be able to swipe and to access views in Fragments
.
Upvotes: 1