我尝试为我的应用程序制作选项卡式片段,但是存在问题。从Android Studio创建选项卡式片段。制作SectionsPagerAdapter。所有好的和我的XML文件都能正常工作,但是当涉及片段源代码时,我的ArchiveALL和ArchiveINS会在同一时间输出。我将标签更改为ArchiveOUT它没有显示任何内容等等。我无法理解,如果我在第一个(ArchiveALL)选项卡上,为什么活动会调用第二个ArchiveINS()。
这是我的SectionPagerAdapter
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
/**
* Created by amrah on 2/28/18.
*/
class SectionsPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> ArchiveList = new ArrayList<>();
private final List<String> titles = new ArrayList<>();
public void addFragment(Fragment fragment, String title){
ArchiveList.add(fragment);
titles.add(title);
}
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
@Override
public Fragment getItem(int position) {
return ArchiveList.get(position);
}
@Override
public int getCount() {
return ArchiveList.size();
}
}
这是我的标签片段
import android.content.Context;
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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ArchiveInfoFragment extends Fragment {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
public ArchiveInfoFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_archive_info, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mSectionsPagerAdapter = new SectionsPagerAdapter(getActivity().getSupportFragmentManager());
mViewPager = (ViewPager) view.findViewById(R.id.ArchivePages);
setupViewAdapter(mViewPager);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.user_archives);
tabLayout.setupWithViewPager(mViewPager);
}
private void setupViewAdapter(ViewPager viewPager){
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getActivity().getSupportFragmentManager());
adapter.addFragment(new ArchiveAll(), "ALL");
adapter.addFragment(new ArchiveINS(), "INS");
adapter.addFragment(new ArchiveOUTS(), "OUTS");
viewPager.setAdapter(adapter);
}
}
这些是我的片段:
ArchiveALL片段:
import android.annotation.SuppressLint;
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 ArchiveAll extends Fragment {
public ArchiveAll() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_archive_all, container, false);
System.out.println("it is ALL fragment ");
return v;
}
@SuppressLint("StaticFieldLeak")
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
import android.annotation.SuppressLint;
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;
这是ArchiveINS片段:
public class ArchiveINS extends Fragment {
public ArchiveINS() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_arhcive_in, container, false);
System.out.println("it is INS fragment ");
return v;
}
@SuppressLint("StaticFieldLeak")
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
和ArchiveOUTS片段:
import android.annotation.SuppressLint;
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 ArchiveOUTS extends Fragment {
public ArchiveOUTS() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_archive_out, container, false);
System.out.println("It is OUTS fragment");
return v;
}
@SuppressLint("StaticFieldLeak")
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
最后,当我从NavigationDrawer菜单中打开ArchiveInfoFragment时,它会出局: I / System.out:它是所有片段 I / System.out:它是INS片段
答案 0 :(得分:0)
我找到了解决方案。实际上它不是问题,因为ViewPager本身加载了位于+和 - 位置的类。因此,他们没有使程序工作到很晚。而不是FragmentPagerAdapter使用FragmentStatePagerAdapter会有所不同。在FragmentStatePagerAdapter类中,在更改之前没有加载类,它只加载用户使用的类。