天天看点

Android—viewpage+Fragment

案例一(仿【饿了吗】)

效果:

Android—viewpage+Fragment

                                                                ②

Android—viewpage+Fragment

Android—viewpage+Fragment

                                                                  ④

Android—viewpage+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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.myapplication.MainActivity">
    <fragment
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/f_main_left"
        android:name="com.example.myapplication.LeftFragment"
        >

    </fragment>
    <fragment
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:id="@+id/f_main_right"
        android:name="com.example.myapplication.RightFragment"
        >

    </fragment>




</LinearLayout>
           

左碎片

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv_fragment_left_contacts"
        ></ListView>

</LinearLayout>
           

右碎片

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv_framgment_phone"
        />

</LinearLayout>
           

Java文件:

主activity

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
           

左activity

package com.example.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Created by Administrator on 2017/6/14 0014.
 */

public class LeftFragment extends Fragment{

    private ListView lv_fragment_left_contacts;
    private String list[]={"文健","逼哥","小小","逼哥的小姐姐"};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View v=inflater.inflate(R.layout.fragment_left,null);
        //获取控件
        lv_fragment_left_contacts = (ListView) v.findViewById(R.id.lv_fragment_left_contacts);
        //设置适配器
        ArrayAdapter arrayAdapter=new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,list);
        lv_fragment_left_contacts.setAdapter(arrayAdapter);
        //给左边的listView设置事件
        lv_fragment_left_contacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //第一种,右边是同样的布局,但是内容不一样
              TextView tv_framgment_phone= (TextView) getActivity().findViewById(R.id.tv_framgment_phone);
                tv_framgment_phone.setText(list[position]);
               
            }
        });

        return v;
    }
}
           

右activity

package com.example.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/14 0014.
 */

public class RightFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_right,null);
    }
}
           

案列二(仿平板【设置】)

效果:

Android—viewpage+Fragment

                                                        ②

Android—viewpage+Fragment

Android—viewpage+Fragment

                                                        ④

Android—viewpage+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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.myapplication.MainActivity">
    <fragment
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/f_main_left"
        android:name="com.example.myapplication.LeftFragment"
        >

    </fragment>
    <fragment
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:id="@+id/f_main_right"
        android:name="com.example.myapplication.RightFragment"
        >

    </fragment>




</LinearLayout>
           

左碎片fragment_left.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv_fragment_left_contacts"
        ></ListView>

</LinearLayout>
           

右碎片fragment_right.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv_framgment_phone"
        />

</LinearLayout>
           

碎片一fragment_smallsister.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Chronometer
        android:id="@+id/chronometer2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextSwitcher
        android:id="@+id/textClock"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <CalendarView
        android:id="@+id/calendarView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
           

碎片二  fragment_wj.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这是文健的布局"
        android:textSize="30sp"
        />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>
           

碎片三  fragmentbg.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text_view_bg"
        android:text="这是逼格的布局"
        />
    <RatingBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
           

碎片四  fragmentxx.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text_view_bg"
        android:text="这是小小的布局"
        />
    <RatingBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
           

Java文件:

主: MainActivity.java

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
           

左:LeftFragment.java

package com.example.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Created by Administrator on 2017/6/14 0014.
 */

public class LeftFragment extends Fragment{

    private ListView lv_fragment_left_contacts;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View v=inflater.inflate(R.layout.fragment_left,null);
        //获取控件
        lv_fragment_left_contacts = (ListView) v.findViewById(R.id.lv_fragment_left_contacts);
        //给左边的listView设置事件
        lv_fragment_left_contacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {           
                //第二种,右边布局不同,内容也不同

                // 第二步,获取随便管理器(可重复利用)
                FragmentManager fragmentManager=getFragmentManager();
                //第三步,获取事务(可重复利用)
                FragmentTransaction ft=fragmentManager.beginTransaction();
                switch (position){
                    case       0:
                        //第一步,获取碎片
                        WjFragment wjFragment=new WjFragment();
                        //第四步,替换
                        ft.replace(R.id.f_main_right,wjFragment);
                        break;
                    case 1:
                      BgFragment bgFragment=new BgFragment();
                        ft.replace(R.id.f_main_right,bgFragment);
                        break;
                    case 2:
                        XxFragment xxFragment=new XxFragment();
                        ft.replace(R.id.f_main_right,xxFragment);
                        break;
                    case 3:
                        SmallSisterFragment smallSisterFragment=new SmallSisterFragment();
                        ft.replace(R.id.f_main_right,smallSisterFragment);
                        break;
                }
                //第五步,提交(可重复利用)
                ft.commit();

            }
        });

        return v;
    }
}
           

