2016-05-16 65 views
0

我想使查看尋呼機圈指示符爲我的應用程序。 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); 
    } 
} 

請幫助!

+0

你的問題還不清楚。什麼不正確?你有什麼嘗試。放置更少的代碼通常更好,只有相關的部分更好。好的問題通常很短或有一些總結。 –

+0

在這個方法initViews()中,你會看到listOfItems.add基本上將文本值更改爲1,2,3,4和5,但我想在用戶swip時顯示不同的佈局。 –

回答

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; 
    } 
+0

它的工作先生THANKyou太多了:) –

相關問題