gpt4 book ai didi

java - ViewPagerAdapter 在 Fragment 中无法正常显示

转载 作者:行者123 更新时间:2023-11-29 18:44:29 25 4
gpt4 key购买 nike

首先我必须说对不起,我的英语不是很好

我必须在扩展 fragment 中

在顶部添加tablayout和ViewPage

在ViewPageAdapter中添加三个Fragment来切换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);
}

最佳答案

您在错误的方法中设置了 TabLayout。正确的方法应该是 onActivityCreated

onActivityCreated 在创建与 fragment 关联的 Activity 时最初调用。

问题是您在 OnActvityResult 中初始化了适配器,该适配器在使用 startActivityForResult() 触发 Intent 时调用。

将下面的代码替换为您当前的代码。

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 将控件放置在 顶部 彼此。所以将其更改为 LinearLayout

关于java - ViewPagerAdapter 在 Fragment 中无法正常显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52287853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com