DrawerLayout实现侧滑以及将侧滑页面与主内容页面进行关联


实现上述功能步骤:

步骤一:将DrawerLayout添加到布局文件中,并书写两个FrameLayout,后续使用主内容页面以及侧滑页面代替FrameLayout

<android.support.v4.widget.DrawerLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/fl_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </FrameLayout>

        <FrameLayout
            android:id="@+id/fl_left"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity="left">

        </FrameLayout>
    </android.support.v4.widget.DrawerLayout>

步骤二:书写主内容Fragment的布局文件以及侧滑Fragment 的布局文件
<?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="match_parent"
              android:orientation="vertical">

    <TextView android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#5f00"
              android:gravity="center"
              android:text="我是主内容界面"
              android:textColor="#000"
              android:textSize="17sp"/>
</LinearLayout>

<?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="match_parent"
              android:orientation="vertical">

    <TextView android:layout_width="200dp"
              android:layout_height="match_parent"
              android:background="#50f0"
              android:text="我是侧滑界面"
              android:textColor="#000"
              android:textSize="17sp"/>
</LinearLayout>

步骤三:将各自的布局文件添加到自己的Fragment中

public class ContentFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = View.inflate(getContext(), R.layout.fragment_content, null);
        return contentView;
    }
}


public class LeftFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = View.inflate(getContext(), R.layout.fragment_left, null);
        return contentView;
    }
}


步骤四:将两个Fragment替换FrameLayout

public class MainActivity extends AppCompatActivity {

    private static final String TAG_CONTENT ="TAG_CONTENT" ;
    private static final String TAG_LEFT ="TAG_LEFT" ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fl_content,new ContentFragment(),TAG_CONTENT);
        ft.replace(R.id.fl_left,new LeftFragment(),TAG_LEFT);
        ft.commit();
    }
}

完成收工!!!

Demo源码下载地址: http://download.csdn.net/detail/k2514091675/9802435



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值