編輯:關於Android編程
此前我們用HorizontalScrollView也實現了類似網易選項卡動態滑動效果,詳見 Android選項卡動態滑動效果這篇文章
這裡我們用TabLayout來實現這一效果。TabLayout是Android Design Support Library庫中的控件。
Google在2015的IO大會上,給我們帶來了更加詳細的Material Design設計規范,同時,也給我們帶來了全新的Android Design Support Library,在這個support庫裡面,Google給我們提供了更加規范的MD設計風格的控件。最重要的是,Android Design Support Library的兼容性更廣,直接可以向下兼容到Android 2.2。
首先我們先來看看效果:
接下來開始實現<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPjxzdHJvbmc+MS4gxeTWw2J1aWxkLmdyYWRsZTwvc3Ryb25nPjwvcD4NCjxwPtTaYnVpbGQuZ3JhZGxlvNPI68jnz8K0+sLrPC9wPg0KPHByZSBjbGFzcz0="brush:java;">
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:recyclerview-v7:22.2.0'
compile 'com.android.support:cardview-v7:22.2.0'
}
com.android.support:design:22.2.0就是我們需要引入的Android Design Support Library,其次我們還引入了Recyclerview和Cardview,還不了解這兩個控件的同學可以看下面這兩篇文章:
Android5.x RecyclerView 應用解析和Android5.x CardView 應用解析
2.AppBarLayout,Toolbar與TabLayout
先看看主界面的布局 (activity_tab_layout.xml)
<code class=" hljs avrasm"><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".TabLayoutActivity" android:orientation="vertical"> <android.support.design.widget.appbarlayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_scrollflags="scroll|enterAlways" app:popuptheme="@style/ThemeOverlay.AppCompat.Light"> <android.support.design.widget.tablayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabindicatorcolor="#ADBE107E" app:tabmode="scrollable"> </android.support.design.widget.tablayout></android.support.v7.widget.toolbar></android.support.design.widget.appbarlayout> <android.support.v4.view.viewpager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> </android.support.v4.view.viewpager></linearlayout> </code>
這裡用到了AppBarLayout和Toolbar,AppBarLayout是Android Design Support Library新加的控件繼承自LinearLayout,它用來將Toolbar和TabLayout組合起來作為一個整體。Toolbar我們在這裡不講了,如果不熟悉可以看Android5.x Toolbar和Palette應用解析這篇文章
這布局文件最關鍵的一點就是android.support.design.widget.TabLayout 標簽中的app:tabMode=”scrollable”,他設置tab的模式為“可滑動的”,現在我們把這句話去掉,來看看效果:
上面的tab由於太多(13個)卻不能滑動就重疊了。
接下來在java中引用 (TabLayoutActivity.java)
package com.example.liuwangshu.mytablayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
public class TabLayoutActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ViewPager mViewPager;
private TabLayout mTabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
initViewPager();
}
initViewPager方法 (TabLayoutActivity.java)
private void initViewPager() {
mTabLayout = (TabLayout) findViewById(R.id.tabs);
List titles = new ArrayList<>();
titles.add("精選");
titles.add("體育");
titles.add("巴薩");
titles.add("購物");
titles.add("明星");
titles.add("視頻");
titles.add("健康");
titles.add("勵志");
titles.add("圖文");
titles.add("本地");
titles.add("動漫");
titles.add("搞笑");
titles.add("精選");
for(int i=0;i fragments = new ArrayList<>();
for(int i=0;i
在這裡我們設定了13個標題內容並創建了相應的TabLayout和Fragment,設置了ViewPager適配器和TabLayout適配器並將將TabLayout和ViewPager關聯起來。
ListFragment的代碼(ListFragment.java)
package com.example.liuwangshu.mytablayout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ListFragment extends Fragment {
private RecyclerView mRecyclerView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRecyclerView =
(RecyclerView) inflater.inflate(R.layout.list_fragment, container, false);
return mRecyclerView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mRecyclerView.setLayoutManager(new LinearLayoutManager(mRecyclerView.getContext()));
mRecyclerView.setAdapter(new RecyclerViewAdapter(getActivity()));
}
}
這裡用RecyclerView來代替ListView來看看RecyclerViewAdapter(RecyclerViewAdapter.java)
package com.example.liuwangshu.mytablayout;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class RecyclerViewAdapter extends RecyclerView.Adapter {
private Context mContext;
public RecyclerViewAdapter(Context mContext) {
this.mContext = mContext;
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_card_main, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final RecyclerViewAdapter.ViewHolder holder, int position) {
final View view = holder.mView;
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public int getItemCount() {
return 10;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public ViewHolder(View view) {
super(view);
mView = view;
}
}
}
最後FragmentAdapter(FragmentAdapter.java)
package com.example.liuwangshu.mytablayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import java.util.List;
public class FragmentAdapter extends FragmentStatePagerAdapter {
private List mFragments;
private List mTitles;
public FragmentAdapter(FragmentManager fm, List fragments, List titles) {
super(fm);
mFragments = fragments;
mTitles = titles;
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mTitles.get(position);
}
}
基本所有的代碼都講到了,當然這種稍微復雜的效果TabLayout能夠實現,那麼簡單的3,4個Tab滑動TabLayout實現起來更是不再話下,修改TabLayoutActivity的initViewPager方法(TabLayoutActivity.java)
private void initViewPager() {
mTabLayout = (TabLayout) findViewById(R.id.tabs);
List titles = new ArrayList<>();
titles.add("精選");
titles.add("體育");
titles.add("巴薩");
titles.add("購物");
for(int i=0;i fragments = new ArrayList<>();
for(int i=0;i
我們只保留了4個Tab,然後去掉activity_tab_layout.xml android.support.design.widget.TabLayout 標簽中的app:tabMode=”scrollable”
運行代碼來看看效果
好了講到這裡,預告:接下來我們會講到Android Design Support Library提供的其他的控件
自定義輪播圖CarouselView 自定義下拉刷新PullRefreshListView 馬上就要正式做畢業設計了,一些零碎時間寫其中的一個模塊,現記錄下來,以備以後忘
activity的啟動流程加載一個Activity肯定不會像加載一般的類那樣,因為activity作為系統的組件有自己的生命周期,有系統的很多回調控制,所以自定義一個De
將自己的編程經歷寫出來是個好習慣先來效果圖:項目結構:1、底部導航底部導航的實現思路,使用button等可點擊控件並置於父布局底部,配合viewpage或fragment
版本:1.0 日期:2014.11.25 2014.11.26版權:©kince特別推薦:泡在網上的日子一、概述 一般Launcher都帶有壁紙設置的功能,A