精华内容
下载资源
问答
  • FrameLayout布局切换

    2018-12-20 20:40:23
    android:id="@+id/rg" android:layout_width="100dp" android:layout_height="wrap_content" > <RadioButton android:id="@+id/radioButton1"

    1,activity_main

    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="100dp"
        android:layout_height="wrap_content" >
    
        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:text="商家1"
            android:button="@null"
            android:gravity="center"
            />
    
        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:text="商家2"
            android:button="@null"
            android:gravity="center"/>
    
        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:text="商家3"
            android:button="@null"
            android:gravity="center"/>
    </RadioGroup>
    
    <FrameLayout
        android:id="@+id/fram"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"></FrameLayout>
    

    2,MainActivity

    public class MainActivity extends FragmentActivity {
    
        private FrameLayout fram;
        private RadioGroup rg;
        private Fra1 fra1=new Fra1();
        private Fra2 fra2=new Fra2();
        private Fra3 fra3=new Fra3();
        private FragmentManager manager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            fram = findViewById(R.id.fram);
            rg = findViewById(R.id.rg);
            manager = getSupportFragmentManager();
    
    
            manager.beginTransaction().replace(R.id.fram,fra1).commit();
    
            rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    FragmentTransaction tran = manager.beginTransaction();
                    switch (checkedId)
                    {
                        case R.id.radioButton1:
                            tran.replace(R.id.fram,fra1);
    
                            break;
                        case R.id.radioButton2:
                            tran.replace(R.id.fram,fra2);
    
                            break;
                        case R.id.radioButton3:
                            tran.replace(R.id.fram,fra3);
                            break;
                    }
    
                    tran.commit();
                }
            });
    
    
        }
    }
    
    展开全文
  • FrameLayout按钮切换页面

    千次阅读 2018-12-20 20:13:20
    一、xml <FrameLayout android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="TextView...

    一、xml

    <FrameLayout
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="TextView" />
    
        <RadioGroup
            android:id="@+id/radiogroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <RadioButton
                android:id="@+id/radioButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:gravity="center"
                android:drawableTop="@drawable/sul_button1"
                android:text="@string/a" />
    
            <RadioButton
                android:id="@+id/radioButton2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:gravity="center"
                android:drawableTop="@drawable/sul_button2"
                android:text="@string/b" />
    
            <RadioButton
                android:id="@+id/radioButton3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:gravity="center"
                android:drawableTop="@drawable/sul_button3"
                android:text="@string/c" />
        </RadioGroup>

    二、Activity

    public class Main2Activity extends AppCompatActivity {
    
        private FrameLayout pager;
        private RadioGroup group;
        private ArrayList<Fragment> list;
        private Fragment01 fragment01;
        private Fragment02 fragment02;
        private Fragment03 fragment03;
        private FragmentManager manager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
    
            //初始化
            pager = findViewById(R.id.pager);
            group = findViewById(R.id.radiogroup);
    
            //获取事物
            manager = getSupportFragmentManager();
            //开启事物
            FragmentTransaction transaction = manager.beginTransaction();
            //new新的界面
            fragment01 = new Fragment01();
            fragment02 = new Fragment02();
            fragment03 = new Fragment03();
            //事物添加
            transaction.add(R.id.pager,fragment01);
            transaction.add(R.id.pager,fragment02);
            transaction.add(R.id.pager,fragment03);
            //展示一个界面隐藏另外两个
            transaction.show(fragment01).hide(fragment02).hide(fragment03);
            //提交事物
            transaction.commit();
    
            //默认选中第一个界面
            group.check(group.getChildAt(0).getId());
    
            //点击界面
            group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    //开启事物
                    FragmentTransaction transaction1 = manager.beginTransaction();
                    //选择
                    switch (checkedId){
                        case R.id.radioButton:
                            //展示一个界面,隐藏另外两个界面
                            transaction1.show(fragment01).hide(fragment02).hide(fragment03);
                            break;
                        case R.id.radioButton2:
                            transaction1.show(fragment02).hide(fragment01).hide(fragment03);
                            break;
                        case R.id.radioButton3:
                            transaction1.show(fragment03).hide(fragment01).hide(fragment02);
                            break;
                    }
                    //提交事物
                    transaction1.commit();
                }
            });
        }
    }
    

     

    展开全文
  • 遇到的问题,之前往FrameLayout中添加Fragment是通过replace去添加的 getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.framelayout, fragment).commit(); 但是出现了Fragment...

    实现的效果图:

    遇到的问题,之前往FrameLayout中添加Fragment是通过replace去添加的

    getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.framelayout, fragment).commit();

    但是出现了Fragment一切换就重新创建的问题,后来采用了add去添加的方式,让Fragment只添加一次,首先对Fragment进行隐藏,然后再去添加Fragment,添加过后再次切换时去判断Fragment是否已经存在了,存在就去显示,不存在则创建。

    上代码一看便知

    public class MainActivity extends BaseActivity {
    
        private static final String TAG = "MainActivity";
        private BottomNavigationBar mBottomNavigationBar;
        private FrameLayout framelayout;
        private int lastSelectedPosition = 0;
        private WarningMonitoringFragment warningInquireFragment;
        private DataInquireFragment dataInquireFragment;
        private PersonalSettingFragment personalSettingFragment;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
            initData();
    
        }
    
    
        private void initView() {
            framelayout = findViewById(R.id.framelayout);
            mBottomNavigationBar = findViewById(R.id.bottom_navigation);
            setBottomNavigationBar();
    
        }
    
        private void initData() {
    
        }
    
        /**
         * 设置BottomNavigationBar属性
         */
        private void setBottomNavigationBar() {
            // TODO 设置模式
            mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
            // TODO 设置背景色样式
            mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
            mBottomNavigationBar.setBarBackgroundColor(R.color.bottombarback);
    
            mBottomNavigationBar
                    .addItem(new BottomNavigationItem(R.mipmap.yujingjiankong_img_unselect, "预警监控").setActiveColorResource(R.color.selecttextcolor).setInactiveIconResource(R.mipmap.yujingjiankong_img_select).setInActiveColorResource(R.color.unselecttextcolor))
                    .addItem(new BottomNavigationItem(R.mipmap.shujuchaxun_img_unselect, "资讯").setActiveColorResource(R.color.selecttextcolor).setInactiveIconResource(R.mipmap.shujuchaxun_img_select).setInActiveColorResource(R.color.unselecttextcolor))
                    .addItem(new BottomNavigationItem(R.mipmap.gerenshezhi_img_unselect, "我的").setActiveColorResource(R.color.selecttextcolor).setInactiveIconResource(R.mipmap.gerenshezhi_img_select).setInActiveColorResource(R.color.unselecttextcolor))
                    .setFirstSelectedPosition(lastSelectedPosition)
                    .initialise();
    
            /**
             * 从这里开始
             */ 
    
    
            //实例化默认显示的WarningMonitoringFragment
            warningInquireFragment = new WarningMonitoringFragment();
            addFragment(warningInquireFragment);
    
            mBottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
                @Override
                public void onTabSelected(int position) {
                    lastSelectedPosition = position;
                    hideFragment();
                    switch (position) {
                        case 0:
    
                            if (warningInquireFragment!=null){
                                showFragment(warningInquireFragment);
                            }
                            break;
                        case 1:
                            //Fragment没添加过就去添加,添加过了,就直接显示
                            if (dataInquireFragment == null) {
                                //实例化DataInquireFragment
                                dataInquireFragment = new DataInquireFragment();
                                addFragment(dataInquireFragment);
                            } else {
    
                                showFragment(dataInquireFragment);
                            }
    
    
                            break;
                        case 2:
                            if (personalSettingFragment == null) {
                                //实例化PersonalSettingFragment
                                personalSettingFragment = new PersonalSettingFragment();
                                addFragment(personalSettingFragment);
                            } else {
    
                                showFragment(personalSettingFragment);
                            }
    
                            break;
                    }
                }
    
                @Override
                public void onTabUnselected(int position) {
    
                }
    
                @Override
                public void onTabReselected(int position) {
    
                }
            });
        }
    
    
        /**
         * 添加Fragment
         */
        private void addFragment(Fragment fragment) {
            getSupportFragmentManager().beginTransaction().addToBackStack(null).add(R.id.framelayout, fragment).commit();
        }
    
        /**
         * 显示Fragment
         */
        private void showFragment(Fragment fragment) {
            getSupportFragmentManager().beginTransaction().addToBackStack(null).show(fragment).commit();
        }
    
        /**
         * 隐藏Fragment
         */
        private void hideFragment() {
            //再重新获取一个开启事务
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            //不等于空或者是否添加的时候
            if (warningInquireFragment != null && warningInquireFragment.isAdded()) {
                fragmentTransaction.hide(warningInquireFragment);
            }
            if (dataInquireFragment != null && dataInquireFragment.isAdded()) {
                fragmentTransaction.hide(dataInquireFragment);
            }
            if (personalSettingFragment != null && personalSettingFragment.isAdded()) {
                fragmentTransaction.hide(personalSettingFragment);
            }
    
            fragmentTransaction.commit();
        }
    
    
    }
    

     

    展开全文
  • 昨天发现使用Google的BottomNavigationView重写了OnNavigationItemSelectedListener之后切换不了fragment在查询了半天资料无果后,发现了一种区别去viewpager的方法 在这里记录一下 使用fragment一般有两种方式 一...

    昨天发现使用Google的BottomNavigationView重写了OnNavigationItemSelectedListener之后切换不了fragment在查询了半天资料无果后,发现了一种区别去viewpager的方法

    在这里记录一下

    使用fragment一般有两种方式 一种是静态添加,在布局文件中加fragment 第二种就是动态添加fragment

    android自动生成的BottomNavigationView方法就是使用了第一种方式

    和BottomNavigationView类似,但是将布局文件中的静态设置fragment改为了动态设置fragment,废话少说,直接上代码

    布局文件:

    <?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"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <include
            layout="@layout/title_layout_blue"
           />
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:layout_below="@+id/total1"
            android:id="@+id/scrollview1"
            android:layout_weight="1">
            <com.handset.lane.widget.ShadowLayout
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
        <FrameLayout
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    
                </LinearLayout>
    
            </com.handset.lane.widget.ShadowLayout>
        </ScrollView>
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="0dp"
            android:layout_marginStart="0dp"
            android:background="?android:attr/windowBackground"
            app:menu="@menu/bottom_nav_menu"
            />
    
    </LinearLayout>

    activity:

    package com.handset.lane.activity;
    
    import android.annotation.SuppressLint;
    import android.os.Bundle;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.FrameLayout;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    import com.google.android.material.bottomnavigation.BottomNavigationView;
    import com.handset.lane.R;
    import com.handset.lane.activity.ui.cpc.HomeFragment;
    import com.handset.lane.activity.ui.obu.DashboardFragment;
    import com.handset.lane.base.BaseActivity;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentManager;
    import androidx.fragment.app.FragmentTransaction;
    import androidx.navigation.NavController;
    import androidx.navigation.Navigation;
    import androidx.navigation.ui.AppBarConfiguration;
    import androidx.navigation.ui.NavigationUI;
    import androidx.viewpager.widget.ViewPager;
    import butterknife.BindView;
    import butterknife.ButterKnife;
    
    public class CardInfoActivity extends AppCompatActivity {
    
        @BindView(R.id.nav_view)
        BottomNavigationView navView;
        @BindView(R.id.img_back)
        LinearLayout back;
        private FragmentManager mSupportFragmentManager;
        private FragmentTransaction mTransaction;
        private List<Fragment> mFragments = new ArrayList<>();
        private HomeFragment homeFragment;
        private DashboardFragment dashboardFragment;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_card_info);
            this.getSupportActionBar().hide();
            ButterKnife.bind(this);
            // Passing each menu ID as a set of Ids because each
            // menu should be considered as top level destinations.
            TextView title = findViewById(R.id.tv_title);
            title.setText(R.string.cpc);
            initView();
            navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.navigation_home:
                            title.setText(R.string.cpc);
                            hide(homeFragment, false);
                            return true;
    
                        case R.id.navigation_dashboard:
                            title.setText(R.string.obu);
                            if (dashboardFragment == null) {
                                dashboardFragment = new DashboardFragment();
                                mFragments.add(dashboardFragment);
                                hide(dashboardFragment, true);
                            } else {
                                hide(dashboardFragment, false);
                            }
    
                            return true;
                    }
                    return false;
                }
            });
    
            back.setVisibility(View.VISIBLE);
            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    
        public void initView() {
            mSupportFragmentManager = getSupportFragmentManager();
            mTransaction = mSupportFragmentManager.beginTransaction();
            homeFragment = new HomeFragment();
            mFragments.add(homeFragment);
            hide(homeFragment, true);
    
        }
    
        private void hide(Fragment fragment, boolean add) {
            mTransaction = mSupportFragmentManager.beginTransaction();
            if (add) {
                mTransaction.add(R.id.nav_host_fragment, fragment);
            }
    
            for (Fragment fragments : mFragments) {
                if (fragment.equals(fragments)) {
                    mTransaction.show(fragments);
                } else {
                    mTransaction.hide(fragments);
                }
            }
            mTransaction.commit();
    
    
        }
    
    }

    动态添加fragment常用的类:

    FragmentManager:用来管理Activity中的fragment,app包中使用getFragmentManager()   v4包中getSupportFragmentManager

    FragmentTransaction:事务,用来添加,移除,替换fragment,FragmentTransaction transaction = fm.benginTransatcion();//开启一个事务

    然后使用事务,在Activity中动态添加,隐藏fragment,在OnNavigationItemSelectedListener调用方法实现隐藏,显示

    展开全文
  • 添加一个Framelayout,在Framelayout中添加需要显示的布局,然后在请求结束之后隐藏此Framelayout
  • FrameLayout+底部栏切换

    2015-11-18 21:42:55
    FrameLayout+底部栏切换,实现点击切换界面的效果
  • 这里是采用FrameLayout+RadioGroup来实现的。 1.主界面的xml文件` &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.a...
  • private String[] TAGS = new String[]{"tag1", "tag2"}; private void showFragment(Fragment from, Fragment to, int position) { FragmentTransaction transaction = getSupportFragmentManager().b
  • 我的布局 <?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="h...
  • FrameLayout是一种比较简单的布局。他的原理就是,整个界面就是一个空白区域,当有新元素要添加时就全部放在这个空白区域的左上角,一个个叠上去,有点类似于卡片一张叠一张的作用。这样后面放的元素全部直接覆盖在...
  • 添加权限: ...uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permissi...
  • viewpager+FrameLayout activity的切换

    千次阅读 2016-12-08 14:15:56
    今天写的是viewpager+FrameLayout ,activity的切换还有页标的显示与切换,就是下面有小圆点的,每切换一个页面就小圆点就会切换到相应的页标上面。 不多说了,直接上代码了! MainActivity import java
  • 目前众多App主界面采取底部导航栏与Framelayout这种方法来实现页面分割,而不是可滑动的ViewPager(感觉主要是因为所有页面都滑动会导致用户体验不佳)。 而实现当前功能则主要有两种方法。 1.replace(); 2.add...
  • Android - FrameLayout覆盖顺序

    万次阅读 2014-11-07 08:12:43
    FrameLayout覆盖顺序本文地址: http://blog.csdn.net/caroline_wendyFrameLayout: Child views are drawn in a stack, with the most recently added child on top.FrameLayout在一个栈(stack)内, 最先出现的在最...
  • Fragment+FrameLayout实现Tab切换

    千次阅读 2018-05-09 17:49:31
    底部切换效果已经很常见了,本文介绍其中的一种实现方法: FrameLayout+Fragment 本案例已上传至Github,你可以查看 完整源码 ,也可以下载 demo apk 体验一下 实现效果如下: Tab切换 Step1:...
  • //定义数据 ...private FrameLayout fram; private RadioGroup radio; private FragmentManager manager; private FragmentTransaction transaction; private YiFragment yiFragment; private Er...
  • framelayout中fragment的切换

    千次阅读 2017-08-10 14:32:54
    我们在android开发中经常会用到fragment,例如侧拉栏的切换,viewPager的切换。 而我们切换fragment无非就两种方法: 1.replace(); 2.add(),hide(),show(); 两种发方法相比较而言,我更加...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 15,634
精华内容 6,253
关键字:

安卓framelayout切换