查看具有不同布局的寻呼机圈指示器

时间:2016-05-16 12:34:58

标签: android android-fragments layout viewpagerindicator

我想为我的应用制作查看寻呼机圈子指示器。 View Pager与圆形指示器2完美配合,但我无法在其中添加不同的布局,即现在它只是在用户翻转屏幕时更改textview值。请告诉我如何实现布局,以便我可以在swip时查看不同的布局 这是我的代码:

public class MainActivity extends Activity {

private ViewPager viewPager;
private MyViewPagerAdapter myViewPagerAdapter;
private ArrayList<Integer> listOfItems;

private LinearLayout dotsLayout;
private int dotsCount;
private TextView[] dots;

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

    initViews();
    setViewPagerItemsWithAdapter();
    setUiPageViewController();
}

private void setUiPageViewController() {
    dotsLayout = (LinearLayout)findViewById(R.id.viewPagerCountDots);
    dotsCount = myViewPagerAdapter.getCount();
    dots = new TextView[dotsCount];

    for (int i = 0; i < dotsCount; i++) {
        dots[i] = new TextView(this);
        dots[i].setText(Html.fromHtml("&#8226;"));
        dots[i].setTextSize(30);
        dots[i].setTextColor(getResources().getColor(android.R.color.darker_gray));
        dotsLayout.addView(dots[i]);            
    }

    dots[0].setTextColor(getResources().getColor(android.R.color.white));
}

private void setViewPagerItemsWithAdapter() {
    myViewPagerAdapter = new MyViewPagerAdapter(listOfItems);
    viewPager.setAdapter(myViewPagerAdapter);
    viewPager.setCurrentItem(0);
    viewPager.setOnPageChangeListener(viewPagerPageChangeListener);
}

//  page change listener
OnPageChangeListener viewPagerPageChangeListener = new OnPageChangeListener() {

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < dotsCount; i++) {
            dots[i].setTextColor(getResources().getColor(android.R.color.darker_gray));
        }
        dots[position].setTextColor(getResources().getColor(android.R.color.white));
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }
};

private void initViews() {

    getActionBar().hide();

    viewPager = (ViewPager)findViewById(R.id.viewPager);

    listOfItems = new ArrayList<Integer>();
    listOfItems.add(1);
    listOfItems.add(2);
    listOfItems.add(3);
    listOfItems.add(4);
    listOfItems.add(5);
}

//  adapter
public class MyViewPagerAdapter extends PagerAdapter{

    private LayoutInflater layoutInflater;
    private ArrayList<Integer> items;

    public MyViewPagerAdapter(ArrayList<Integer> listOfItems) {
        this.items = listOfItems;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.view_pager_item, container,false);

        TextView tView = (TextView)view.findViewById(R.id.PageNumber);

        tView.setText(listOfItems.get(position).toString());

        ((ViewPager) container).addView(view);

        return view;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object obj) {
        return view == ((View)obj);
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View)object;
        ((ViewPager) container).removeView(view);
    }
}

请帮助!

1 个答案:

答案 0 :(得分:1)

全部在instantiateItem()。只需为该页面充气并填写所需的视图。

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view;
        switch (position) {
        case 0: // page 1
            view = layoutInflater.inflate(R.layout.page1, container,false);
            // TODO set up your page 1 views
            break;
        case 1: // page 2
            view = layoutInflater.inflate(R.layout.page2, container,false);
            // TODO set up your page 2 views
            break;
        case 2: // page 3
            view = layoutInflater.inflate(R.layout.page3, container,false);
            // TODO set up your page 3 views
            break;
        // etc. etc. etc.
        }

        ((ViewPager) container).addView(view);

        return view;
    }