Reputation: 744
I have implemented THIS in my activity and it works perfectly fine, but I would like to change the content of my fragment every time when the user slides a finger on the screen. Now it shows the same text not considering the slide(left to right or right to left). How can I handle it? I know that I'm missing an extremely small part here, but I'm not able to understand what is going on. Where should I add the listener and handle the actions?
Here is what I have so far:
public class ViewrerAct extends FragmentActivity {
private static final int NUM_PAGES = 5;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.bulgarian_sayings, 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();
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 {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_viewer, container, false);
return rootView;
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
System.out.println("inside!");
return new ScreenSlidePageFragment();
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
AND
public class ScreenSlidePageFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_viewer, container, false);
return rootView;
}
}
Upvotes: 2
Views: 3570
Reputation: 17401
You should post the code for ScreenSlidPagerAdapter
.
Nevertheless, you need to override setPrimaryItem
method of pager adapter that gets called every time you slid the pager and change the displayed page.
for ex:
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
currentPage = position;
}
You can store your Strings in array and pass it to adapter and inside adapter you can change the content of the fragment with respect to the selected position.
Post pager adapter as well if want more appropriate example.
In your case, you can simply pass string array, assuming you just want different test to be displayed, to adapter for ex:
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
String[] displayText;
public ScreenSlidePagerAdapter(FragmentManager fm,String[] displayText) {
super(fm);
this.displayText=displayText;
}
@Override
public Fragment getItem(int position) {
System.out.println("inside!");
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
fragment.setText(displayTest[position]);
return fragment;
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
And your Fragment class would change to:
public class ScreenSlidePageFragment extends Fragment {
private String text;
public void setText(String text){
this.text=text;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_viewer, container, false);
((TextView)rootView.findViewById(textViewId)).setText(Text);
return rootView;
}
}
Upvotes: 2