我正在尝试使用ViewPager从另一个片段“我的事件”(在导航抽屉中)实现两个片段(选项卡:相机,聊天)。 这是ViewPager类,PagerViewAdapter.java:
package com.ercess.ercess_app;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
class PagerViewAdapter extends FragmentPagerAdapter {
public PagerViewAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment;
switch (position){
case 0:
fragment = new Fragment_Camera();
break;
case 1:
fragment = new Fragment_Chat();
break;
default:
return null;
}
return fragment;
}
@Override
public int getCount() {
return 2;
}
}
这是Java类,其中包含选项卡MyEventsFragment.java:
package com.ercess.ercess_app;
import android.content.Context;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MyEventsFragment extends Fragment {
TextView camera,chat;
ViewPager viewPager;
PagerViewAdapter pagerViewAdapter;
Context context;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_myevents, container, false);
camera = view.findViewById(R.id.camera);
chat = view.findViewById(R.id.chat);
viewPager = view.findViewById(R.id.fragment_container);
pagerViewAdapter = new PagerViewAdapter(getFragmentManager());
viewPager.setAdapter(pagerViewAdapter);
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(0);
}
});
chat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(1);
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onPageSelected(int position) {
//position = 0;
onChangeTab(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
return view;
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void onChangeTab(int position) {
if (position == 0)
{
camera.setTextSize(25);
//camera.setTextColor(context.getColor(R.color.bright_color));
chat.setTextSize(20);
//chat.setTextColor(getColor(R.color.light_color));
//camera.setTextColor(context.getColor(R.color.light_color));
}
if (position == 1)
{
camera.setTextSize(20);
//camera.setTextColor(getColor(R.color.light_color));
//camera.setTextColor(context.getColor(R.color.light_color));
chat.setTextSize(25);
//chat.setTextColor(getColor(R.color.bright_color));
//camera.setTextColor(context.getColor(R.color.bright_color));
}
}
}
这是fragment_myevents.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<!--RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="My Event Fragment"
android:textSize="30sp" />
</RelativeLayout-->
<android.support.constraint.ConstraintLayout 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:background="@drawable/bg_color"
tools:context=".MyEventsFragment">
<include layout="@layout/tab_bar"/>
<android.support.v4.view.ViewPager
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="52dp"/>
单击“我的事件”片段后,它将重定向到选项卡页面。照相机选项卡片段看起来应该像聊天选项卡片段一样,看起来不正确。尽管选项卡栏消失了,也可以从相机移到聊天选项卡。可以从相机滑动到聊天页面,但不能反向。