我遇到了FragmentPagerAdapter問題。Android TabPagerAdapter:舊版API無法正常工作
我已經在我的應用程序的其他部分成功地使用了它,但是現在我遇到了這個討厭的問題:在較老的API上,當我從一個標籤頁滑動到另一個標籤頁時,一切工作都完美無缺,我通過點擊標題來改變標籤,它被選中,但底層的片段沒有改變。
我已經開始認爲這個問題可能是由於這個傳呼機嵌套在另一個片段中。儘管如此,奇怪的是我可以通過刷標籤來改變標籤,但是不能通過點擊標題來改變標籤。
這裏是我的代碼:
public class PetrolStationFragment extends Fragment {
private PetrolStation mPetrolStation;
private int mPetrolStationIndex;
private AppCompatImageView mLogo;
private TextView mNameTextView;
private CheckBox mCheckBox;
private TextView mAddressTextView;
private TextView mDistanceTextView;
private Switch mSwitch;
private GoogleMap mGoogleMap;
private static TabLayout mTabLayout;
private static ViewPager mViewPager;
private static int intItems = 2;
// Design pattern to instantiate a new fragment.
public static PetrolStationFragment newInstance(int petrolStationIndex) {
Bundle args = new Bundle();
args.putInt(ARG_PETROL_STATION_INDEX, petrolStationIndex);
PetrolStationFragment fragment = new PetrolStationFragment();
fragment.setArguments(args);
return fragment;
}
/********************************************************/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPetrolStationIndex = getArguments().getInt(ARG_PETROL_STATION_INDEX);
mPetrolStation = PetrolStationsArray.get().getItem(mPetrolStationIndex);
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceStace) {
View view = inflater.inflate(R.layout.fragment_petrol_station, container, false);
mLogo = (AppCompatImageView) view.findViewById(R.id.ps_logo);
mNameTextView = (TextView) view.findViewById(R.id.petrol_station);
mCheckBox = (CheckBox) view.findViewById(R.id.checkbox);
mAddressTextView = (TextView) view.findViewById(R.id.address);
mDistanceTextView = (TextView) view.findViewById(R.id.distance);
mSwitch = (Switch) view.findViewById(R.id.on_service);
mLogo.setImageDrawable(ContextCompat.getDrawable(getActivity(), getPetrolStationLogo(mPetrolStation.getLogo())));
mNameTextView.setText(mPetrolStation.getName());
mCheckBox.setChecked(mPetrolStation.isFavourite());
mAddressTextView.setText(mPetrolStation.getAddress());
// To start the marquee-effect on the address TextView.
mAddressTextView.setSelected(true);
LatLng centre = getLocation();
LatLng point = new LatLng(mPetrolStation.getLat(), mPetrolStation.getLon());
if (centre == null) {
mDistanceTextView.setText(getResources().getString(R.string.distence_nd));
} else {
double distance = computeDistance(centre, point);
mDistanceTextView.setText(getResources().getString(R.string.distance_km, distance));
}
// TODO: to fix.
mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isFavourite) {
// mPetrolStation.setFavourite(isFavourite);
}
});
((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.getUiSettings().setAllGesturesEnabled(false);
double lat = mPetrolStation.getLat();
double lon = mPetrolStation.getLon();
LatLng latLng = new LatLng(lat, lon);
mGoogleMap.addMarker(new MarkerOptions().position(latLng));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15);
mGoogleMap.moveCamera(cameraUpdate);
}
});
// Setup Views.
mTabLayout = (TabLayout) view.findViewById(R.id.pager_header);
mViewPager = (ViewPager) view.findViewById(R.id.pager);
// Set an Adapter for the View Pager.
TabPagerAdapter tabPagerAdapter = new TabPagerAdapter(getChildFragmentManager());
mViewPager.setAdapter(tabPagerAdapter);
mTabLayout.setupWithViewPager(mViewPager);
mViewPager.setCurrentItem(SELF_SERVICE_TAB_POSITION);
mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
class TabPagerAdapter extends FragmentPagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
// Return fragment with respect to position.
@Override
public Fragment getItem(int position) {
Fragment fragment;
switch (position) {
case SELF_SERVICE_TAB_POSITION: {
fragment = SelfServiceFragment.newInstance(mPetrolStationIndex);
return fragment;
}
case FULL_SERVICE_TAB_POSITION: {
fragment = FullServiceFragment.newInstance(mPetrolStationIndex);
return fragment;
}
}
return null;
}
@Override
public int getCount() {
return intItems;
}
// This method returns the title of the tab according to its position.
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case SELF_SERVICE_TAB_POSITION: {
String mSelfService = getResources().getString(R.string.self_service);
return mSelfService;
}
case FULL_SERVICE_TAB_POSITION: {
String mFullService = getResources().getString(R.string.full_service);
return mFullService;
}
}
return null;
}
}
}
如果你看看我的代碼,那是我做過的。 – Davide3i
嘗試爲您的viewpager使用適配器,我通過該適配器添加了選項卡,並且它工作正常。 – ibraizQ9