關(guān)于實(shí)現(xiàn)網(wǎng)易新聞客戶端的界面,以前寫過很多博客,請(qǐng)參考:
今天用ViewPager + FragmentAdapter + ViewPagerIndicator來實(shí)現(xiàn)。
ViewPagerIndicator是一款分頁指標(biāo)小部件兼容ViewPager,封裝上做得非常不錯(cuò),目前已為眾多知名應(yīng)用所使用。具體API的使用,大家可以下載官方demo示例研究研究就知道啦!
sample是提供給我們的例子,library是庫工程,我們需要將其作為我們自己項(xiàng)目的依賴庫,我們新建一個(gè)Android工程,將library導(dǎo)入工程我就不介紹了。
注:
Eclipse:如果你新建的項(xiàng)目libs目錄下面有android-support-v4.jar,你要將其刪除,因?yàn)閂iewPageIndicator里面有這個(gè)庫,我們項(xiàng)目中不允許兩個(gè)android-support-v4.jar,不刪除我們的項(xiàng)目不能編譯的。
Android Studio:直接import module就行。
activity_main.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
歐冠直播android:orientation="vertical" >
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent" >
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEF3FA">
布局很簡(jiǎn)單,頂部引入了一個(gè)activity_top.xml的布局,具體內(nèi)容可以自己定義。
MainActivity.java
package com.jackie.neteasenews;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
歐冠直播import com.viewpagerindicator.TabPageIndicator;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends FragmentActivity {
private TabPageIndicator mTabPageIndicator;
private ViewPager mViewPager;
private ViewPagerIndicatorAdapter mAdapter;
private HeadlineFragment mHeadlineFragment;
private EnjoyFragment mEnjoyFragment;
private HotspotFragment mHotspotFragment;
private SportFragment mSportFragment;
private HouseFragment mHouseFragment;
private List mFragmentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mTabPageIndicator = (TabPageIndicator) findViewById(R.id.indicator);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mHeadlineFragment = new HeadlineFragment();
mEnjoyFragment = new EnjoyFragment();
mHotspotFragment = new HotspotFragment();
mSportFragment = new SportFragment();
mHouseFragment = new HouseFragment();
mFragmentList = new ArrayList<>();
mFragmentList.add(mHeadlineFragment);
mFragmentList.add(mEnjoyFragment);
mFragmentList.add(mHotspotFragment);
mFragmentList.add(mSportFragment);
mFragmentList.add(mHouseFragment);
mAdapter = new ViewPagerIndicatorAdapter(getSupportFragmentManager(), mFragmentList);
mViewPager.setAdapter(mAdapter);
//實(shí)例化TabPageIndicator然后設(shè)置ViewPager與之關(guān)聯(lián)
mTabPageIndicator.setViewPager(mViewPager, 1);
}
}
ViewPagerIndicatorAdapter.java
package com.jackie.neteasenews;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
public class ViewPagerIndicatorAdapter extends FragmentPagerAdapter {
private List mFragmentList;
public static String[] TITLES = new String[] { "頭條", "娛樂", "熱點(diǎn)", "體育", "房產(chǎn)" };
public ViewPagerIndicatorAdapter(FragmentManager fm, List fragmentList) {
super(fm);
this.mFragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
}
代碼很簡(jiǎn)單,但是有一點(diǎn),上面的Indicator是系統(tǒng)默認(rèn)的,不太好看,所以還需要在styles.xml添加下面的樣式:
@style/CustomTabPageIndicator
true
5000
@null
center
@drawable/tab_indicator
22dip
22dip
8dp
8dp
@style/CustomTabPageIndicator.Text
16sp
1
@drawable/tab_text
注意:開發(fā)中過程中跟Fragment相關(guān)的類,導(dǎo)入包時(shí)會(huì)提示兩個(gè)包android.app 和 android.support.v4.app,切記,要保證所有類都導(dǎo)入同一個(gè)包下的,否則會(huì)編譯報(bào)錯(cuò)。
效果圖如下:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持找一找教程網(wǎng)。
24直播網(wǎng) 24直播網(wǎng) 歐洲杯直播免費(fèi)看