国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現底部圖標與Fragment的聯動實例

2019-10-22 18:32:25
字體:
來源:轉載
供稿:網友

本文介紹了ndroid實現底部圖標與Fragment的聯動,分享給大家,希望此文章對各位有所幫助。

效果如下:

Android圖標與Fragment聯動,Android圖標聯動,Android聯動圖標

1.首先在res下的drawable下新建四個圖標的xml,分別把圖標的選中和未選中的狀態設置好,所有的圖片可以放在res下新建的一個drawable-xhdpi目錄下,這里僅展示一個圖標的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="購物車"      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.新建四個Fragment類,這里僅展示一個

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() {    //定義一個Fragment數組,存放四個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的監聽事件    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {      @Override      public void onCheckedChanged(RadioGroup group, int checkedId) {        //找到當前選中的圖標      RadioButton rb= (RadioButton) group.findViewById(checkedId);        //找到所選圖標的標簽并轉換為整數類型放到下面的方法中        int i = Integer.parseInt(rb.getTag().toString().trim());        showAndHideFragment(i);      }    });  }  //展示和隱藏Fragment的方法  private void showAndHideFragment(int position) {    FragmentTransaction transaction = fm.beginTransaction();    //如果沒有fragment就在framelayout里面加上    if (!mfragments[position].isAdded()){      transaction.add(R.id.framelayout,mfragments[position],""+position);    }    //把所有的fragment設為隱藏    for (Fragment fragment:mfragments){      transaction.hide(fragment);    }    //把選中的設為顯示    transaction.show(mfragments[position]);    transaction.commit();  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长岭县| 宁夏| 亳州市| 洛川县| 阜阳市| 阿克陶县| 阆中市| 靖安县| 渭源县| 南开区| 凤翔县| 乌苏市| 增城市| 正安县| 桐庐县| 巴林左旗| 吉安市| 乌拉特前旗| 孝义市| 义马市| 布拖县| 灵丘县| 佛冈县| 鲁甸县| 威远县| 东方市| 南召县| 义乌市| 治多县| 荣昌县| 石首市| 广平县| 兴隆县| 清新县| 乌什县| 利津县| 万宁市| 广东省| 休宁县| 曲阜市| 内丘县|