Reputation: 212
I have a activity in which i am loading a fragment
Home fragmentA = new Home();
android.support.v4.app.FragmentTransaction fragmentTransactionA = getSupportFragmentManager().beginTransaction();
fragmentTransactionA.replace(R.id.mainFrame, fragmentA);
fragmentTransactionA.commitAllowingStateLoss();
in my home fragment i have a slidingtablayout and viewpager
<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="mobile.data.woises.fragment.Home">
<mobile.data.woises.tabs.SlidingTabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/main_color"
app:tabIndicatorColor="#0bbea5"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white"/>
</FrameLayout>
and my java code of my home fragment is this
public class Home extends Fragment {
View infHome;
SlidingTabLayout slidingTabLayout;
ViewPager viewPager;
private FragmentPageAdapterHome Pa;
CharSequence Titles[] = {"Requirements", "Notification"};
int Numboftabs = 2;
public Home() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
infHome = inflater.inflate(R.layout.fragment_home2, container, false);
return infHome;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
slidingTabLayout = (SlidingTabLayout) infHome.findViewById(R.id.sliding_tabs);
viewPager = (android.support.v4.view.ViewPager) infHome.findViewById(R.id.viewpager);
// adapter = new ViewPagerAdapter(getResources(), getChildFragmentManager());
// FragmentManager fragmentManager = getChildFragmentManager();
Pa = new FragmentPageAdapterHome(getChildFragmentManager(), Titles, Numboftabs, getActivity().getApplicationContext());
viewPager.setAdapter(Pa);
slidingTabLayout.setSelectedIndicatorColors(Color.parseColor("#0bbea5"));
slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setViewPager(viewPager);
}
}
And my FragmentPageAdapterHome is this
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
import android.view.ViewGroup;
import java.util.HashMap;
import java.util.Map;
import mobile.data.woises.fragment.Notification;
import mobile.data.woises.fragment.Requirements;
/*import com.example.uday.innohabit.fragments.FragA;
import com.example.uday.innohabit.fragments.FragB;
import com.example.uday.innohabit.fragments.FragC;*/
/**
* Created by Uday on 05-11-2015.
*/
public class FragmentPageAdapterHome extends FragmentPagerAdapter {
private Map<Integer, String> mFragmentTags;
private FragmentManager mFragmentManager;
private Context mContext;
private String WoisesId;
CharSequence Titles[];
int NumofTabs;
//int icons[] = {R.drawable.search,R.drawable.cart,R.drawable.quickorder};
//String tabTitle;
public FragmentPageAdapterHome(FragmentManager fm, CharSequence mTitles[],
int mNumbOfTabsumb, Context context) {
super(fm);
this.Titles = mTitles;
this.NumofTabs = mNumbOfTabsumb;
mFragmentManager = fm;
mFragmentTags = new HashMap<Integer, String>();
mContext = context;
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
Log.d("FragmentPageAdapterHome","Arg="+arg0);
switch (arg0) {
case 0:
Requirements requirements = new Requirements();
return requirements;
case 1:
Notification notification = new Notification();
return notification;
default:
break;
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return NumofTabs;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Object obj = super.instantiateItem(container, position);
if (obj instanceof Fragment) {
// record the fragment tag here.
Fragment f = (Fragment) obj;
String tag = f.getTag();
mFragmentTags.put(position, tag);
}
return obj;
}
public Fragment getFragment(int position) {
String tag = mFragmentTags.get(position);
if (tag == null)
return null;
return mFragmentManager.findFragmentByTag(tag);
}
}
all the Log in the Requirement and the notification fragment is being displayed in Logcat that means the function is being called but the fragment is not being displayed please help me where i am making the mistake . I was using the getsupportfragmentmanager() but someone on stack overflow posted that i should use the getChildFragmentManager() when it is nested fragment . what should i do .please help me i am unable to figure out what is problem.
Upvotes: 2
Views: 781
Reputation: 5517
You set your ViewPagers
height
to 0px
. layout_weight
is a feature from LinearLayout
, since you are using FrameLayout
, nothing is shown. Possible fixes:
<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"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="mobile.data.woises.fragment.Home">
<mobile.data.woises.tabs.SlidingTabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/main_color"
app:tabIndicatorColor="#0bbea5"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/white"/>
</LinearLayout>
//or:
<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="mobile.data.woises.fragment.Home">
<mobile.data.woises.tabs.SlidingTabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/main_color"
app:tabIndicatorColor="#0bbea5"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/white"/>
</FrameLayout>
Also, don't use px
, since your layouts will look different on different screens. Use dp
(density points) instead!
Upvotes: 1