Viewpager Fragment& ImageView的

时间:2016-06-01 09:49:05

标签: android-fragments android-viewpager android-imageview android-adapter

在片段中我想制作viewpager imageview,但我在Fragment中堆叠。

我的代码:"我的活动,适配器和片段"

我的活动:

public class MainActivity extends FragmentActivity {
ViewPager mPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mPager = (ViewPager) findViewById(R.id.pager);
    FragmentManager fm = getSupportFragmentManager();

    ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
        }
    };

    mPager.setOnPageChangeListener(ViewPagerListener);
    FragmentAdapter viewpageradapter = new FragmentAdapter(fm);
    mPager.setAdapter(viewpageradapter);
}   

public interface FragmentInterface{
    void Fragmentbecamevisible();
}}

我的适配器:

public class FragmentAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 5;

public FragmentAdapter (FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int arg0) {
    switch (arg0) {
    case 0:
        Tab1 fragmenttab1 = new Tab1();
        return fragmenttab1;
    case 1:
        Tab2 fragmenttab2 = new Tab2();
        return fragmenttab2;
    case 2:
        Tab3 fragmenttab3 = new Tab3();
        return fragmenttab3;
    case 3:
        Tab4 fragmenttab4 = new Tab4();
        return fragmenttab4;
    case 4:
        Tab5 fragmenttab5 = new Tab5();
        return fragmenttab5;
    }
    return null;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return PAGE_COUNT;
}}

Stack in Fragment:

public class Tab1 extends Fragment {
PagerAdapter adapter;
int[] IMAGES = new int[] { R.drawable.img1, 
        R.drawable.img2, R.drawable.img3, 
        R.drawable.img4, R.drawable.img5};  

@SuppressLint("NewApi")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Get the view from fragmenttab1.xml
    View view = inflater.inflate(R.layout.fragment_imageview, container, false);
    return view;
}}

添加viewpager,imageview和适配器.. 帮助,谢谢

Like This

0 个答案:

没有答案