Fragment和ViewPager的简单使用
来源:互联网 发布:vnc mac 连windows 编辑:程序博客网 时间:2024/05/20 23:29
首先这是MainActivity的布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.myapplication.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tv_current" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="正在上映" android:textColor="#f00" /> <TextView android:id="@+id/tv_late" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="即将上映" /> </LinearLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v4.view.ViewPager></LinearLayout>
然后创建新的类继承Fragment
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;import android.widget.TextView;/** * Created by on 2017/1/3. */public class CuerrentFileFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { TextView textView=new TextView(getActivity()); textView.setText("我是正在上线的界面"); return textView; }}
在次创建一个新的类继承Fragment也可以直接粘贴
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;import android.widget.TextView;/** * Created by on 2017/1/3. */public class LateFilmFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { TextView textView=new TextView(getActivity()); textView.setText("我是即将上映的界面"); return textView; }}
最后我们来写一下MainActivity的代码
package com.example.myapplication;import android.graphics.Color;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;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.TextView;import org.w3c.dom.Text;import java.util.ArrayList;public class MainActivity extends FragmentActivity implements View.OnClickListener{ ArrayList<TextView> viewList = new ArrayList<TextView>(); private TextView tv_current; private TextView tv_late; private ViewPager viewpager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找控件 initView(); viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { Fragment fragment = null; switch (position) { case 0: fragment = new CuerrentFileFragment(); break; case 1: fragment = new LateFilmFragment(); break; } return fragment; } @Override public int getCount() { return 2; } }); 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 < viewList.size(); i++) { TextView textView = viewList.get(i); if (i == position) { textView.setTextColor(Color.RED); }else{ textView.setTextColor(Color.BLACK); } } } @Override public void onPageScrollStateChanged(int state) { } }); } private void initView() { viewpager = (ViewPager) findViewById(R.id.viewpager); tv_current = (TextView) findViewById(R.id.tv_current); tv_late = (TextView) findViewById(R.id.tv_late); viewList.add(tv_current); viewList.add(tv_late); for (int i=0;i<viewList.size();i++){ viewList.get(i).setOnClickListener(this); } } @Override public void onClick(View v) { switch (v.getId()){ case R.id.tv_current: viewpager.setCurrentItem(0); break; case R.id.tv_late: viewpager.setCurrentItem(1); } }}
0 0
- Fragment和ViewPager的简单使用
- Fragment和ViewPager的简单使用
- ViewPager和ViewPager+Fragment简单使用
- ViewPager和fragment的使用
- ViewPager 和Fragment的使用
- ViewPager和Fragment的使用
- fragment和viewpager的使用
- TabLayout的简单使用(TabLayout+Fragment+ViewPager)
- Fragment+ViewPager的简单使用 Android
- ViewPager与Fragment结合的简单使用
- Fragment和ViewPager的简单组合
- Fragment和ViewPager的简单实用
- viewpager和Fragment的联合使用
- ViewPager和Fragment的组合使用
- ViewPager和Fragment的组合使用
- ViewPager 和 Fragment 使用注意的问题
- 关于ViewPager和Fragment的使用
- ViewPager和Fragment的组合使用
- 编程一小时
- 数据结构-简单栈的实现
- (HDU 5926)Mr. Frog’s Game 水题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- 2.Eclipse版本android 65535解决方案(原理等同android studio现在的分包方式)
- Android TextView自动换行文字排版参差不齐的原因
- Fragment和ViewPager的简单使用
- Swift语言一览
- MB21,MB_RESERVATION_BADI无法检查预留单所有项次
- 虽然很简单,但都是一些基础的东西,人非圣人,温故而知新.
- NET将关于多线程的功能
- BZOJ2428: [HAOI2006]均分数据 模拟退火
- 蓝牙协议分析_基本概念及Android4.4 BT代码架构
- LeetCode OJ-22.Generate Parentheses(回溯法)
- android 修改电池电量图标介绍