package com.beacons.railwayaid.raid;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.customlbs.library.IndoorsFactory;
import com.customlbs.surface.library.IndoorsSurfaceFactory;
import com.customlbs.surface.library.IndoorsSurfaceFragment;
public class Navigation extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.navigation_layout, container, false);
IndoorsFactory.Builder indoorsBuilder = new IndoorsFactory.Builder();
IndoorsSurfaceFactory.Builder surfaceBuilder = new IndoorsSurfaceFactory.Builder();
indoorsBuilder.setContext(getContext());
indoorsBuilder.setApiKey("69af5e85-007a-4df5-8715-d1c2c22e206f");
indoorsBuilder.setBuildingId((long) 979231679);
surfaceBuilder.setIndoorsBuilder(indoorsBuilder);
IndoorsSurfaceFragment indoorsFragment = surfaceBuilder.build();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(android.R.id.tabcontent, indoorsFragment, "indoors");
transaction.commit();
return v;
}
}
我是Android应用程序开发的新手,我无法弄清楚为什么在所有选项卡下查看片段。之前当它只是textview时,应用程序在每个选项卡下显示特定文本,应用程序工作,但添加这些新代码会导致在所有选项卡下查看片段。
编辑:
适配器代码:
package com.beacons.railwayaid.raid;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
public class PagerAdapter extends FragmentPagerAdapter {
List<Fragment> listFragments;
public PagerAdapter(FragmentManager fm, List<Fragment> listFragments){
super(fm);
this.listFragments = listFragments;
}
@Override
public Fragment getItem(int position) {
return listFragments.get(position);
}
@Override
public int getCount() {
return listFragments.size();
}
}
答案 0 :(得分:0)
因为您只设置了片段,所以您必须使用switch case或if condition并获取每个选项卡的Id并设置在特定选项卡上。
就像这样:
private void setupViewPager(ViewPager viewPager) {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
ViewPagerAdapter adapter = new ViewPagerAdapter(fragmentManager);
if (role == 1) {
adapter.addFragment(new MyDetailsB(), "My Details");
adapter.addFragment(new Biography(), "Biography");
viewPager.setAdapter(adapter);
}
}
if (st == 1) {
adapter.addFragment(new MyDetailsB(), "My Details");
adapter.addFragment(new Biography(), "Biography");
viewPager.setAdapter(adapter);
}
}
private static class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<android.support.v4.app.Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
private ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
private void addFragment(android.support.v4.app.Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
答案 1 :(得分:0)
您需要使用Adpater在片段之间切换。 同样在您的适配器中,您需要提供switch case以根据选定的选项卡选择适当的片段。
看看这个。
https://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html