在我的項目中,我正在使用可滑動的threefragments(選項卡與viewpager)的操作欄,我試圖用第四個片段替換其中一個片段,但我不能得到它..在viewpager中替換片段不工作
**Below is my code**
public class GetRideFragment extends Fragment implements ICustomDateTimeListener, SelectedLocationListener{
public GetRideFragment() {
}
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_getride, container, false);
return rootView;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
fromac.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/* Intent newintent = new Intent(getActivity(), AutoSearchActivity.class);
startActivity(newintent);
getActivity().overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);*/
PlaceSearchFragment fh = new PlaceSearchFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragcontainer, fh);
ft.addToBackStack("fh");
ft.commit();
}
});
下面是我的佈局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragcontainer">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/fragcontainer"
>
<TextView
android:id="@+id/ridetitile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get a Ride"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:textColor="#33b5e5"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:background="@drawable/gray_btn_disabled_normal" >
...............
...........
我adpater
public class TabsPagerAdapter extends FragmentStatePagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new GetRideFragment();
case 1:
// Games fragment activity
return new PostRideFragment();
case 2:
// Movies fragment activity
return new SearchRidesFragment();
}
return null;
}
@Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
不知道我要去哪裏錯了。任何幫助表示讚賞。
問題應該在適配器中。哪一個是你用的? –
任何錯誤?如果隨後顯示您的logcat – MGDroid
@ carlo.marinangeli IAM使用FragmentStatePagerAdapter ...更新我的問題與適配器請看看 – teekib