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
原创粉丝点击