Android 使用Fragment,ViewPagerIndicator 製作csdn app主要架構,androidfragment

來源:互聯網
上載者:User

Android 使用Fragment,ViewPagerIndicator 製作csdn app主要架構,androidfragment

轉載  轉載請註明出處:http://blog.csdn.net/lmj623565791/article/details/23513993

本來準備下載個CSDN的用戶端放手機上,沒事可以瀏覽瀏覽資訊,下載了官方的之後,發現並不能很好的使用。恰好搜到一個大神自己寫的csdn的app,下載安裝了一下,感覺很不錯,也很流暢,基本滿足了我們 日常瀏覽的需求。

app:


 

 







我會在部落格中完整的介紹這個項目的製作,第一篇當然是整個項目的整體結構了。

:


1、頭部的布局檔案,這個很簡單:

 

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.zhy.csdndemo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="13"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.zhy.csdndemo.MainActivity"            android:label="@string/app_name"             android:theme="@style/MyTheme">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

 

 

 

就顯示一個表徵圖和標題。
2、主布局檔案:

 

 

<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"    android:background="#eee"    android:orientation="vertical" >    <include layout="@layout/main_head" />    <com.viewpagerindicator.TabPageIndicator        android:id="@+id/id_indicator"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="@color/transparentblue" >    </com.viewpagerindicator.TabPageIndicator>    <android.support.v4.view.ViewPager        android:id="@+id/id_pager"        android:layout_width="fill_parent"        android:layout_height="0dp"        android:layout_weight="1" /></LinearLayout>

 

 

一個TabPageIndicator和一個ViewPager。
3、主Activity

     
package com.zhy.csdndemo;import com.viewpagerindicator.TabPageIndicator;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentPagerAdapter;import android.support.v4.view.ViewPager;public class MainActivity extends FragmentActivity{    private TabPageIndicator mIndicator ;    private ViewPager mViewPager ;    private FragmentPagerAdapter mAdapter ;    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);            mIndicator = (TabPageIndicator) findViewById(R.id.id_indicator);        mViewPager = (ViewPager) findViewById(R.id.id_pager);        mAdapter = new TabAdapter(getSupportFragmentManager());        mViewPager.setAdapter(mAdapter);        mIndicator.setViewPager(mViewPager, 0);                            }        }

 

TabAdapter.java

 
package com.zhy.csdndemo;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;public class TabAdapter extends FragmentPagerAdapter{    public static final String[] TITLES = new String[] { "業界", "移動", "研發", "程式員雜誌", "雲端運算" };    public TabAdapter(FragmentManager fm)    {        super(fm);    }    @Override    public Fragment getItem(int arg0)    {        MainFragment fragment = new MainFragment(arg0);        return fragment;    }    @Override    public CharSequence getPageTitle(int position)    {        return TITLES[position % TITLES.length];    }    @Override    public int getCount()    {        return TITLES.length;    }}

 

  

MainFragment.java

  

package com.zhy.csdndemo;import android.annotation.SuppressLint;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;@SuppressLint("ValidFragment")public class MainFragment extends Fragment{    private int newsType = 0;    public MainFragment(int newsType)    {        this.newsType = newsType;    }    @Override    public void onActivityCreated(Bundle savedInstanceState)    {        super.onActivityCreated(savedInstanceState);    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)    {        View view = inflater.inflate(R.layout.tab_item_fragment_main, null);        TextView tip = (TextView) view.findViewById(R.id.id_tip);        tip.setText(TabAdapter.TITLES[newsType]);        return view;    }}

 

 

 

4、在styles.xml中自訂Theme
  

<style name="MyTheme" parent="AppBaseTheme">        <item name="vpiTabPageIndicatorStyle">@style/MyWidget.TabPageIndicator</item>        <item name="android:windowBackground">@drawable/init_pic</item>        <item name="android:windowNoTitle">true</item>          <item name="android:animationDuration">5000</item>        <item name="android:windowContentOverlay">@null</item>      </style>        <style name="MyWidget.TabPageIndicator" parent="Widget">        <item name="android:gravity">center</item>        <item name="android:background">@drawable/vpi__tab_indicator</item>        <item name="android:paddingLeft">22dip</item>        <item name="android:paddingRight">22dip</item>        <item name="android:paddingTop">8dp</item>        <item name="android:paddingBottom">8dp</item>        <item name="android:textAppearance">@style/MyTextAppearance.TabPageIndicator</item>        <item name="android:textSize">16sp</item>        <item name="android:maxLines">1</item>    </style>    <style name="MyTextAppearance.TabPageIndicator" parent="Widget">        <item name="android:textStyle">bold</item>        <item name="android:textColor">@color/black</item>    </style>

 


在AndroidManifest中註冊使用:

  
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.zhy.csdndemo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="13"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.zhy.csdndemo.MainActivity"            android:label="@string/app_name"             android:theme="@style/MyTheme">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

 


總體還是很簡單的,但是效果很不錯啊,現在不是流行Fragment麼~好了 ,如果這篇文章對你有協助,贊一個~


兩個包有什不同app--------supportv4app

fagment是3.0才有的,所以google出了support包來相容3.0一下版本
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.