最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)底部圖標(biāo)與Fragment的聯(lián)動(dòng)實(shí)例

 更新時(shí)間:2017年07月13日 08:29:56   作者:leehbhs  
本篇文章主要介紹了Android實(shí)現(xiàn)底部圖標(biāo)與Fragment的聯(lián)動(dòng)實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下

本文介紹了ndroid實(shí)現(xiàn)底部圖標(biāo)與Fragment的聯(lián)動(dòng),分享給大家,希望此文章對(duì)各位有所幫助。

效果如下:

1.首先在res下的drawable下新建四個(gè)圖標(biāo)的xml,分別把圖標(biāo)的選中和未選中的狀態(tài)設(shè)置好,所有的圖片可以放在res下新建的一個(gè)drawable-xhdpi目錄下,這里僅展示一個(gè)圖標(biāo)的xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_checked="true" android:drawable="@drawable/ic_nav_home_press"/>
<item android:state_checked="false" android:drawable="@drawable/ic_nav_home"/>
<item android:drawable="@drawable/ic_nav_home"/>
</selector>

2.在布局中開始布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.baway.lizongshu.view.activity.MainActivity">
 <FrameLayout
  android:id="@+id/framelayout"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1"
  >

 </FrameLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
  <RadioGroup
    android:id="@+id/rg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
      android:id="@+id/fenlei"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="分類"
      android:button="@null"
      android:checked="true"
      android:drawableTop="@drawable/fenlei"
      android:gravity="center"
      android:textSize="12sp"
      android:tag="0"
      />
    <RadioButton
      android:id="@+id/gouwuche"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="購(gòu)物車"
      android:button="@null"
      android:drawableTop="@drawable/gouwuche"
      android:gravity="center"
      android:textSize="12sp"
      android:tag="1"
      />
    <RadioButton
      android:id="@+id/qita"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="其他"
      android:button="@null"
      android:drawableTop="@drawable/qita"
      android:gravity="center"
      android:textSize="12sp"
      android:tag="2"
      />

    <RadioButton
      android:id="@+id/wode"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="我的"
      android:button="@null"
      android:drawableTop="@drawable/wode"
      android:gravity="center"
      android:textSize="12sp"
      android:tag="3"
      />
  </RadioGroup>
  </LinearLayout>
</LinearLayout>

3.新建四個(gè)Fragment類,這里僅展示一個(gè)

public class FenleiFragment extends Fragment {
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fenlei, container, false);
    return view;
  }
}

4. 主界面中:

public class MainActivity extends AppCompatActivity {
  private RadioGroup rg;
  private Fragment[] mfragments;
  private FragmentManager fm;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initview();
    initdata();

  }

  private void initdata() {
    //定義一個(gè)Fragment數(shù)組,存放四個(gè)Fragment
    mfragments=new Fragment[4];
    mfragments[0]=new FenleiFragment();
    mfragments[1]=new GouwucheFragment();
    mfragments[2]=new QitaFragment();
    mfragments[3]=new WodeFragment();
    //獲得Fragment管理者
    fm = getSupportFragmentManager();
    //處理
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.framelayout,mfragments[0],"0");
    ft.commit();

  }

  private void initview() {
    rg=(RadioGroup) findViewById(R.id.rg);
    //RadioGroup的監(jiān)聽(tīng)事件
    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
        //找到當(dāng)前選中的圖標(biāo)
      RadioButton rb= (RadioButton) group.findViewById(checkedId);
        //找到所選圖標(biāo)的標(biāo)簽并轉(zhuǎn)換為整數(shù)類型放到下面的方法中
        int i = Integer.parseInt(rb.getTag().toString().trim());
        showAndHideFragment(i);

      }


    });


  }
  //展示和隱藏Fragment的方法
  private void showAndHideFragment(int position) {
    FragmentTransaction transaction = fm.beginTransaction();
    //如果沒(méi)有fragment就在framelayout里面加上
    if (!mfragments[position].isAdded()){
      transaction.add(R.id.framelayout,mfragments[position],""+position);
    }
    //把所有的fragment設(shè)為隱藏
    for (Fragment fragment:mfragments){
      transaction.hide(fragment);
    }
    //把選中的設(shè)為顯示
    transaction.show(mfragments[position]);
    transaction.commit();

  }


}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

陈巴尔虎旗| 维西| 建昌县| 鄂尔多斯市| 兴国县| 井陉县| 平舆县| 乐陵市| 英德市| 张家港市| 汽车| 班戈县| 乌兰浩特市| 化州市| 阜平县| 娱乐| 南木林县| 疏勒县| 象州县| 宁波市| 辛集市| 嘉禾县| 将乐县| 罗平县| 萝北县| 左云县| 福清市| 彭水| 湘阴县| 湾仔区| 高唐县| 屏东市| 洛宁县| 澄江县| 体育| 肥西县| 罗平县| 昌吉市| 寻甸| 东乌珠穆沁旗| 平湖市|