Android ViewPagerIndicator 使用示例

jopen 7年前


github地址:https://github.com/JakeWharton/Android-ViewPagerIndicator


ViewPagerIndicator作为一款分页指标小部件兼容ViewPager,封装上做得非常不错,目前已为众多知名应用所使用。具体API的使用,大家可以下载官方demo示例研究研究就知道啦!

 

自己写了一个简单的Demo mark一下

 

运行效果图

qq截图20140912160818.png

 

 

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      tools:context=".MainActivity"       android:orientation="vertical">        <com.viewpagerindicator.TabPageIndicator          android:id="@+id/indicator"          android:layout_height="wrap_content"          android:layout_width="fill_parent"          />      <android.support.v4.view.ViewPager          android:id="@+id/pager"          android:layout_width="fill_parent"          android:layout_height="0dp"          android:layout_weight="1"          />    </LinearLayout>


MainActivity.java

package com.example.vpitest;    import android.os.Bundle;  import android.support.v4.app.Fragment;  import android.support.v4.app.FragmentActivity;  import android.support.v4.app.FragmentManager;  import android.support.v4.app.FragmentPagerAdapter;  import android.support.v4.view.ViewPager;  import com.example.vpitest.fragment.ContentFragment;  import com.viewpagerindicator.TabPageIndicator;    public class MainActivity extends FragmentActivity {   private static final String[] CONTENT = new String[] { "头条", "娱乐", "体育", "财经", "科技", "汽车","NBA" };     @Override   protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);        findView();   }     private void findView() {    FragmentPagerAdapter adapter = new WYNewsAdapter(getSupportFragmentManager());            ViewPager pager = (ViewPager)findViewById(R.id.pager);          pager.setAdapter(adapter);            TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);          indicator.setViewPager(pager);   }      class WYNewsAdapter extends FragmentPagerAdapter {          public WYNewsAdapter(FragmentManager fm) {              super(fm);          }            @Override          public Fragment getItem(int position) {              return ContentFragment.newInstance(CONTENT[position % CONTENT.length]);          }            @Override          public CharSequence getPageTitle(int position) {              return CONTENT[position % CONTENT.length].toUpperCase();          }            @Override          public int getCount() {            return CONTENT.length;          }      }    }


ContentFragment.java

package com.example.vpitest.fragment;    import android.os.Bundle;  import android.support.v4.app.Fragment;  import android.view.Gravity;  import android.view.LayoutInflater;  import android.view.View;  import android.view.ViewGroup;  import android.widget.LinearLayout;  import android.widget.LinearLayout.LayoutParams;  import android.widget.TextView;    public final class ContentFragment extends Fragment {      private static final String KEY_CONTENT = "TestFragment:Content";        public static ContentFragment newInstance(String content) {          ContentFragment fragment = new ContentFragment();            StringBuilder builder = new StringBuilder();          for (int i = 0; i < 10; i++) {              builder.append(content).append(" ");          }          builder.deleteCharAt(builder.length() - 1);          fragment.mContent = builder.toString();            return fragment;      }        private String mContent = "";        @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);            if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {              mContent = savedInstanceState.getString(KEY_CONTENT);          }      }        @Override      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {          TextView text = new TextView(getActivity());          text.setGravity(Gravity.CENTER);          text.setText(mContent);          text.setTextSize(20 * getResources().getDisplayMetrics().density);          text.setPadding(20, 20, 20, 20);            LinearLayout layout = new LinearLayout(getActivity());          layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));          layout.setGravity(Gravity.CENTER);          layout.addView(text);            return layout;      }        @Override      public void onSaveInstanceState(Bundle outState) {          super.onSaveInstanceState(outState);          outState.putString(KEY_CONTENT, mContent);      }  }


最后记得在 清单文件 activity 节点上配置 theme 属性

<activity              android:name="com.example.vpitest.MainActivity"              android:label="@string/app_name" android:theme="@style/Theme.PageIndicatorDefaults">              <intent-filter>                  <action android:name="android.intent.action.MAIN" />                    <category android:name="android.intent.category.LAUNCHER" />              </intent-filter>          </activity>