Android uses Fragment and ViewPagerIndicator to create the main csdn app framework, androidfragment
Reprint reprint please indicate the source: http://blog.csdn.net/lmj623565791/article/details/23513993
I was going to download a CSDN client and let it go on the machine. I was able to browse the information. After downloading the official client, I found that it was not very easy to use. I just found a csdn app written by a great god. After downloading and installing it, I felt very good and smooth, basically meeting our daily browsing needs.
App:
I will introduce the creation of this project in my blog. The first article is of course the overall structure of the entire project.
:
1. layout file in the header, which is very simple:
<?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>
An icon and title are displayed.
2. Main layout file:
<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>
One TabPageIndicator and one ViewPager.
3. Main 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 [] {"industry", "mobile", "R & D", "Programmer Magazine ", "Cloud computing"}; 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. Customize Theme in styles. xml
<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>
Register and use in 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>
The overall process is quite simple, but the results are quite good. Isn't Fragment popular now ~ Okay. If this article is helpful to you, just like it ~
What are the differences between the two packages? -------- supportv4app
Fagment is only available in version 3.0, So google has a support package to be compatible with version 3.0.