Android 에서 viewpager 슬라이더 의 인 스 턴 스 코드

먼저 효과 그림 보기:

이것 은 오픈 소스 라 이브 러 리 가 필요 합 니 다.이것 도 나중에 말씀 드 리 겠 습 니 다.

프로젝트 디 렉 터 리:

1. MainActivity.java

public class MainActivity extends FragmentActivity { 
  private ViewPagerFrameAdapter adapter;  //   (     ) 
 private ViewPager mPager;        
 private TabPageIndicator   tabbPageIndicator;   // 
 private UnderlinePageIndicatorEx underlinePageIndicator; 
 private int COUNT = 0; 
 private List<String> titles = new ArrayList<String>(); //   
 @Override 
 protected void onCreate(Bundle bundle) { 
    super.onCreate(bundle); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_main); 
    //  ,          
    for (int j = 0; j < 3 ; j++){ 
     titles.add( " "+ j +"   "); 
    } 
    mPager = (ViewPager)findViewById(R.id.pager); 
    adapter = new ViewPagerFrameAdapter(getSupportFragmentManager(),titles); 
    mPager.setAdapter(adapter); 
    //      
    tabbPageIndicator = (TabPageIndicator) findViewById(R.id.tab_indicator);  
    tabbPageIndicator.setViewPager(mPager); 
    //   
    underlinePageIndicator = (UnderlinePageIndicatorEx)findViewById(R.id.underline_indicator); 
    underlinePageIndicator.setViewPager(mPager); 
    underlinePageIndicator.setFades(false); 
    //      
    tabbPageIndicator.setOnPageChangeListener(underlinePageIndicator); 
 } 
 //adapter              
 private class ViewPagerFrameAdapter extends FragmentStatePagerAdapter{ 
  private List<String> title; //         
  public ViewPagerFrameAdapter(FragmentManager fm, List<String> title) { 
   super(fm); 
   this.title = title; 
  } 
  //  position    fragment 
  @Override 
  public Fragment getItem(int position) { 
   return ArrayFragment.newInstance(position); 
  } 
  //size 
  @Override 
  public int getCount() { 
    return title.size(); 
  } 
  //   title 
  @Override 
  public CharSequence getPageTitle(int position) { 
   if (titles != null){ 
    return titles.get(position).toString(); 
   } 
   return super.getPageTitle(position); 
  }  
 } 
} 
UnderlinePageIndicatorEx.java

//         
public class UnderlinePageIndicatorEx extends UnderlinePageIndicator{ 
  public UnderlinePageIndicatorEx(Context context) { 
   super(context, null);   
  } 
  public UnderlinePageIndicatorEx(Context context, AttributeSet attrs) { 
   super(context, attrs, R.attr.vpiUnderlinePageIndicatorStyle);  
  } 
  public UnderlinePageIndicatorEx(Context context, AttributeSet attrs, int defStyle) { 
   super(context, attrs, defStyle);   
  } 
  //    ViewPage     
  @Override 
  public void setViewPager(ViewPager viewPager) { 
   if (mViewPager == viewPager){ 
    return; 
   } 
//   if (mViewPager != null) { 
//    mViewPager.setOnPageChangeListener(null); 
//   } 
   if (viewPager.getAdapter() == null) { 
    throw new IllegalStateException(" pager       adapter"); 
   } 
   mViewPager = viewPager; 
//   mViewPager.setOnPageChangeListener(this); 
   invalidate(); 
   post(new Runnable(){ 
    @Override public void run() { 
     if (mFades) { 
      post(mFadeRunnable); 
     } 
    }  
   }); 
  } 
} 
ArrayFragment.java

public class ArrayFragment extends Fragment{ 
 private int position; 
 public ArrayFragment(int position){ 
  this.position = position; 
 } 
  //    viwePager         
 public static ArrayFragment newInstance(int position){ 
  return new ArrayFragment(position); 
 } 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
 } 
 @Override 
 public void onDestroy() { 
  super.onDestroy(); 
 } 
 //   position      view 
 @Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
   Bundle savedInstanceState) { 
  View view = inflater.inflate(R.layout.layoutaa, null);  
  return view; 
 } 
} 
자,다음은 레이아웃 xml 입 니 다.

<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:orientation="vertical" 
  > 
 <!--                  --> 
  <LinearLayout 
  android:layout_width="fill_parent" 
  android:layout_height="35dp" 
  android:orientation="vertical" 
  android:background="@drawable/main_header_background"> 
    <com.viewpagerindicator.TabPageIndicator 
     android:id="@+id/tab_indicator" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent"/> 
    <com.test.linepagerindictor.widget.UnderlinePageIndicatorEx 
     android:id="@+id/underline_indicator" 
     android:layout_height="3dip" 
     android:layout_width="fill_parent" 
     android:layout_alignParentBottom="true"/> 
  </LinearLayout>  
  <android.support.v4.view.ViewPager 
  android:id="@+id/pager" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent"/>  
</LinearLayout> 
다음 슬라이더 의 style 설정 

<!--   style           tab style   Indicator style    --> 
 <style name="StyledAllIndicators" parent="@android:style/Theme.Light"> 
  <item name="vpiTabPageIndicatorStyle">@style/CustomTabPageIndicator</item> 
  <item name="vpiUnderlinePageIndicatorStyle">@style/CustomUnderlinePageIndicator</item> 
 </style> 
 <!-- tab  style --> 
 <style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator"> 
  <item name="android:background">#00000000</item> 
  <item name="android:textAppearance">@style/CustomTabPageIndicator_text</item> 
  <item name="android:textColor">@color/selector_tab</item> 
  <item name="android:textSize">20sp</item> 
  <!--    ,      --> 
  <item name="android:divider">@drawable/custom_tab_indicator_divider</item> 
  <item name="android:dividerPadding">10dip</item> 
  <item name="android:showDividers">middle</item> 
  <item name="android:paddingTop">4dp</item> 
  <item name="android:paddingLeft">8dp</item> 
  <item name="android:paddingBottom">4dp</item> 
  <item name="android:paddingRight">8dp</item> 
  <item name="android:fadingEdge">horizontal</item> 
  <item name="android:fadingEdgeLength">8dp</item> 
 </style> 
 <style name="CustomTabPageIndicator_text" parent="android:TextAppearance.Medium"> 
  <item name="android:typeface">monospace</item> 
 </style> 
 <!--           --> 
 <style name="CustomUnderlinePageIndicator"> 
  <item name="selectedColor">#EB5548</item> 
  <item name="android:background">#00000000</item> 
  <item name="fadeLength">1000</item> 
  <item name="fadeDelay">1000</item> 
 </style> 
 selector_tab.xml     이것 은 선택 기 가 style 에서 인용 한 것 으로 선 택 된 배경 색 을 전환 하 는 데 사 용 됩 니 다.

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 <item android:state_selected="true" android:color="#91D228" /> 
 <item android:state_focused="true" android:color="#91D228" /> 
 <item android:color="#dddddd" /> 
</selector> 

좋은 웹페이지 즐겨찾기