android高级控件-Fragment
来源:互联网 发布:制作电子报刊的软件 编辑:程序博客网 时间:2024/05/10 21:27
Frament+ViewPager
viewpage可以做简单的导航,到页面菜单,可以使用视图滑动,实现像lanucher左右滑动等等,
在布局文件中加入控件viewpager,这个组件,注意这个组件是用来显示左右滑动的界面的,如果不加载xml布局文件,他是不会显示内容的
1.frament作为activity界面的一部分组成出现。
2.可以一个activity界面同时出现对个frament,并且一个frament也可以再多个activity中使用
3.在activity运行中可以动态添加,移除,替换frament
结果如下:能够自由切换效果
这是主页面
[html] view plain copy
- <?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"
- tools:context="com.example.android_pagetframent.MainActivity"
- android:orientation="vertical"
- >
- <android.support.v4.view.ViewPager
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:id="@+id/Res_ViewPager">
- </android.support.v4.view.ViewPager>
- <RadioGroup
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:id="@+id/res_RadioGroup"
- >
- <RadioButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:drawableTop="@drawable/xiao"
- android:background="@drawable/yanse"
- android:paddingLeft="60dp"
- android:text="微信"
- android:button="@null"
- android:id="@+id/res_1"
- />
- <RadioButton
- android:id="@+id/res_2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/yanse"
- android:button="@null"
- android:drawableTop="@drawable/xiao"
- android:paddingLeft="100dp"
- android:text="通讯录" />
- <RadioButton
- android:id="@+id/res_3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/yanse"
- android:button="@null"
- android:drawableTop="@drawable/xiao"
- android:paddingLeft="90dp"
- android:text="发现" />
- <RadioButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingLeft="103dp"
- android:drawableTop="@drawable/xiao"
- android:background="@drawable/yanse"
- android:button="@null"
- android:text="我的"
- android:id="@+id/res_4"
- />
- </RadioGroup>
- </LinearLayout>
每一个按钮都是一个View视图,所以我写了四个View,在调用的时候转成Frament
[html] view plain copy
- <?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="vertical"
- tools:context="com.example.android_pagetframent.MainActivity">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:text="这是发现界面"
- android:background="#66000000"
- />
- </LinearLayout>
[html] view plain copy
[html] view plain copy
- <span style="font-size:18px;">ActivityJAVA类</span>
[html] view plain copy
- package com.example.android_pagetframent;
- import android.os.Bundle;
- 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 weixinFrament extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- return inflater.inflate(R.layout.weixin,null);
- }
- }
[html] view plain copy
- <?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="vertical"
- tools:context="com.example.android_pagetframent.MainActivity">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:text="这是通讯录界面"
- android:background="#66ff00ff"
- />
- </LinearLayout>
[html] view plain copy
- <span style="font-size:18px;">ActivityJAVA类</span>
[html] view plain copy
- package com.example.android_pagetframent;
- 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 tongxunluFrament extends Fragment {
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- return inflater.inflate(R.layout.tongxunlu,null);
- }
- }
每个视图都有一个ActivityJAVA类为了能实现切换效果,我就继承了Frament碎片,把所有的View转成碎片
加入到一个集合中去根据他所点击的来切换。
[html] view plain copy
- package com.example.android_pagetframent;
- 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.app.FragmentTransaction;
- import android.support.v4.view.ViewPager;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.RadioGroup;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.List;
- public class MainActivity extends AppCompatActivity{
- private ViewPager res_viewPager;
- private List<Fragment> fragmentList=new ArrayList<>();
- private RadioGroup res_radioGroup;
- private int[] list={R.id.res_1,R.id.res_2,R.id.res_3,R.id.res_4};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- res_viewPager = (ViewPager) findViewById(R.id.Res_ViewPager);
- <span style="white-space:pre"> </span>
[html] view plain copy
- <span style="font-size: 18px; white-space: pre;"> </span><span style="font-size:24px;">//这是我自己写的四个Frament</span><span style="font-size: 18px;">
- faxianFrament faxian=new faxianFrament();
- tongxunluFrament tong=new tongxunluFrament();
- weixinFrament weixain=new weixinFrament();
- woFrament wo=new woFrament();
- fragmentList.add(faxian);
- fragmentList.add(tong);
- fragmentList.add(weixain);
- fragmentList.add(wo);
- res_viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
- res_radioGroup = (RadioGroup) findViewById(R.id.res_RadioGroup);
- res_radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {</span>
[html] view plain copy
- <span style="font-size: 18px;"><span style="white-space:pre"> </span>//拿到我所点击的id,进行判断要跳转的页面
- if(checkedId==list[0]){
- res_viewPager.setCurrentItem(0);
- }else if(checkedId==list[1]){
- res_viewPager.setCurrentItem(1);
- }else if(checkedId==list[2]){
- res_viewPager.setCurrentItem(2);
- }else if(checkedId==list[3]){
- res_viewPager.setCurrentItem(3);
- }
- }
- });</span>
[html] view plain copy
- <span style="font-size: 18px;"><span style="white-space:pre"> </span>//设置默认被选中的
- res_viewPager.setCurrentItem(0);
- res_radioGroup.check(list[0]);
- //ViewPager控件的滑动监听
- res_viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
- @Override
- public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
- }
- @Override
- public void onPageSelected(int position) {
- }
- //此方法是在状态改变的时候调用
- @Override
- public void onPageScrollStateChanged(int state) {
- //state==0 代表什么都没做
- //state==1 代表正在滑动
- //state==2 代表滑动完毕
- if (state==2){</span>
[html] view plain copy
- <span style="font-size: 18px;"><span style="white-space:pre"> </span>//当他滑动的时候调用这个方法,一切换页面成功则刷新一次按钮选中的是他相对应的</span>
[html] view plain copy
- <span style="font-size: 18px;"><span style="white-space:pre"> </span>界面,
- res_radioGroup.check(list[res_viewPager.getCurrentItem()]);</span>
[html] view plain copy
- <span style="font-size: 18px;"></span><pre name="code" class="html"><span style="font-size: 18px;"><span style="white-space:pre"> </span>//res_viewPager.getCurrentItem();拿到当前页面的下标</span></pre> } } }); } class MyAdapter extends FragmentPagerAdapter{ public MyAdapter(FragmentManager
- fm) { super(fm); } @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } }}<p></p>
- <pre></pre>
- <br>
- <br>
- <p></p>
- <p><span style="font-size:14px"><br>
- </span></p>
阅读全文
0 0
- android高级控件-Fragment
- Android常用高级控件之——Fragment
- Android 控件之 Fragment
- android 控件 fragment 生命周期
- android 控件 fragment baseFragment
- Android控件之Fragment
- Android UI高级之Fragment
- Android高级控件
- Android 高级控件
- android高级控件ListView
- Android 高级控件
- Android高级控件
- Android的高级控件
- Android 高级控件
- android高级控件1
- ListView android高级控件
- android高级控件-ListView
- Android高级控件
- SQL语句嵌套查询问题
- 内核线程创建
- 查看DLL导出函数的方法
- 监听页面 F5 ctrl+R 事件
- SQL Server DBA调优---大数据量查询记录数优化及原理探讨
- android高级控件-Fragment
- 树上的动态规划
- Fragment+viewPager
- js,css加时间戳
- Another reason for Xtrabackup error “log block numbers mismatch”
- linux之nfs的配置和使用
- MonggoDB开关服务的个人小笔记
- 535. Encode and Decode TinyURL
- Qt5 C++源码中使用中文的简单步骤