Search code examples
androidlistviewandroid-viewpagerfragmentstatepageradapter

Can't swipe to next item inside my detail activity after implementing viewpager


I am trying to swipe item(from listview) in my detail activity using viewpager. I have 20 items in my listview (data parsed from json) Instead of swiping the next/previous item , It's only the same item that I swipe (20 times).

here is my DetailActivity.

public class DetailsActivity extends FragmentActivity {


Article feed;

int pos;
private DescAdapter adapter;
private ViewPager pager;
...
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.item_details);

    pos = getIntent().getExtras().getInt("pos");

    adapter = new DescAdapter(getSupportFragmentManager());
    pager = (ViewPager) findViewById(R.id.pager);       
    pager.setAdapter(adapter);  
    pager.setCurrentItem(pos);

}

public class DescAdapter extends FragmentStatePagerAdapter {
    public DescAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public int getCount() {
        return 20;
    }

    public Fragment getItem(int position) {
         return DetailFragment.newInstance(position);
     }
}

And my detailFragment

public class DetailFragment extends Fragment {

static DetailFragment newInstance(int position) {
     DetailFragment f = new DetailFragment();        
     return f;
 }

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.detail_fragment, container, false);

    String displaytitle= getActivity().getIntent().getStringExtra("title");
    TextView titleF = (TextView)view.findViewById(R.id.title);

    titleF.setText(displaytitle);  
    ...      
    return view;
} 

What am I doing wrong?


Solution

  • You are not using the correct argument:
    You send the argument "pos2" but you retrieve the argument "title".

    @Override
    public Fragment getItem(int position) {
        DetailFragment frag = new DetailFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("fPos",position);
        frag.setArguments(bundle);
        return frag;
    }