fragment滑动界面
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>
fragment滑动界面的更多相关文章
- viewpager fragment 滑动界面
先新建几个fragment,包括java和xml 然后在主界面的布局文件中: <android.support.v4.view.ViewPager android:id="@+id/m ...
- (转)android Fragments详解三:实现Fragment的界面
为fragment添加用户界面 fragment一般作为activity的用户界面的一部分,把它自己的layout嵌入到activity的layout中. 一个 要为fragment提供layo ...
- Android Fragment详解(三): 实现Fragment的界面
为fragment添加用户界面: Fragment一般作为activity的用户界面的一部分,把它自己的layout嵌入到activity的layout中. 一个 要为fragment提供layout ...
- android关于实现滑动界面
首先要说的是,滑动界面,我们需要一个以上的view切换,实际上可以使用ArrayList<View> pageViews要保存view信息,然后切换 LayoutInflater infl ...
- 创建多个Fragment可滑动界面
创建新项目,选择Tabbed Activity 默认就有2个Fragment,这里我们删除相关代码. 在切换时 FragmentPagerAdapter onDestroyView onCreateV ...
- FragmentCustomAnimation实现Fragment的界面切换
1.知识点:FragmentCustomAnimation 2.演示样例:来自于官网演示样例的简化,这样更方便于学习该知识点. 本演示样例的源代码下载地址为:http://download.csdn. ...
- [Android] 使用Include布局+Fragment滑动切换屏幕
前面的文章已经讲述了"随手拍"项目图像处理的技术部分,该篇文章主要是主界面的布局及屏幕滑动切换,并结合鸿洋大神的视频和郭神的第一行代码(强推两人Android博客),完毕了 ...
- Fragment滑动切换简单案例
Fragment的产生与介绍Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应平板神 ...
- SlidingMenu开源项目滑动界面的实现总结
先上图 须要准备的是先得在GitHub上下载ActionBarSherlock-master.zip,和SlidingMenu-master.zip这两个开源文件,然后解压这两个包,SlidingMe ...
随机推荐
- JS中的加减乘除和比较赋值
隐式类型转换 使用Boolean(), Number(), String()去转换数据类型的方法叫显示类型转换,而与它相对的就是隐式类型转换,隐式类型转换并没有明显的标志,而是JS解释器觉得做这样一个 ...
- makefile笔记9 - makefile隐含规则
在我们使用 Makefile 时,有一些我们会经常使用,而且使用频率非常高的东西,比如,我们编译C/C++的源程序为中间目标文件(Unix 下是[.o]文件,Windows 下是[.obj]文件). ...
- 团队-student_blog-最终程序
托管平台地址:https://github.com/gengwenhao/student_blog 小组名称:逛逛踹电脑 程序运行方法: 其他附加内容:demo版本:http://blog.gengw ...
- extract()函数,将selector对象中data的值取出来;extract_first()函数,将列表中第0个selector对象拿出来,然后取data的值。
- Mybatis-基于配置文件的配置(——纪念这个即将被抛弃的孩子)
虽然内心相信Mybatis基于配置文件的配置早已经在实战之中被注解所遗忘,但是我相信还是会有一小部分人还是需要这种技术去维护原有使用这种方式去搭建的项目. 废话不多说首先使用框架包是不能少的了.导入M ...
- Thymeleaf前后端传值 页面取值与js取值
参考: Thymeleaf前后端传值 页面取值与js取值 Thymeleaf 与 Javascript Thymeleaf教程 (十二) 标签内,js中使用表达式 目的: 后端通过Model传值到前端 ...
- Csrf_token ||| CSRF跨站请求伪造
# 注: 部分内容参考网上,侵删 CSRF(Cross-site request forgery) 跨站请求伪造,是一种对网站的恶意利用 它会通过伪装成受信任用户的请求来利用受信任的网站来获取一 ...
- Python 常见字符串常量和表达式
常见字符串常量和表达式 操作 解释 s = '' 空字符串 s = "spam's" 双引号和单引号相同 S = 's\np\ta\x00m' 转义序列 s = "&qu ...
- 用TSNE进行数据降维并展示聚类结果
TSNE提供了一种有效的数据降维方式,让我们可以在2维或3维的空间中展示聚类结果. # -*- coding: utf-8 -*- from __future__ import unicode_lit ...
- 【oracle入门】SQL的命令动词
SQL的功能 命令动词 数据定义 CREATE,DROP,ALTER 数据操纵 SELECT,INSERT,UPDATE,DELETE 数据控制 CRANT,REVOKE