Android ViewPager實現Tabhost選項卡底部滑塊動態滑動過渡,viewpagertabhost

來源:互聯網
上載者:User

Android ViewPager實現Tabhost選項卡底部滑塊動態滑動過渡,viewpagertabhost


《Android ViewPager實現Tabhost選項卡底部滑塊動態滑動過渡》

之前基於github上的第三方開原始檔控制ViewPagerIndicator的UnderlinePageIndicator(原文連結:http://blog.csdn.net/zhangphil/article/details/44752213),自己寫了一個底部帶有滑塊、且當ViewPager頁面切換時候選項卡也隨之相應切換,且滑塊也隨之相應動態滑動效果得控制項。但寫的太過於緊耦合,不利於複用,所以現在重構了代碼,重寫了代碼,把這些效果做成了一個自定製的松耦合控制項:TabLinearLayout,該控制項可以在未來的項目中直接拿出使用。

控制項:


測試用的MainActivity.java:

package zhangphil.view;import java.util.ArrayList;import zhangphil.underline.R;import com.viewpagerindicator.UnderlinePageIndicator;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.support.v7.app.ActionBarActivity;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.TextView;import android.content.Context;import android.graphics.Color;import android.os.Bundle;/** * 基於第三方開原始檔控制ViewPagerIndicator的UnderlinePageIndicator,自己寫的一個在選項卡底部有襯線的滑動控制項。 * 控制項。 這裡面有一個特別的效果是:頭部的選項卡在ViewPager切換過程中,底部的滑塊也隨之動態漸漸滑動過渡。 *  * */public class MainActivity extends ActionBarActivity {private TabLinearLayout mTabLinearLayout;private ArrayList<Fragment> mArryList;private ViewPager mPager;// 未被選中的選項卡字型顏色private int COLOR_NORMAL = Color.DKGRAY;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mArryList = new ArrayList<Fragment>();// 初始化5個Fragment作為測試。for (int i = 0; i < 5; i++) {TestFragment f = (TestFragment) TestFragment.newInstance();f.id = i;mArryList.add(f);}PagerAdapter mAdapter = new MyFragmentAdapter(getSupportFragmentManager());mPager = (ViewPager) findViewById(R.id.pager);mPager.setAdapter(mAdapter);UnderlinePageIndicator mIndicator = (UnderlinePageIndicator) findViewById(R.id.indicator);mIndicator.setViewPager(mPager);mIndicator.setFades(false);mIndicator.setSelectedColor(0xff33B5E5);mTabLinearLayout = (TabLinearLayout) findViewById(R.id.tab_LinearLayout);ArrayAdapter mTabLinearLayoutAdapter = new MyTabLinearLayoutAdapter(this, -1);mTabLinearLayout.initialization(mPager, mTabLinearLayoutAdapter,mIndicator);}private class MyTabLinearLayoutAdapter extends ArrayAdapter {private Context context;public MyTabLinearLayoutAdapter(Context context, int resource) {super(context, resource);this.context = context;}@Overridepublic int getCount() {return mArryList.size();}// 在這裡僅僅返回一個TextView作為選項卡的View。// 此處可以返回更豐富的View。@Overridepublic View getView(int position, View convertView, ViewGroup parent) {TextView v = new TextView(context);v.setGravity(Gravity.CENTER);v.setText("選項卡" + position);v.setTextColor(COLOR_NORMAL);return v;}}// 僅僅用於測試的Fragment,用一個id標識。private static class TestFragment extends Fragment {public int id;public static Fragment newInstance() {return new TestFragment();}public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {TextView v = new TextView(getActivity());v.setGravity(Gravity.CENTER);v.setTextSize(50f);v.setText("Fragment: " + id);return v;}}private class MyFragmentAdapter extends FragmentPagerAdapter {public MyFragmentAdapter(FragmentManager fm) {super(fm);}@Overridepublic Fragment getItem(int position) {return mArryList.get(position);}@Overridepublic int getCount() {return mArryList.size();}}}



MainActivity.java需要的activity_main.xml布局檔案:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <zhangphil.view.TabLinearLayout        android:id="@+id/tab_LinearLayout"        android:layout_width="match_parent"        android:layout_height="30dip"        android:orientation="horizontal" />    <com.viewpagerindicator.UnderlinePageIndicator        android:id="@+id/indicator"        android:layout_width="match_parent"        android:layout_height="7px" />    <View        android:layout_width="match_parent"        android:layout_height="1px"        android:background="#33B5E5" />    <android.support.v4.view.ViewPager        android:id="@+id/pager"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1" /></LinearLayout>



自己重寫的TabLinearLayout.java:

package zhangphil.view;import com.viewpagerindicator.PageIndicator;import android.content.Context;import android.graphics.Color;import android.support.v4.view.ViewPager;import android.util.AttributeSet;import android.view.View;import android.widget.ArrayAdapter;import android.widget.LinearLayout;import android.widget.TextView;public class TabLinearLayout extends LinearLayout {private int COLOR_NORMAL = Color.DKGRAY;private ArrayAdapter mAtapter;private ViewPager mPager;private TabLinearLayoutListener mTabLinearLayoutListener;public TabLinearLayout(Context context, AttributeSet attrs) {super(context, attrs);}public TabLinearLayout(Context context) {super(context);}public void initialization(ViewPager mPager, ArrayAdapter mAtapter,PageIndicator mPageIndicator) {this.mAtapter = mAtapter;this.mPager = mPager;mPageIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {@Overridepublic void onPageSelected(int pos) {setCurrentItem(pos);if (mTabLinearLayoutListener != null)mTabLinearLayoutListener.onTab(pos);}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {}@Overridepublic void onPageScrollStateChanged(int arg0) {}});// 添加選項卡addIndicators();// 初始化,第0項被選中setIndicatorViewSelected(0);}public void initialization(ViewPager mPager, ArrayAdapter mAtapter,PageIndicator mPageIndicator,TabLinearLayoutListener mTabLinearLayoutListener) {this.mTabLinearLayoutListener = mTabLinearLayoutListener;initialization(mPager, mAtapter, mPageIndicator);}// 添加Tab選項卡private void addIndicators() {for (int i = 0; i < mAtapter.getCount(); i++) {View v = mAtapter.getView(i, null, null);addIndicatorItem(v, i);}}// 在這裡設定被選中時候選項卡變化的效果private void setIndicatorViewSelected(int pos) {for (int i = 0; i < super.getChildCount(); i++) {if (i == pos) {View v = super.getChildAt(i);TextView tv = (TextView) v;// Android Holo 樣式的藍色tv.setTextColor(0xff33B5E5);} else {View v = super.getChildAt(i);TextView tv = (TextView) v;tv.setTextColor(COLOR_NORMAL);}}}// 線上性布局裡面依次添加一個View,為添加的View添加事件。private void addIndicatorItem(View view, final int index) {LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT, 1);super.addView(view, index, params);view.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// 當使用者點擊該View時候,設定ViewPager正確而Pager Itemif (mTabLinearLayoutListener != null)mTabLinearLayoutListener.onTab(v, index);// 設定ViewPager的顯示項目。mPager.setCurrentItem(index, true);setCurrentItem(index);}});}// 設定當前LinearLayout的pos項子view被選中。public void setCurrentItem(int pos) {setIndicatorViewSelected(pos);}// 一個介面,當使用者點擊選項卡時候,方法被回調。public interface TabLinearLayoutListener {public void onTab(View v, int pos);public void onTab(int pos);}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.