2017-10-06 70 views
0

我使用Viewpager根據循環中的多個問題動態膨脹我的佈局。如何在Viewpager中查找最後一個膨脹的佈局

for (String x : array1) { 
       //loop out the number of layouts relative to the number of questions held in x 
       View current_layout = LayoutInflater.from(getActivity()).inflate(R.layout.question_fragment, null); 

     //use the page adapter to add the layout to the user's view 
      pagerAdapter.addView(current_layout); 
    } 

我有我的佈局的按鈕,我只希望基於最終佈局進行充氣該按鈕可見:

這如下實現。

我使用下面的適配器

public class MainPagerAdapter extends PagerAdapter 
{ 
    // This holds all the currently displayable views, in order from left to right. 
    private ArrayList<View> views = new ArrayList<View>(); 

    //----------------------------------------------------------------------------- 
    // Used by ViewPager. "Object" represents the page; tell the ViewPager where the 
    // page should be displayed, from left-to-right. If the page no longer exists, 
    // return POSITION_NONE. 
    @Override 
    public int getItemPosition (Object object) 
    { 
     int index = views.indexOf (object); 
     if (index == -1) 
      return POSITION_NONE; 
     else 
      return index; 
    } 

    //----------------------------------------------------------------------------- 
    // Used by ViewPager. Called when ViewPager needs a page to display; it is our job 
    // to add the page to the container, which is normally the ViewPager itself. Since 
    // all our pages are persistent, we simply retrieve it from our "views" ArrayList. 
    @Override 
    public Object instantiateItem (ViewGroup container, int position) 
    { 
     View v = views.get (position); 
     container.addView (v); 
     return v; 
    } 

    //----------------------------------------------------------------------------- 
    // Used by ViewPager. Called when ViewPager no longer needs a page to display; it 
    // is our job to remove the page from the container, which is normally the 
    // ViewPager itself. Since all our pages are persistent, we do nothing to the 
    // contents of our "views" ArrayList. 
    @Override 
    public void destroyItem (ViewGroup container, int position, Object object) 
    { 
     container.removeView (views.get (position)); 
    } 

    //----------------------------------------------------------------------------- 
    // Used by ViewPager; can be used by the app as well. 
    // Returns the total number of pages that the ViewPage can display. This must 
    // never be 0. 
    @Override 
    public int getCount() 
    { 
     return views.size(); 
    } 

    //----------------------------------------------------------------------------- 
    // Used by ViewPager. 
    @Override 
    public boolean isViewFromObject (View view, Object object) 
    { 
     return view == object; 
    } 

    //----------------------------------------------------------------------------- 
    // Add "view" to right end of "views". 
    // Returns the position of the new view. 
    // The app should call this to add pages; not used by ViewPager. 
    public int addView (View v) 
    { 
     return addView (v, views.size()); 
    } 

    //----------------------------------------------------------------------------- 
    // Add "view" at "position" to "views". 
    // Returns position of new view. 
    // The app should call this to add pages; not used by ViewPager. 
    public int addView (View v, int position) 
    { 
     views.add (position, v); 
     return position; 
    } 

    //----------------------------------------------------------------------------- 
    // Removes "view" from "views". 
    // Retuns position of removed view. 
    // The app should call this to remove pages; not used by ViewPager. 
    public int removeView (ViewPager pager, View v) 
    { 
     return removeView (pager, views.indexOf (v)); 
    } 

    //----------------------------------------------------------------------------- 
    // Removes the "view" at "position" from "views". 
    // Returns position of removed view. 
    // The app should call this to remove pages; not used by ViewPager. 
    public int removeView (ViewPager pager, int position) 
    { 
     // ViewPager doesn't have a delete method; the closest is to set the adapter 
     // again. When doing so, it deletes all its views. Then we can delete the view 
     // from the adapter and finally set the adapter to the pager again. Note 
     // that we set the adapter to null before removing the view from "views" - that's 
     // because while ViewPager deletes all its views, it will call destroyItem which 
     // will, in turn, cause a null pointer ref. 
     pager.setAdapter (null); 
     views.remove (position); 
     pager.setAdapter (this); 

     return position; 
    } 

    //----------------------------------------------------------------------------- 
    // Returns the "view" at "position". 
    // The app should call this to retrieve a view; not used by ViewPager. 
    public View getView (int position) 
    { 
     return views.get (position); 
    } 

    // Other relevant methods: 

    // finishUpdate - called by the ViewPager - we don't care about what pages the 
    // pager is displaying so we don't use this method. 
} 

我是新來使用Viewpager所以不能完全肯定這是如何實現的 - 我假設一個自定義的方法需要被創建,但具有搜索後,我無法找到解釋清楚這是如何實現或對我的方案有用的內容。

我知道我可以編寫以下代碼以使按鈕在第一個佈局上消失,但我對於最後一個佈局感到困惑。

//Set button on the last layout in the viewpager 

    if (count == pagerAdapter.getCount()+1){ 

getCurrentPage().findViewById(R.id.btn_submit_question).setVisibility(View.GONE); 
    } 

我試圖實現我是什麼後

//Set button on the last layout in the viewpager 
       if (count != pagerAdapter.getCount()-1){ 

        current_layout.findViewById(R.id.btn_submit_question).setVisibility(View.GONE); 

       } 

但沒有運氣。

我也有可用的

// Here's what the app should do to add a view to the ViewPager. 
    public void addView (View newPage) 
    { 
     int pageIndex = pagerAdapter.addView (newPage); 
     // You might want to make "newPage" the currently displayed page: 
     pager.setCurrentItem (pageIndex, true); 
    } 

    //----------------------------------------------------------------------------- 
    // Here's what the app should do to remove a view from the ViewPager. 
    public void removeView (View defunctPage) 
    { 
     int pageIndex = pagerAdapter.removeView (pager, defunctPage); 
     // You might want to choose what page to display, if the current page was "defunctPage". 
     if (pageIndex == pagerAdapter.getCount()) 
      pageIndex--; 
     pager.setCurrentItem (pageIndex); 
    } 

    //----------------------------------------------------------------------------- 
    // Here's what the app should do to get the currently displayed page. 
    public View getCurrentPage() 
    { 
     return pagerAdapter.getView (pager.getCurrentItem()); 
    } 

    //----------------------------------------------------------------------------- 
    // Here's what the app should do to set the currently displayed page. "pageToShow" must 
    // currently be in the adapter, or this will crash. 
    public void setCurrentPage (View pageToShow) 
    { 
     pager.setCurrentItem (pagerAdapter.getItemPosition (pageToShow), true); 
    } 
+0

可以使用viewPager.addOnPageChangeListener和views.get(位置) –

回答

0

我設法解決了下面的問題。

感謝Pranav Bhatt &冷火指引我在正確的方向。

//Set button on the last layout in the viewpager 

if (pager.getAdapter().getCount()!= array1.length){ 

current_layout.findViewById(R.id.btn_submit_question).setVisibility(View.GONE); 

    } 
1

您應該使用ViewPager.getAdapter().getCount()

The getAdapter()方法這些方法返回提供了ViewPager頁面的對象。

getCount()方法返回多少頁已經加入已經使

if(ViewPager.getAdapter().getCount()==array1-1){ 
// set button invisible here 
} 

希望這可以幫助你。

+0

你能提供一個例子獲得最後選定的看法?我很感謝你試圖幫助我找到解決方案。 –

+0

@Displayname檢查編輯 –

+0

謝謝ColdFire編輯這個。 @Displayname請現在檢查答案。 – InsaneCat