右:RightFragment.java

package com.example.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/14 0014.
 */

public class RightFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_right,null);
    }
}
           

一:SmallSisterFragment.java

package com.example.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/14 0014.
 */

public class SmallSisterFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_smallsister,null);
    }
}
           

二:WjFragment.java

package com.example.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/14 0014.
 */

public class WjFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_wj,null);
    }
}
           

三:BgFragment.java

package com.example.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/14 0014.
 */

public class BgFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragmentbg,null);
    }
}
           

四:XxFragment.java

package com.example.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/14 0014.
 */

public class XxFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragmentxx,null);
    }
}
           

案列三(仿【微信】)

效果:(可点击改变,可滑动改变)

Android—viewpage+Fragment

                                                      ②

Android—viewpage+Fragment

Android—viewpage+Fragment

                                                       ④

Android—viewpage+Fragment

布局文件:

主布局 activity_main.xml

碎片一:fragment_contacts.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这是通讯录界面"
        android:textSize="30sp"
        android:background="#660000ff"
        />

</LinearLayout>
           

碎片二:fragment_find.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这是发现界面"
        android:textSize="30sp"
        android:background="#660000ff"
        />

</LinearLayout>
           

碎片三:fragment_mine.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这是我的界面"
        android:textSize="30sp"
        android:background="#660000ff"
        />

</LinearLayout>
           

碎片四:fragment_winxin.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这是微信界面(聊天历史)"
        android:textSize="30sp"
        android:background="#660000ff"
        />

</LinearLayout>
           

Java文件:

主文件: MainActivity.java

package com.example.myapplication;

import android.os.Handler;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager vp_main_pager;
    private List<Fragment> fragments=new ArrayList<>();
    private RadioGroup rb_main_rg;
    private RadioButton radioButton;
    private List<View> list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp_main_pager = (ViewPager) findViewById(R.id.vp_main_pager);
        rb_main_rg = (RadioGroup) findViewById(R.id.rb_main_rg);
        list=rb_main_rg.getTouchables();
        //
        WeiXinFragment weiXinFragment=new WeiXinFragment();
        MineFragment mineFragment=new MineFragment();
        FindFragment findFragment=new FindFragment();
        ContactsFragment contactsFragment=new ContactsFragment();
        fragments.add(weiXinFragment);
        fragments.add(contactsFragment);
        fragments.add(findFragment);
        fragments.add(mineFragment);
     //设置适配器
        myAdapter mm=new myAdapter(getSupportFragmentManager());
        vp_main_pager.setAdapter(mm);

        rb_main_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {




            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {//下标从一开始
                vp_main_pager.setCurrentItem(checkedId-1);

            }
        });

      vp_main_pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
          @Override
          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
          }

          @Override
          public void onPageSelected(int position) {
              radioButton = (RadioButton) list.get(position);
              radioButton.setChecked(true);
          }
          @Override
          public void onPageScrollStateChanged(int state) {
          }
      });
    }

   class myAdapter extends FragmentPagerAdapter{
       public myAdapter(FragmentManager fm) {
           super(fm);
       }

       @Override
       public Fragment getItem(int position) {

           return fragments.get(position);
       }

       @Override
       public long getItemId(int position) {
           return position;
       }

       @Override
       public int getCount() {
           return fragments.size();
       }

   }
}
           

碎片一:ContactsFragment.ava

package com.example.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/14 0014.
 */

public class ContactsFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_contacts,null);
    }
}
           

碎片二:FindFragment.java

package com.example.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/14 0014.
 */

public class FindFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_find,null);
    }
}
           

碎皮三:MineFragment.java

package com.example.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/14 0014.
 */

public class MineFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine,null);
    }
}
           

碎片四:WeiXinFragment.java

package com.example.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/14 0014.
 */

public class WeiXinFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_winxin,null);
    }
}