本帖最后由 liu 于 2018-12-17 16:22 编辑
ViewPager+Fragment实现页面的切换
平时开发中一般都是ViewPager和Fragment一起来使用,这次来简单模仿实现下微信的主界面的切换效果
首先先建四个Fragment(HomeFragment,ContactsFragment,DiscoverFragment,MyFragment),只在布局中放入一个TextView,不做其他的操作,这里只以其中一个布局举例:
fragment_home.xml:
[XML] 纯文本查看 复制代码 <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.company.helloworld.firstapplication.HomeFragment">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="第一个Fragment"
/>
</FrameLayout>
然后设置主布局,添加viewpager
主布局activity_main.xml:
[XML] 纯文本查看 复制代码 <?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<include layout="@layout/layout_bottom" />
</LinearLayout>
layout_bottom:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/layout_home"
style="@style/layout">
<ImageView
android:id="@+id/iv_home"
style="@style/image"
android:src="@drawable/home_on" />
<TextView
android:id="@+id/tv_home"
style="@style/textview"
android:textColor="#00c800"
android:text="微信" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_contacts"
style="@style/layout">
<ImageView
android:id="@+id/iv_contacts"
style="@style/image"
android:src="@drawable/contacts_off" />
<TextView
android:id="@+id/tv_contacts"
style="@style/textview"
android:text="联系人" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_discover"
style="@style/layout">
<ImageView
android:id="@+id/iv_discover"
style="@style/image"
android:src="@drawable/discover_off" />
<TextView
android:id="@+id/tv_discover"
style="@style/textview"
android:text="发现" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_my"
style="@style/layout">
<ImageView
android:id="@+id/iv_my"
style="@style/image"
android:src="@drawable/my_off" />
<TextView
android:id="@+id/tv_my"
style="@style/textview"
android:text="我" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
style.xml:
[XML] 纯文本查看 复制代码 <resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="layout">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:orientation">vertical</item>
<item name="android:gravity">center</item>
</style>
<style name="image">
<item name="android:layout_width">30dp</item>
<item name="android:layout_height">30dp</item>
</style>
<style name="textview">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">@android:color/black</item>
</style>
</resources>
最后将新建的Fragment添加到集合中,设置适配器
MainActivity:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends FragmentActivity implements View.OnClickListener {
private LinearLayout layout_home;
private LinearLayout layout_contacts;
private LinearLayout layout_discover;
private LinearLayout layout_my;
private ImageView iv_home;
private ImageView iv_contacts;
private ImageView iv_discover;
private ImageView iv_my;
private TextView tv_home;
private TextView tv_contacts;
private TextView tv_discover;
private TextView tv_my;
private List<Fragment> list;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
//初始化控件
viewPager = findViewById(R.id.vp);
layout_home = findViewById(R.id.layout_home);
layout_contacts = findViewById(R.id.layout_contacts);
layout_discover = findViewById(R.id.layout_discover);
layout_my = findViewById(R.id.layout_my);
iv_home = findViewById(R.id.iv_home);
iv_contacts = findViewById(R.id.iv_contacts);
iv_discover = findViewById(R.id.iv_discover);
iv_my = findViewById(R.id.iv_my);
tv_home = findViewById(R.id.tv_home);
tv_contacts = findViewById(R.id.tv_contacts);
tv_discover = findViewById(R.id.tv_discover);
tv_my = findViewById(R.id.tv_my);
layout_home.setOnClickListener(this);
layout_contacts.setOnClickListener(this);
layout_discover.setOnClickListener(this);
layout_my.setOnClickListener(this);
//创建数据源
list = new ArrayList<>();
HomeFragment homeFragment = new HomeFragment();
ContactsFragment contactsFragment = new ContactsFragment();
DiscoverFragment discoverFragment = new DiscoverFragment();
MyFragment myFragment = new MyFragment();
list.add(homeFragment);
list.add(contactsFragment);
list.add(discoverFragment);
list.add(myFragment);
//设置适配器
FragmentManager manager = getSupportFragmentManager();
MyAdapter adapter = new MyAdapter(manager, list);
viewPager.setAdapter(adapter);
}
//设置成为选择状态
private void initColor() {
iv_home.setImageResource(R.drawable.home_off);
tv_home.setTextColor(Color.BLACK);
iv_contacts.setImageResource(R.drawable.contacts_off);
tv_contacts.setTextColor(Color.BLACK);
iv_discover.setImageResource(R.drawable.discover_off);
tv_discover.setTextColor(Color.BLACK);
iv_my.setImageResource(R.drawable.my_off);
tv_my.setTextColor(Color.BLACK);
}
@Override
public void onClick(View view) {
initColor();
switch (view.getId()) {
case R.id.layout_home:
iv_home.setImageResource(R.drawable.home_on);
tv_home.setTextColor(Color.parseColor("#00c800"));
//设置viewpager显示哪一页,索引从0开始
viewPager.setCurrentItem(0);
break;
case R.id.layout_contacts:
iv_contacts.setImageResource(R.drawable.contacts_on);
tv_contacts.setTextColor(Color.parseColor("#00c800"));
viewPager.setCurrentItem(1);
break;
case R.id.layout_discover:
iv_discover.setImageResource(R.drawable.discover_on);
tv_discover.setTextColor(Color.parseColor("#00c800"));
viewPager.setCurrentItem(2);
break;
case R.id.layout_my:
iv_my.setImageResource(R.drawable.my_on);
tv_my.setTextColor(Color.parseColor("#00c800"));
viewPager.setCurrentItem(3);
break;
}
}
}
MyAdapter:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
* Created by Administrator on 2018/12/17.
*/
public class MyAdapter extends FragmentPagerAdapter {
private List<Fragment> list;
public MyAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.list = fragments;
}
//返回第几个fragment
@Override
public Fragment getItem(int position) {
return list.get(position);
}
//总共有多少个fragment
@Override
public int getCount() {
return list.size();
}
}
看下运行效果:
|