首先我必须对不起,我的英语不太好
我必须在扩展片段中
在顶部添加标签布局和ViewPage
在ViewPageAdapter中添加三个片段以切换ViewPage
但是我目前不知道为什么我的适配器在模拟器上显示为空
我找到了一些文章,但我不知道哪一种适合我的当前情况。
麻烦大家给我答复和建议
我会谦虚地接受它,谢谢!
这是我的XML
<?xml version="1.0" encoding="utf-8"?>
<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=".HomeFragment">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabIndicatorColor="@color/colorAccent"
android:id="@+id/tablayout_fragment_home"
>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewpager_fragment_home"/>
</FrameLayout>
这是我的.java
package com.example.teng.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HomeFragment extends android.support.v4.app.Fragment {
View view ;
private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPagerAdapter adapter;
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_home, container, false);
return view ;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
adapter = new ViewPagerAdapter(getChildFragmentManager());
tabLayout = view.findViewById(R.id.tablayout_fragment_home);
viewPager = view.findViewById(R.id.viewpager_fragment_home);
adapter.AddFragment(new LatestNews(),"Page1");
adapter.AddFragment(new PopularProduct(),"Page2");
adapter.AddFragment(new ThemeActivity(),"Page3");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
}
答案 0 :(得分:2)
您用错误的方法设置了TabLayout
。
正确的方法应该是onActivityCreated
onActivityCreated
在创建与片段相关联的活动时最初被调用。
问题是您在OnActvityResult中初始化了适配器,当使用startActivityForResult()
触发意图时调用该适配器。
将下面的代码替换为当前代码。
package com.example.teng.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HomeFragment extends android.support.v4.app.Fragment {
View view ;
private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPagerAdapter adapter;
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_home, container, false);
return view ;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initComponent();
}
// Initialise components and variables
private void initComponent() {
adapter = new ViewPagerAdapter(getChildFragmentManager());
tabLayout = view.findViewById(R.id.tablayout_fragment_home);
viewPager = view.findViewById(R.id.viewpager_fragment_home);
adapter.AddFragment(new LatestNews(),"Page1");
adapter.AddFragment(new PopularProduct(),"Page2");
adapter.AddFragment(new ThemeActivity(),"Page3");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
}
用以下代码替换您的XML文件:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".HomeFragment">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabIndicatorColor="@color/colorAccent"
android:id="@+id/tablayout_fragment_home"/>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewpager_fragment_home"/>
</LinearLayout>
由于 FrameLayout , FrameLayout 将控件彼此置于 top 位置,因此您的点击不起作用。因此将其更改为 LinearLayout 。