仿网易选项卡的实现
来源:互联网 发布:淘宝卖家怎么用手机 编辑:程序博客网 时间:2024/05/21 16:17
Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的Android Design Support Library,在这个support库里面,Google给我们提供了更加规范的MD设计风格的控件。最重要的是,Android Design Support Library的兼容性更广,直接可以向下兼容到Android 2.2。
app/build.gradle
apply plugin: 'com.android.application'android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.hd.tabviewtest" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.2.0' compile 'com.android.support:support-annotations:23.2.0' compile 'com.android.support:design:23.2.0'}
需要添加compile ‘com.android.support:design:23.2.0’包。
layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.hd.tabviewtest.MainActivity"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" 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.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" ></android.support.v4.view.ViewPager> </android.support.design.widget.AppBarLayout></LinearLayout>
这里需要注意控件所属的包别用错了,android.widget.TabLayout也有一个TabLayout,我们需要使用design.support包中的控件。
com.hd.tabviewtest.MainActivity
package com.hd.tabviewtest;import android.support.design.widget.TabLayout;import android.support.v4.app.Fragment;import android.support.v4.view.ViewPager;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 MainActivity extends AppCompatActivity { private Toolbar toolbar; private ViewPager viewPager; private TabLayout mTablayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); viewPager = (ViewPager) findViewById(R.id.viewpager); initViewger(); } private void initViewger() { mTablayout = (android.support.design.widget.TabLayout) findViewById(R.id.tabs); List<String> 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("精选"); for (int i=0;i<titles.size();i++) { mTablayout.addTab(mTablayout.newTab().setText(titles.get(i))); } List<Fragment> fragmentList = new ArrayList<>(); for (int i =0;i<titles.size();i++) { ListFragment fragment = new ListFragment(); Bundle bundle = new Bundle(); bundle.putString("title",titles.get(i)); fragment.setArguments(bundle); fragmentList.add(fragment); } FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), fragmentList, titles); viewPager.setAdapter(adapter); mTablayout.setupWithViewPager(viewPager); }}
这里主要说下mTablayout.setupWithViewPager(viewPager)这个方法。这个方法会把TabLayout和ViewPager进行关联,无论哪一个控件改变,另外一个控件都会对应做出改变。当然,这是因为适配器的功能,当进行页面切换时,viewpger会回调适配器中的getItem方法去加载对应Frament,而TabLayout也会回调适配器中的getPageTitle方法。
layout/fragment_1.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="fragment1"/></LinearLayout>
package com.hd.tabviewtest;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;/** * Created by hd on 2016/3/5. */public class ListFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { String title = getArguments().getString("title"); View view = inflater.inflate(R.layout.fragment_1, container, false); TextView mTextView = (TextView) view.findViewById(R.id.textView); mTextView.setText(title); return view; }}
com.hd.tabviewtest.FragmentAdapter
package com.hd.tabviewtest;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentStatePagerAdapter;import java.util.List;/** * Created by hd on 2016/3/5. */public class FragmentAdapter extends FragmentStatePagerAdapter { private List<Fragment> mFragmentList; private List<String> mTitleList; public FragmentAdapter(FragmentManager fm, List<Fragment> fragmentsList, List<String> TitleList) { super(fm); mFragmentList = fragmentsList; mTitleList = TitleList; } @Override public Fragment getItem(int position) { return mFragmentList.get(position); } @Override public int getCount() { return mFragmentList.size(); } @Override public CharSequence getPageTitle(int position) { return mTitleList.get(position); }}
1 0
- 仿网易选项卡的实现
- 仿网易选项卡制作
- Android实现仿网易首页选项卡动态滑动效果
- 使用ViewPager+Fragment+仿网易云音乐的tab选项栏来实现多屏滑动
- JS+CSS仿网易的选项卡TAB标签样式-导航菜单类
- JS+CSS仿网易的选项卡TAB标签样式-导航菜单类 .
- 仿网易div css滑动门选项卡效果
- 仿网易、人人滑动界面的实现
- javascript实现仿网易
- tab选项卡,类似网易新闻的
- 仿网易彩票代码实现
- iOS viewDeck和scrollView结合实现的侧滑栏(仿网易)
- 仿网易新闻广告启动页的实现
- 仿网易ios滚动条控件的实现
- Android仿网易云音乐转盘的实现
- Qt音乐进度条的实现仿网易云音乐
- 仿网易云播放界面圆盘动画的实现
- 仿windows选项卡
- GuideMap 登录界面 详细文档(三)
- 每个Xcode开发者应该知道的七个使用技巧
- BZOJ2223(可持久化线段树)
- Leet Code OJ 189. Rotate Array [Difficulty: Easy]
- Web标准
- 仿网易选项卡的实现
- hdu 1505 动态规划
- BZOJ2101(DP)
- CentOS命令行写c程序
- Linux系统运维/Bash/5-4-Bash操作环境
- Double Queue(SET水题)
- 高加索
- android:theme常用相关属性值
- 读《经济解释》卷一