您好,登錄后才能下訂單哦!
之前用JakeWharton的開源框架ActionBarSherlock和ViewPager實現了對網易新聞客戶端Tab標簽的功能,ActionBarSherlock是在3.0以下的機器支持ActionBar的功能,有興趣的可以看看開源框架ActionBarSherlock 和 ViewPager 仿網易新聞客戶端,今天用到的是JakeWharton的另一開源控件ViewPageIndicator,ViewPager想必大家都知道,Indicator指示器的意思,所以ViewPageIndicator就是ViewPager指示器咯,什么是ViewPager指示器呢?看了下面這副圖你就明白了
你看明白了沒?就是ViewPager的分頁指示器,比如很多應用的指引界面,下面的小圓點,還有一些應用上面的Tab等等,ViewPageIndicator都能很好的實現,今天就繼續帶大家來實現網易新聞的Tab功能
我們先得去Github上面下載這個庫,下載地址:https://github.com/JakeWharton/Android-ViewPagerIndicator,下載下來之后你可以運行例子,來看看我們需要什么樣的效果,然后在此基礎上改成我們自己想要的效果
sample是提供給我們的例子,library是庫工程,我們需要將其作為我們自己項目的依賴庫,我們新建一個Android工程,將library導入工程我就不介紹了,比較簡單相信大家都會,不知道的可以看看我的這篇文章http://blog.csdn.net/xiaanming/article/details/9971721
注:如果你新建的項目libs目錄下面有android-support-v4.jar,你要將其刪除,因為ViewPageIndicator里面有這個庫,我們項目中不允許兩個android-support-v4.jar,不刪除我們的項目不能編譯的
下面我們來介紹主要代碼的編寫了,先看布局文件,上面一個TabPageIndicator,下面一個ViewPager,還是很簡單的布局咯
[html] view plaincopyprint?
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.viewpagerindicator.TabPageIndicator
android:id="@+id/indicator"
android:background="@drawable/base_action_bar_bg"
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>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.viewpagerindicator.TabPageIndicator android:id="@+id/indicator" android:background="@drawable/base_action_bar_bg" 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] view plaincopyprint?
package com.example.viewpageindicator;
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 android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.Toast;
import com.viewpagerindicator.TabPageIndicator;
public class MainActivity extends FragmentActivity {
/**
* Tab標題
*/
private static final String[] TITLE = new String[] { "頭條", "×××", "另一面", "女人",
"財經", "數碼", "情感", "科技" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ViewPager的adapter
FragmentPagerAdapter adapter = new TabPageIndicatorAdapter(getSupportFragmentManager());
ViewPager pager = (ViewPager)findViewById(R.id.pager);
pager.setAdapter(adapter);
//實例化TabPageIndicator然后設置ViewPager與之關聯
TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
indicator.setViewPager(pager);
//如果我們要對ViewPager設置監聽,用indicator設置就行了
indicator.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
Toast.makeText(getApplicationContext(), TITLE[arg0], Toast.LENGTH_SHORT).show();
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
/**
* ViewPager適配器
* @author len
*
*/
class TabPageIndicatorAdapter extends FragmentPagerAdapter {
public TabPageIndicatorAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
//新建一個Fragment來展示ViewPager item的內容,并傳遞參數
Fragment fragment = new ItemFragment();
Bundle args = new Bundle();
args.putString("arg", TITLE[position]);
fragment.setArguments(args);
return fragment;
}
@Override
public CharSequence getPageTitle(int position) {
return TITLE[position % TITLE.length];
}
@Override
public int getCount() {
return TITLE.length;
}
}
}
package com.example.viewpageindicator; 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 android.support.v4.view.ViewPager.OnPageChangeListener; import android.widget.Toast; import com.viewpagerindicator.TabPageIndicator; public class MainActivity extends FragmentActivity { /** * Tab標題 */ private static final String[] TITLE = new String[] { "頭條", "×××", "另一面", "女人", "財經", "數碼", "情感", "科技" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //ViewPager的adapter FragmentPagerAdapter adapter = new TabPageIndicatorAdapter(getSupportFragmentManager()); ViewPager pager = (ViewPager)findViewById(R.id.pager); pager.setAdapter(adapter); //實例化TabPageIndicator然后設置ViewPager與之關聯 TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator); indicator.setViewPager(pager); //如果我們要對ViewPager設置監聽,用indicator設置就行了 indicator.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { Toast.makeText(getApplicationContext(), TITLE[arg0], Toast.LENGTH_SHORT).show(); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }); } /** * ViewPager適配器 * @author len * */ class TabPageIndicatorAdapter extends FragmentPagerAdapter { public TabPageIndicatorAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { //新建一個Fragment來展示ViewPager item的內容,并傳遞參數 Fragment fragment = new ItemFragment(); Bundle args = new Bundle(); args.putString("arg", TITLE[position]); fragment.setArguments(args); return fragment; } @Override public CharSequence getPageTitle(int position) { return TITLE[position % TITLE.length]; } @Override public int getCount() { return TITLE.length; } } }
我們先實例化ViewPager,然后實例化TabPageIndicator,并且要設置TabPageIndicator和ViewPager關聯,就是調用TabPageIndicator的setViewPager(ViewPager view)方法,這樣子我們就實現了點擊上面的Tab,下面的ViewPager切換,滑動ViewPager上面的Tab跟著切換,ViewPager的每一個Item我們使用的是Fragment,使用Fragment可以使我們的布局更加靈活一點,所以建議大家多用用Fragment,你會發現他真的很好用,上面還有一點需要注意的地方,當我們需要給ViewPager設置監聽的時候,我們之前設置OnPageChangeListener直接用ViewPager.setOnPageChangeListener,而現在我們不能這樣子了,TabPageIndicator提供了設置OnPageChangeListener的方法,我們需要調用TabPageIndicator.setOnPageChangeListener來設置監聽
ViewPager的item,也就是Fragment,我這里只用了一個TextView來顯示從MainActivity傳遞過來的Tab的標題,只是為了模擬平常我們實際使用中,從Activity傳遞參數給Fragment,布局我就不貼了,代碼也比較簡單,直接看代碼吧
[java] view plaincopyprint?
package com.example.viewpageindicator;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ItemFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View contextView = inflater.inflate(R.layout.fragment_item, container, false);
TextView mTextView = (TextView) contextView.findViewById(R.id.textview);
//獲取Activity傳遞過來的參數
Bundle mBundle = getArguments();
String title = mBundle.getString("arg");
mTextView.setText(title);
return contextView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
package com.example.viewpageindicator; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class ItemFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View contextView = inflater.inflate(R.layout.fragment_item, container, false); TextView mTextView = (TextView) contextView.findViewById(R.id.textview); //獲取Activity傳遞過來的參數 Bundle mBundle = getArguments(); String title = mBundle.getString("arg"); mTextView.setText(title); return contextView; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } }
當然通過上面幾步我們還不能對網易新聞的模仿,我們還必須簡單修改其樣式,有些東西我是直接從開源框架ActionBarSherlock 和 ViewPager 仿網易新聞客戶端拿出來的,不過這里的樣式修改比用ViewPageIndicator簡單多了,樣式如下
[html] view plaincopyprint?
<style name="StyledIndicators" parent="@android:style/Theme.Light">
<item name="vpiTabPageIndicatorStyle">@style/CustomTabPageIndicator</item>
</style>
<style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator">
<item name="android:background">@drawable/tab_indicator</item>
<item name="android:textAppearance">@style/CustomTabPageIndicator.Text</item>
<item name="android:textSize">14sp</item>
<item name="android:dividerPadding">8dp</item>
<item name="android:showDividers">middle</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:paddingRight">10dp</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>
<item name="android:textColor">@drawable/selector_tabtext</item>
</style>
<style name="StyledIndicators" parent="@android:style/Theme.Light"> <item name="vpiTabPageIndicatorStyle">@style/CustomTabPageIndicator</item> </style> <style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator"> <item name="android:background">@drawable/tab_indicator</item> <item name="android:textAppearance">@style/CustomTabPageIndicator.Text</item> <item name="android:textSize">14sp</item> <item name="android:dividerPadding">8dp</item> <item name="android:showDividers">middle</item> <item name="android:paddingLeft">10dp</item> <item name="android:paddingRight">10dp</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> <item name="android:textColor">@drawable/selector_tabtext</item> </style>
通過上面簡單的幾步我們就實現了對網易新聞的模仿,接下來我們運行下項目看下效果
總結
用ViewPageIndicator實現這樣子的Tab標簽很容易,而且ViewPageIndicator里面還有各種的指示器,相對于用ActionBarSherlock來實現,ViewPageIndicator更加靈活,而且修改ActionBarSherlock的樣式比較復雜,如果你的項目中有用到ActionBarSherlock這個庫,你可以使用來實現Tab分頁,如果Tab上面還加導航條,ActionBarSherlock只能使用ActionBar來實現咯,而使用ViewPageIndicator我們可以更加靈活的使用自己的布局,我們我還是推薦使用ViewPageIndicator,這只是我的個人觀點
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。