1.代码页面

package com.example.fragment_list_copy;

import android.app.FragmentManager;
import android.support.v4.app.Fragment; 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.LinearLayout; import com.example.fragment_list_copy.Fragment.OneFragment;
import com.example.fragment_list_copy.Fragment.ThreeFragment;
import com.example.fragment_list_copy.Fragment.TwoFragment; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private LinearLayout linearLayout_points;
private List<Fragment> list_fragment = new ArrayList<>();
private View point; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
} private void init() {
viewPager = findViewById(R.id.viewPager);
linearLayout_points = findViewById(R.id.point_linearLayout);
list_fragment.add(new OneFragment()); /** 实例化Frament 并放入list_fragment*/
list_fragment.add(new TwoFragment());
list_fragment.add(new ThreeFragment());
viewPager.setAdapter(new MyAdapyer(getSupportFragmentManager())); for (int i = 0;i<list_fragment.size();i++){
point = new View(this);
point.setBackgroundResource(R.drawable.ic_launcher_background); /** 指定小圆点的选择器*/
point.setEnabled(false); /** 设置小圆点为白色*/
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(10,10);/** 设置小圆点大小*/
layoutParams.leftMargin = 10; /** 设置小圆点左边距*/
linearLayout_points.addView(point,layoutParams); /** 将小圆点添加到布局*/
}
linearLayout_points.getChildAt(0).setEnabled(true); /** 得到第一个小圆点并设置为红色*/
/**
* 为viewPager 设置监听
*/
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
@Override
public void onPageSelected(int position) {
/**
* 当显示的页面变更的时候 循环遍历
* 将与选中页面对应的小圆点变为红色,其他变为白色
*/
for (int i = 0;i<list_fragment.size();i++){
if (i == position){
linearLayout_points.getChildAt(i).setEnabled(true);
}else {
linearLayout_points.getChildAt(i).setEnabled(false);
}
}
} @Override
public void onPageScrollStateChanged(int state) { }
});
}
/**
* 重写 FragmentPagerAdapter
* MyAdapter 、getCount 、getItem
*
*/ private class MyAdapyer extends FragmentPagerAdapter {
public MyAdapyer(android.support.v4.app.FragmentManager fragmentManager) {
super(fragmentManager);
} @Override
public int getCount() {
return list_fragment.size();
} @Override
public Fragment getItem(int position) {
return list_fragment.get(position);
}
}
}

2.主布局页面

<?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:orientation="vertical" tools:context="com.example.fragment_list_copy.MainActivity">
<include layout=""
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"> </android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/point_linearLayout"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginBottom="40dp"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="40dp"> </LinearLayout> </RelativeLayout>

3.创建三个小页面

1.package com.example.fragment_list_copy.Fragment;

import android.os.Bundle;
import android.os.TokenWatcher;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast; import com.example.fragment_list_copy.R; /**
* A simple {@link Fragment} subclass.
*/
public class OneFragment extends Fragment {
public OneFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one,container, false); } @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(getActivity(), "=========", Toast.LENGTH_SHORT).show();
}
}
2.
package com.example.fragment_list_copy.Fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import com.example.fragment_list_copy.R; /**
* A simple {@link Fragment} subclass.
*/
public class TwoFragment extends Fragment { public TwoFragment() {
// Required empty public constructor
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_two,container, false); } }
3.
package com.example.fragment_list_copy.Fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import com.example.fragment_list_copy.R; /**
* A simple {@link Fragment} subclass.
*/
public class ThreeFragment extends Fragment { public ThreeFragment() {
// Required empty public constructor
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_three,container, false);
} }

4.小页面的布局代码

第一个小页面中有五个button

1<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.fragment_list_copy.Fragment.OneFragment"
android:background="#00aaaa"
> <!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout> <LinearLayout
android:layout_marginBottom="10dp"
android:gravity="bottom|center"
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="环境指标"
android:id="@+id/bt_hjzb"
/>
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="历史数据"
android:id="@+id/bt_lssj"/>
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手动控制"
android:id="@+id/bt_sdkz"
/>
<Button android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="系统设置"
android:id="@+id/bt_xtsz"
/> <Button
android:id="@+id/bt_cy"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="创意设计"
/>
</LinearLayout> </FrameLayout>
2.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.fragment_list_copy.Fragment.TwoFragment"
android:background="#ba55"
> <!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"/> </FrameLayout>
3.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.fragment_list_copy.Fragment.ThreeFragment"
android:background="#ccc3"
> <!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"/> </FrameLayout>

最新文章

  1. PoEdu - C++阶段班【Po学校】- Lesson03-4_构造函数&amp;赋值函数&amp;拷贝构造函数&amp;学习方式 - 第6天
  2. nginx集群报错“upstream”directive is not allow here 错误
  3. PowerShell Script to Deploy Multiple VM on Azure in Parallel #azure #powershell
  4. iOS - OC NSDictionary 字典
  5. background-size 设置背景图片的大小
  6. 在windows2003系统上安装两个版本的oracle
  7. magento addFieldToFilter()方法常用的过滤条件
  8. asp.net的3个经典范例(ASP.NET Starter Kit ,Duwamish,NET Pet Shop)学习资料
  9. CGContext
  10. fastboot,bootloader,recovery
  11. APIcloud 移动端常用事件
  12. vue-cli项目使用mock数据的方法(借助express)
  13. 内地视频网站对各种浏览器HTML5的支持情况
  14. R语言统计学习-1简介
  15. 怎么安装Scrapy框架以及安装时出现的一系列错误(win7 64位 python3 pycharm)
  16. 我的WafBypass之道(Misc篇)
  17. MOBA项目定点数的一个想法
  18. JS 模块 p6
  19. MySQL表名大小写敏感导致的问题
  20. Android log 日志分析

热门文章

  1. Swing学习2——图标添加Icon接口使用
  2. tcl实现批量压缩文件夹
  3. UITableView section 圆角 阴影
  4. 马凯军201771010116《面向对象与程序设计Java》第十二周学习总结
  5. double 四舍五入保留一定的位数
  6. [转载] Fiddler为所欲为第三篇 封包逆向必备知识[三]
  7. Java的URL类(一)
  8. HTML入门标签学习
  9. 面试知识点准备-C++常见问题
  10. JavaScript创建对象的方式