0

如何實現ViewPagerIndicator像SwiftPagingNav iOS Library如何實現ViewPagerIndicator兩個冠軍和圈

  • 只顯示當前頁面的標題
  • 當刷卡顯示當前和下一個或前一頁

現在我可以同時顯示標題指示和圓圈指示,但現在顯示2或3頁標題,我只想要當前頁面和上一頁或下一頁時刷卡

images

對不起,下面的代碼來設置View尋呼機指示燈的英語不好

+0

使用https://github.com/JakeWharton/ViewPagerIndicator –

+0

請按照給定的答案..! –

回答

0

使用,你需要:

/* 
* Function to set Indicator to ViewPager. 
* */ 
public void setIndicator() { 
    yourViewPager = (ViewPager) findViewById(R.id.yourViewPager); 
    yourAdapter adpt = new yourAdapter(YourActivity.this); 
    yourViewPager.setAdapter(adpt); 
    count_layout = (LinearLayout) findViewById(R.id.image_count); 
    count = yourArrayList.size(); 
    page_text = new ImageView[count]; 
    for (int i = 0; i < count; i++) { 
     page_text[i] = new ImageView(this); 
     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     lp.setMargins(5, 0, 5, 0); 
     page_text[i].setLayoutParams(lp); 
     page_text[i].setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pagination_default)); 
     count_layout.addView(page_text[i]); 
    } 
    yourViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
     @Override 
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 
      try { 
       for (int i = 0; i < count; i++) { 
        page_text[i] 
          .setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pagination_default)); 
       } 

       page_text[position] 
         .setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pagination_selected)); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } 

     @Override 
     public void onPageSelected(int position) { 
     } 

     @Override 
     public void onPageScrollStateChanged(int state) { 
     } 
    }); 
} 

正如從代碼邏輯,你可以看到,我已創建陣列的總圖像即page_text[]

您的佈局可能如下所示:

<RelativeLayout 
    android:id="@+id/relContent" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/yourViewPager" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <LinearLayout 
     android:id="@+id/image_count" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="20dp" 
     android:background="#00000000" 
     android:gravity="center" 
     android:orientation="horizontal"></LinearLayout> 


    <LinearLayout 
     android:id="@+id/linTemp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/viewPagerItemDetails" 
     android:orientation="horizontal"> 

     <ScrollView 
      android:id="@+id/scrollTags" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"></ScrollView> 
    </LinearLayout> 
</RelativeLayout> 
+1

謝謝你的幫助。 –