Android ViewPager没有在xml中显示,也没有在emulator中工作?

Android ViewPager没有在xml中显示,也没有在emulator中工作?,android,android-fragments,layout,android-viewpager,android-tablayout,Android,Android Fragments,Layout,Android Viewpager,Android Tablayout,我有一个应用程序,其中屏幕的一部分用于显示有关学生的信息,其余部分使用tablayout和viewpager。我的问题是tablayout显示在设计编辑器中,但是viewpager没有。尽管编写了代码,但没有显示任何内容。告诉我哪里出了问题 CAMarksFragment.java package com.learn.app; import android.content.Context; import android.content.SharedPreferences; import and

我有一个应用程序,其中屏幕的一部分用于显示有关学生的信息,其余部分使用tablayout和viewpager。我的问题是tablayout显示在设计编辑器中,但是viewpager没有。尽管编写了代码,但没有显示任何内容。告诉我哪里出了问题

CAMarksFragment.java

package com.learn.app;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class CAMarksFragment extends Fragment {
TabLayout tabLayout;
ViewPager viewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    return inflater.inflate(R.layout.fragment_camarks, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    //you can set the title for your toolbar here for different fragments different titles
    getActivity().setTitle("CA Marks");
    viewPager=(ViewPager)getActivity().findViewById(R.id.viewPager);
    viewPager.setAdapter(new CustomAdapter(getActivity().getSupportFragmentManager(),getActivity().getApplicationContext()));
    tabLayout=(TabLayout)getActivity().findViewById(R.id.tabLayout);
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }
    });
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    SharedPreferences pref = getActivity().getApplicationContext().getSharedPreferences("AppStatus", 0); // 0 - for private mode
    SharedPreferences.Editor editor = pref.edit();
    editor.putBoolean("Home", false);
    editor.commit();



}

private class CustomAdapter extends FragmentPagerAdapter {
    private String[] fragments={"Theory","Practical"};
    public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
        super(supportFragmentManager);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return new Theory();
            case 1:
                return new Practical();
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return fragments.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return fragments[position];
    }
}
}
package com.learn.app;

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;


public class Practical extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_layout1, container,  false);
   }
}
package com.learn.app;

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;

public class Theory extends Fragment {
     @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    return inflater.inflate(R.layout.fragment_layout, container, false);
}
}
fragments\u camarks.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.learn.app.TestTimeTableFragment">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/tablebackgrounds"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="100"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/ca_all"
            android:background="@drawable/allbackgrounds"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"


                >
                <ImageButton
                    android:id="@+id/user_profile_photo"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    android:layout_marginTop="10dp"
                    android:layout_centerHorizontal="true"
                    android:background="@drawable/profile_circular_border_imageview"
                    android:padding="20dp"
                    android:scaleType="centerCrop"
                    android:src="@drawable/defaultpic" />
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"

                    >
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textSize="20dp"
                        android:text="Hewitt"
                        android:textStyle="bold"
                        android:textColor="#ffffff"
                        android:id="@+id/camarks_name"
                        android:padding="10dp"
                        />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textSize="14dp"
                        android:id="@+id/camarks_dept"
                        android:text="Master of Computer Application"
                        android:textColor="#ffffff"
                        android:padding="10dp"
                        />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textSize="14dp"
                        android:id="@+id/camarks_rollno"
                        android:text="15mx13"
                        android:textColor="#ffffff"
                        android:padding="10dp"
                        />

                </LinearLayout>
            </LinearLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20dp"
                android:textColor="#ffffff"
                android:textStyle="bold"
                android:background="#80000000"
                android:id="@+id/ca_title"
                android:text="CA Marks"
                android:gravity="center"
                android:padding="10dp"
                />
        </LinearLayout>

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tabLayout"
            android:layout_below="@id/ca_all"
            android:background="@color/colorPrimaryDark"
            app:tabGravity="fill"
            app:tabMode="fixed"
            >


        </android.support.design.widget.TabLayout>
        <android.support.v4.view.ViewPager
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/viewPager"
            android:layout_below="@id/tabLayout"
            android:layout_centerHorizontal="true"
            >

        </android.support.v4.view.ViewPager>
        </RelativeLayout>
    </ScrollView>
    </FrameLayout>
Theory.java

package com.learn.app;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class CAMarksFragment extends Fragment {
TabLayout tabLayout;
ViewPager viewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    return inflater.inflate(R.layout.fragment_camarks, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    //you can set the title for your toolbar here for different fragments different titles
    getActivity().setTitle("CA Marks");
    viewPager=(ViewPager)getActivity().findViewById(R.id.viewPager);
    viewPager.setAdapter(new CustomAdapter(getActivity().getSupportFragmentManager(),getActivity().getApplicationContext()));
    tabLayout=(TabLayout)getActivity().findViewById(R.id.tabLayout);
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }
    });
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    SharedPreferences pref = getActivity().getApplicationContext().getSharedPreferences("AppStatus", 0); // 0 - for private mode
    SharedPreferences.Editor editor = pref.edit();
    editor.putBoolean("Home", false);
    editor.commit();



}

private class CustomAdapter extends FragmentPagerAdapter {
    private String[] fragments={"Theory","Practical"};
    public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
        super(supportFragmentManager);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return new Theory();
            case 1:
                return new Practical();
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return fragments.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return fragments[position];
    }
}
}
package com.learn.app;

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;


public class Practical extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_layout1, container,  false);
   }
}
package com.learn.app;

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;

public class Theory extends Fragment {
     @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    return inflater.inflate(R.layout.fragment_layout, container, false);
}
}
fragment_布局包含带有文本视图的linearlayout。 在emulator中,viewpager不显示,TableLayout保持不可触摸。我在这里出错了。请帮助我继续。提前谢谢

模拟器输出


代码中有很多错误

在xml中:为viewpager添加
android:layout\u height=“match\u parent”

<android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/tabLayout"
            android:layout_centerHorizontal="true">

这可能是因为您使用的是来自androidx的视图 它有ViewPager和ViewPager2

在build.gradle(:app)文件中添加以下行:


谢谢你的回复。Tablayout有效,我可以在选项卡之间进行选择。现在我需要为每个选项卡显示一个布局文件。你能帮我吗?如何在选择选项卡时更改viewpager的布局。您已经在适配器中调用viewpager片段
getItem()
方法初始化适配器,如:
new CustomAdapter(getActivity().getSupportFragmentManager(),getActivity())
我已经尝试了您的建议。但是OnTabSelectedListener似乎不起作用,所以我不会让屏幕被实际或理论片段所取代。每当选择一个选项卡时,我都需要在屏幕上加载特定的片段。现在我所拥有的只是一个空白屏幕,它不会随着选项卡的选择而改变(代码应该可以工作..您是否在viewpager片段中添加了任何视图?将日志放在InAbSelected中,以查看它是否调用。尝试添加一些背景色以查看其加载是否正确?