Android: Table swipe in fragment goes too far

127 views Asked by At

Here it is my problem: My activity has 3 tabs with a relative fragment. The first fragment contains a swiping-photo-viewer. Why when I reaches the end of the pictures of the first fragment, it jumps to other tabs? Did I do something wrong or is there a method to stop the swiping to just the pictures of the first fragment? Here it is my code.

Main Activity

public class Home extends FragmentActivity implements ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;


private String[] tabs = { "Panoramica", "Contesto", "Storia" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

    // Adding Tabs
    for (String tab_name : tabs)
    {
        actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.table:
             startActivity(new Intent("android.intent.action.Home"));
            return true;
        case R.id.action_search2:
             startActivity(new Intent("android.intent.action.Reperti"));
            return true;
        case R.id.action_search3:
             startActivity(new Intent("android.intent.action.Sito"));
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
} [ --- Other Methods ---]

FirstFragment

    public class panoramica extends Fragment {

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
           Bundle savedInstanceState) {    


       View rootView = inflater.inflate(R.layout.fragment_panoramica, container, false);
       ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.pager);
       PagerAdapter adapter = new ImageAdapter(getActivity());
       viewPager.setAdapter(adapter);           
       return rootView;
   }

}

And this is part of my adapter

public class ImageAdapter extends PagerAdapter{

    Context context;
    int[] imageId = {R.drawable.icon,R.drawable.home};

   [--- Other Methods---]

 public Object instantiateItem(ViewGroup container, int position) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = ((Activity)context).getLayoutInflater();

        View viewItem = inflater.inflate(R.layout.fragment_foto, container, false);
        ImageView imageView = (ImageView) viewItem.findViewById(R.id.img);
        imageView.setImageResource(imageId[position]);
        ((ViewPager)container).addView(viewItem);

        return viewItem;
    }

}

1

There are 1 answers

0
user2788843 On

Use custom view pager for three tabs, where swipe is disabled. You can find example here: Is it possible to disable scrolling on a ViewPager