供新手使用,代码已作最简化处理,在AS4.1.1上实测通过
<?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="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/blue">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="QQ"
android:textColor="@android:color/white"
android:textSize="20sp"/>
</RelativeLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/mainViewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="1dp"
android:background="@android:color/white"
android:baselineAligned="false"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp">
<TextView
android:id="@+id/item_weixin"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal|center_vertical"
android:text="QQ"
android:textColor="@drawable/main_tab_text_color"
android:textSize="15dp"/>
<TextView
android:id="@+id/item_tongxunlu"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal|center_vertical"
android:text="通讯录"
android:textColor="@drawable/main_tab_text_color"
android:textSize="15dp"/>
<TextView
android:id="@+id/item_faxian"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal|center_vertical"
android:text="发现"
android:textColor="@drawable/main_tab_text_color"
android:textSize="15dp"/>
<TextView
android:id="@+id/item_me"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal|center_vertical"
android:text="我"
android:textColor="@drawable/main_tab_text_color"
android:textSize="15dp"/>
</LinearLayout>
</LinearLayout>
package com.cc.ViewPagerDemo;
import android.graphics.Color;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView title, item_weixin, item_tongxunlu, item_faxian, item_me;
private ViewPager vp;
private OneFragment oneFragment;
private TwoFragment twoFragment;
private ThreeFragment threeFragment;
private FouthFragment fouthFragment;
private List<Fragment> mFragmentList = new ArrayList<Fragment>();
private FragmentAdapter mFragmentAdapter;
String[] titles = new String[]{"QQ", "通讯录", "发现", "我"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除工具栏
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
initViews();
mFragmentAdapter = new FragmentAdapter(this.getSupportFragmentManager(), mFragmentList);
vp.setOffscreenPageLimit(4);//ViewPager的缓存为4帧
vp.setAdapter(mFragmentAdapter);
vp.setCurrentItem(0);//初始设置ViewPager选中第一帧
item_weixin.setTextColor(Color.parseColor("#66CDAA"));
//ViewPager的监听事件
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
title.setText(titles[position]);
changeTextColor(position);
}
@Override
public void onPageScrollStateChanged(int state) {
/*此方法是在状态改变的时候调用,其中arg0这个参数有三种状态(0,1,2)。
arg0 ==1的时辰默示正在滑动,
arg0==2的时辰默示滑动完毕了,
arg0==0的时辰默示什么都没做。*/
}
});
}
private void initViews() {
title = (TextView) findViewById(R.id.title);
item_weixin = (TextView) findViewById(R.id.item_weixin);
item_tongxunlu = (TextView) findViewById(R.id.item_tongxunlu);
item_faxian = (TextView) findViewById(R.id.item_faxian);
item_me = (TextView) findViewById(R.id.item_me);
item_weixin.setOnClickListener(this);
item_tongxunlu.setOnClickListener(this);
item_faxian.setOnClickListener(this);
item_me.setOnClickListener(this);
vp = (ViewPager) findViewById(R.id.mainViewPager);
oneFragment = new OneFragment();
twoFragment = new TwoFragment();
threeFragment = new ThreeFragment();
fouthFragment = new FouthFragment();
//给FragmentList添加数据
mFragmentList.add(oneFragment);
mFragmentList.add(twoFragment);
mFragmentList.add(threeFragment);
mFragmentList.add(fouthFragment);
}
/**
* 点击底部Text 动态修改ViewPager的内容
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.item_weixin:
vp.setCurrentItem(0, true);
break;
case R.id.item_tongxunlu:
vp.setCurrentItem(1, true);
break;
case R.id.item_faxian:
vp.setCurrentItem(2, true);
break;
case R.id.item_me:
vp.setCurrentItem(3, true);
break;
}
}
public class FragmentAdapter extends FragmentPagerAdapter {
List<Fragment> fragmentList = new ArrayList<Fragment>();
public FragmentAdapter(FragmentManager fm, List<Fragment> fragmentList) {
super(fm);
this.fragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
}
/*
*由ViewPager的滑动修改底部导航Text的颜色
*/
private void changeTextColor(int position) {
if (position == 0) {
item_weixin.setTextColor(Color.parseColor("#66CDAA"));
item_tongxunlu.setTextColor(Color.parseColor("#000000"));
item_faxian.setTextColor(Color.parseColor("#000000"));
item_me.setTextColor(Color.parseColor("#000000"));
} else if (position == 1) {
item_tongxunlu.setTextColor(Color.parseColor("#66CDAA"));
item_weixin.setTextColor(Color.parseColor("#000000"));
item_faxian.setTextColor(Color.parseColor("#000000"));
item_me.setTextColor(Color.parseColor("#000000"));
} else if (position == 2) {
item_faxian.setTextColor(Color.parseColor("#66CDAA"));
item_weixin.setTextColor(Color.parseColor("#000000"));
item_tongxunlu.setTextColor(Color.parseColor("#000000"));
item_me.setTextColor(Color.parseColor("#000000"));
} else if (position == 3) {
item_me.setTextColor(Color.parseColor("#66CDAA"));
item_weixin.setTextColor(Color.parseColor("#000000"));
item_tongxunlu.setTextColor(Color.parseColor("#000000"));
item_faxian.setTextColor(Color.parseColor("#000000"));
}
}
}
package com.cc.ViewPagerDemo;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class OneFragment extends Fragment {
ListView lv;
private List<String> stringList;
private ArrayAdapter lvAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
lv = (ListView) view.findViewById(R.id.lv);
initData();
return view;
}
private void initData() {
stringList = new ArrayList<String>();
for (int i = 0; i < 30; i++) {
stringList.add(String.valueOf(i)+" 记录");
}
lvAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, stringList);
lv.setAdapter(lvAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getActivity(), stringList.get(i).toString(), Toast.LENGTH_SHORT).show();
}
});
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getActivity(), "long click:" + stringList.get(i).toString(), Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.river.ViewPagerDemo"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
}
源码下载地址(https://download.csdn.net/download/szwyxriver/14933204)