Fragment does not support left and right sliding tabs. fragmenttab

Source: Internet
Author: User

Fragment does not support left and right sliding tabs. fragmenttab

Main Idea: top title top. xml, middle Fragment, bottom Tab navigation.

Specific implementation of top. xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="45dp"android:background="@drawable/title_bar"android:gravity="center"android:orientation="vertical" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text=""android:textColor="#ffffff"android:textSize="20sp"android:textStyle="bold" /></LinearLayout>

 

 

Bottom. xml implementation:

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "55dp" android: background = "@ drawable/bottom_bar" android: orientation = "horizontal"> <LinearLayoutandroid: id = "@ + id/id_tab_weixin" android: layout_width = "0dp" android: layout_height = "fill_parent" android: layout_weight = "1" android: gravity = "center" android: orientation = "vertical"> <ImageButtonandroid: id = "@ + id/id_tab_weixin_img" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: background = "#00000000" android: clickable = "false" android: src = "@ drawable/tab_weixin_pressed"/> <TextViewandroid: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "" android: textColor = "# ffffff"/> </LinearLayout> <LinearLayoutandroid: id = "@ + id/id_tab_frd" android: layout_width = "0dp" android: layout_height = "fill_parent" android: layout_weight = "1" android: gravity = "center" android: orientation = "vertical"> <ImageButtonandroid: id = "@ + id/id_tab_frd_img" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: background = "#00000000" android: clickable = "false" android: src = "@ drawable/tab_find_frd_normal"/> <TextViewandroid: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "friend" android: textColor = "# ffffff"/> </LinearLayout> <LinearLayoutandroid: id = "@ + id/id_tab_address" android: layout_width = "0dp" android: layout_height = "fill_parent" android: layout_weight = "1" android: gravity = "center" android: orientation = "vertical"> <ImageButtonandroid: id = "@ + id/id_tab_address_img" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: background = "#00000000" android: clickable = "false" android: src = "@ drawable/tab_address_normal"/> <TextViewandroid: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Address Book" android: textColor = "# ffffff"/> </LinearLayout> <LinearLayoutandroid: id = "@ + id/id_tab_settings" android: layout_width = "0dp" android: layout_height = "fill_parent" android: layout_weight = "1" android: gravity = "center" android: orientation = "vertical"> <ImageButtonandroid: id = "@ + id/id_tab_settings_img" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: background = "#00000000" android: clickable = "false" android: src = "@ drawable/tab_settings_normal"/> <TextViewandroid: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Settings" android: textColor = "# ffffff"/> </LinearLayout>

 

 

Activity_main.xml:

<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:orientation="vertical" ><include layout="@layout/top" /><FrameLayout android:id="@+id/id_content"android:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"></FrameLayout><include layout="@layout/bottom" /></LinearLayout>

 

 

WeixinFragment. java implementation:

package com.imooc.tab02;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class WeixinFragment extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){return inflater.inflate(R.layout.tab01, container, false);}}

 

The implementation of the other three Fragment is similar.

MainActivity. java Implementation: package com. imooc. tab02; import android. OS. bundle; import android. support. v4.app. fragment; import android. support. v4.app. fragmentActivity; import android. support. v4.app. fragmentManager; import android. support. v4.app. fragmentTransaction; import android. view. view; import android. view. view. onClickListener; import android. view. window; import android. widget. imageButton; import android. widget. LinearLayout; public class MainActivity extends FragmentActivity implements OnClickListener {private LinearLayout listener; private LinearLayout mTabAddress; private LinearLayout mTabSettings; private extends listener; private extends mImgAddress; private ImageButton mImgSettings; private Fragment mTab01; private Fragment mTab02; private Fragment mTab03; private Fragment mTab04; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_main); initView (); initEvent (); setSelect (0);} private void initEvent () {mTabWeixin. setOnClickListener (this); mTabFrd. setOnClickListener (this); mTabAddress. setOnClickListener (this); mTabSettings. SetOnClickListener (this);} private void initView () {mTabWeixin = (LinearLayout) findViewById (R. id. id_tab_weixin); mTabFrd = (LinearLayout) findViewById (R. id. id_tab_frd); mTabAddress = (LinearLayout) findViewById (R. id. id_tab_address); mTabSettings = (LinearLayout) findViewById (R. id. id_tab_settings); mImgWeixin = (ImageButton) findViewById (R. id. id_tab_weixin_img); mImgFrd = (ImageButton) findViewById (R. id. Id_tab_frd_img); mImgAddress = (ImageButton) findViewById (R. id. id_tab_address_img); mImgSettings = (ImageButton) findViewById (R. id. id_tab_settings_img);} private void setSelect (int I) {FragmentManager fm = getSupportFragmentManager (); FragmentTransaction transaction = fm. beginTransaction (); hideFragment (transaction); // set the image to bright. // set the content area to switch (I) {case 0: if (mTab01 = null) {mTab01 = new WeixinFragment (); Transaction. add (R. id. id_content, mTab01);} else {transaction. show (mTab01);} mImgWeixin. setImageResource (R. drawable. tab_weixin_pressed); break; case 1: if (mTab02 = null) {mTab02 = new FrdFragment (); transaction. add (R. id. id_content, mTab02);} else {transaction. show (mTab02);} mImgFrd. setImageResource (R. drawable. tab_find_frd_pressed); break; case 2: if (mTab03 = null) {mTab03 = new AddressFragment (); transactio N. add (R. id. id_content, mTab03);} else {transaction. show (mTab03);} mImgAddress. setImageResource (R. drawable. tab_address_pressed); break; case 3: if (mTab04 = null) {mTab04 = new SettingFragment (); transaction. add (R. id. id_content, mTab04);} else {transaction. show (mTab04);} mImgSettings. setImageResource (R. drawable. tab_settings_pressed); break; default: break;} transaction. commit ();} private void hideFragment (Fragm EntTransaction transaction) {if (mTab01! = Null) {transaction. hide (mTab01);} if (mTab02! = Null) {transaction. hide (mTab02);} if (mTab03! = Null) {transaction. hide (mTab03);} if (mTab04! = Null) {transaction. hide (mTab04) ;}@overridepublic void onClick (View v) {resetImgs (); switch (v. getId () {case R. id. id_tab_weixin: setSelect (0); break; case R. id. id_tab_frd: setSelect (1); break; case R. id. id_tab_address: setSelect (2); break; case R. id. id_tab_settings: setSelect (3); break; default: break;}/*** switch the image to a dark color */private void resetImgs () {mImgWeixin. setImageResource (R. drawable. tab_weixin_normal); mImgFrd. setImageResource (R. drawable. tab_find_frd_normal); mImgAddress. setImageResource (R. drawable. tab_address_normal); mImgSettings. setImageResource (R. drawable. tab_settings_normal );}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.