美文网首页
Android fragment切换及传值基本用法

Android fragment切换及传值基本用法

作者: ryanxun | 来源:发表于2019-06-13 10:56 被阅读0次

    运用AndroidStudio来实现基本实现
    首先这个需要在主界面创建控件Activity_main

    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/drawer_layout"
        android:background="#F2F6FC">
    
        <LinearLayout
            android:id="@+id/daohanglan"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <LinearLayout
                android:onClick="shouye"
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content">
    
                <ImageView
                    android:src="@drawable/index"
                    android:layout_width="25dp"
                    android:layout_height="25dp" />
    
                <TextView
                    android:text="首页"
                    android:gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
    
        <LinearLayout
            android:gravity="center"
            android:orientation="vertical"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content">
            <ImageView
            android:src="@drawable/gongsi"
            android:layout_width="25dp"
            android:layout_height="25dp" />
            <TextView
                android:text="公司"
                android:gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    
            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content">
                <ImageView
                android:src="@drawable/xiaoxi"
                android:layout_width="25dp"
                android:layout_height="25dp" />
                <TextView
                    android:text="消息"
                    android:gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
    
            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content">
                <ImageView
                android:src="@drawable/shenghuoquan"
                android:layout_width="25dp"
                android:layout_height="25dp" />
                <TextView
                    android:text="生活圈"
                    android:gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
    
    
        </LinearLayout>
        
        <FrameLayout
            android:id="@+id/lunbo"
            android:layout_above="@+id/daohanglan"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            
            <fragment
                android:name="com.example.ryan.hatching.fragment.index"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
    
        </FrameLayout>
    
    
    
    </RelativeLayout>
    

    然后在准备一个fragmentMain

    
    package com.example.ryan.hatching.fragment;
    
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.view.ViewPager;
    import android.support.v4.widget.DrawerLayout;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.ListView;
    
    import com.example.ryan.hatching.MainActivity;
    import com.example.ryan.hatching.R;
    import com.example.ryan.hatching.adapter.CompanyAdapter;
    import com.example.ryan.hatching.adapter.MyPagerAdapter;
    import com.example.ryan.hatching.pojo.Company;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class index extends Fragment {
        private View view;
        private DrawerLayout drawerLayout;
        private ImageView userhead;
        private ViewPager view_pager;
        private ArrayList<View> aList;
        private MyPagerAdapter mAdapter;
        private ListView ComLv;
        private List<Company> companies;
        private CompanyAdapter companyAdapter;
    
        //虚拟数据
        private Integer id []={1,2,3,4};
        private Integer[] img = {R.drawable.logo,R.drawable.logo,R.drawable.logo,R.drawable.logo};
        private String Comname [] ={ "创孵","共享悦生活","酷狗","关乎"};
        private String[] sheng={"辽宁","北京","山东","成都"};
        private String[] shi={"鞍山","中山","廊坊","四川"};
        private String[] qu={"岫岩","关中","莲花","嘟嘟"};
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            view=inflater.inflate(R.layout.fragment_index,container,false);
    
            init();
            onclick();
            Slideshow();
            Companyshow();
    
            final DrawerLayout drawerLayout=view.findViewById(R.id.drawer_layout);
    
            ImageView userhead=view.findViewById(R.id.userhead);
            userhead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    drawerLayout.openDrawer(GravityCompat.START);
                }
            });
            return view;
    
    
    }
    
        private void init() {
            userhead=view.findViewById(R.id.userhead);
            drawerLayout=view.findViewById(R.id.drawer_layout);
            view_pager=view.findViewById(R.id.view_pager);
            ComLv=view.findViewById(R.id.ComLv);
        }
        private void Companyshow() {
            companies=new ArrayList<>();
            companyAdapter=new CompanyAdapter(getActivity(),R.layout.company_adapter,companies,ComLv);
            for (int i=0; i<id.length;i++){
                Company c=new Company();
                c.setId(i);
                c.setImageid(img[i]);
                c.setCompanyname(Comname[i]);
                c.setSheng(sheng[i]);
                c.setShi(shi[i]);
                c.setQu(qu[i]);
                companies.add(c);
            }
            ComLv.setAdapter(companyAdapter);
    
        }
    
    
    
        private void onclick() {
            userhead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    drawerLayout.openDrawer(GravityCompat.START);
                }
            });
        }
    
        private void Slideshow() {
            aList = new ArrayList<View>();
            LayoutInflater li = getLayoutInflater();
            aList.add(li.inflate(R.layout.slideshow_one,null,false));
            aList.add(li.inflate(R.layout.slideshow_two,null,false));
            aList.add(li.inflate(R.layout.slideshow_three,null,false));
            mAdapter=new MyPagerAdapter(aList);
            view_pager.setAdapter(mAdapter);
        }
    }
    

    需要一个fragment布局文件

    
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/drawer_layout"
        android:background="#F2F6FC">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <RelativeLayout
                android:id="@+id/toolbar"
                android:layout_centerHorizontal="true"
                android:background="#00CCFF"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize">
    
    
                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/userhead"
                    android:layout_marginLeft="12dp"
                    android:layout_centerVertical="true"
                    android:src="@drawable/logo"
                    android:layout_width="40dp"
                    android:layout_height="40dp" />
                <TextView
                    android:layout_centerInParent="true"
                    android:textSize="20dp"
                    android:textColor="#ffffff"
                    android:text="创孵"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
                <ImageView
                    android:layout_marginRight="12dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/serch"
                    android:layout_width="30dp"
                    android:layout_height="30dp" />
            </RelativeLayout>
    
            <RelativeLayout
                android:id="@+id/slideshow"
                android:layout_below="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="200dp">
    
                <android.support.v4.view.ViewPager
                    android:id="@+id/view_pager"
                    android:layout_centerInParent="true"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                </android.support.v4.view.ViewPager>
            </RelativeLayout>
    
            <RelativeLayout
                android:id="@+id/findwork"
                android:layout_below="@+id/slideshow"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <TextView
                    android:text="找工作"
                    android:textSize="20dp"
                    android:textColor="#000000"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </RelativeLayout>
            <RelativeLayout
                android:id="@+id/findwork_icon"
                android:layout_below="@+id/findwork"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <LinearLayout
                    android:layout_centerInParent="true"
                    android:id="@+id/quanzhi"
                    android:orientation="horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
    
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
    
                        <ImageView
                            android:src="@drawable/quanzhi"
                            android:layout_width="30dp"
                            android:layout_height="30dp" />
                        <TextView
                            android:gravity="center"
                            android:textColor="#000000"
                            android:textSize="15dp"
                            android:text="全职"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />
                    </LinearLayout>
    
                    <LinearLayout
                        android:layout_marginLeft="150dp"
                        android:orientation="vertical"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
    
                        <ImageView
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:src="@drawable/jianzhi" />
                        <TextView
                            android:gravity="center"
                            android:textColor="#000000"
                            android:textSize="15dp"
                            android:text="兼职"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />
                    </LinearLayout>
                </LinearLayout>
            </RelativeLayout>
    
            <RelativeLayout
                android:id="@+id/remen"
                android:layout_below="@+id/findwork_icon"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <TextView
                    android:text="热门公司"
                    android:textSize="20dp"
                    android:textColor="#000000"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </RelativeLayout>
    
            <RelativeLayout
                android:layout_below="@+id/remen"
                android:id="@+id/conpany"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
    
                    <ListView
                        android:id="@+id/ComLv"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
                </LinearLayout>
    
    
            </RelativeLayout>
    
    
    
        </RelativeLayout>
        <!--侧滑栏-->
        <android.support.design.widget.NavigationView
            android:id="@+id/main_right_drawer_layout"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#F2F6FC"
            android:paddingTop="50dp"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/nav_menu">
            <RelativeLayout
                android:layout_margin="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <ImageView
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:src="@drawable/shezhi"
                    android:layout_width="30dp"
                    android:layout_height="30dp" />
            </RelativeLayout>
        </android.support.design.widget.NavigationView>
    
    </android.support.v4.widget.DrawerLayout>
    

    相关文章

      网友评论

          本文标题:Android fragment切换及传值基本用法

          本文链接:https://www.haomeiwen.com/subject/fcjjfctx.html