Good day to all. I can't figure out how to access parent Fragment layout from child Fragment. Let's say I have main activity with tabs defined in ActionBar. Each Tab is Fragment. Now in one of those tabs I want to use tabs again (sticked to bottom this time). I'm able to create needed layout using classic TabHost. Each of these sub-tabs will be operated by same Fragment class - literally it should be plain ListView with almost same data from database, it will differ by one field only (full list, "visited" items and "not visited").
So here's my parent PlanFragment, which is placed on of tabs of main Activity. It has TabHost and populates 3 Tabs using PlanListFragment:
public class PlanFragment extends Fragment implements OnTabChangeListener {
protected static final String TAG = "PlanFragment";
public static final String TAB_FULL = "full";
public static final String TAB_VISITED = "visited";
public static final String TAB_NOT_VISITED = "not_visited";
private View mRoot;
private TabHost mTabHost;
private int mCurrentTab;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRoot = inflater.inflate(R.layout.fragment_plan, container, false);
mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
setupTabs();
return mRoot;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
mTabHost.setOnTabChangedListener(this);
mTabHost.setCurrentTab(mCurrentTab);
updateTab(TAB_FULL, R.id.tab_plan_full);
}
private void setupTabs() {
mTabHost.setup();
mTabHost.addTab(newTab(TAB_FULL, R.string.label_tab_plan_full,
R.id.tab_plan_full));
mTabHost.addTab(newTab(TAB_VISITED,
R.string.label_tab_plan_visited, R.id.tab_plan_visited));
mTabHost.addTab(newTab(TAB_NOT_VISITED,
R.string.label_tab_plan_unvisited, R.id.tab_plan_not_visited));
}
private TabSpec newTab(String tag, int labelId, int tabContentId) {
TabSpec tabSpec = mTabHost.newTabSpec(tag);
tabSpec.setIndicator(getActivity().getString(labelId));
tabSpec.setContent(tabContentId);
return tabSpec;
}
#Override
public void onTabChanged(String tabId) {
if(TAB_FULL.equals(tabId)) {
updateTab(tabId, R.id.tab_plan_full);
mCurrentTab = 0;
return;
}
if(TAB_VISITED.equals(tabId)) {
updateTab(tabId, R.id.tab_plan_visited);
mCurrentTab = 1;
return;
}
if(TAB_NOT_VISITED.equals(tabId)) {
updateTab(tabId, R.id.tab_plan_not_visited);
mCurrentTab = 2;
return;
}
}
private void updateTab(String tabId, int placeholder) {
FragmentManager fm = getFragmentManager();
if (fm.findFragmentByTag(tabId) == null) {
PlanListFragment plan = new PlanListFragment();
Bundle params = new Bundle();
params.putString(PlanListFragment.TAG, tabId);
plan.setArguments(params);
fm.beginTransaction()
.replace(placeholder, plan, tabId)
.commit();
}
}
}
Here's layout with TabHost:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp" >
<FrameLayout
android:id="#+id/tab_plan_full"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include layout="#layout/plan_list" />
</FrameLayout>
<FrameLayout
android:id="#+id/tab_plan_visited"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include layout="#layout/plan_list" />
</FrameLayout>
<FrameLayout
android:id="#+id/tab_plan_not_visited"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include layout="#layout/plan_list" />
</FrameLayout>
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-4dp"
android:layout_weight="0" />
</LinearLayout>
</TabHost>
plan_list.xml included on each tab:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
And finally PlanListFragment in which I plan to setup CursorAdapter for database based on parameter passed from parent Fragment. How do I access ListView here?
public class PlanListFragment extends ListFragment {
protected static final String TAG = "PlanListFragment";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle params = getArguments();
Log.d(TAG, params.getString(TAG));
return null;
}
}
Now during my further investigations I found following scheme to work. Key parameter moved to "tag" attribute of call in layout. If there are any drawbacks please advice.
PlanFragment.java
public class PlanFragment extends Fragment implements OnTabChangeListener {
protected static final String TAG = "PlanFragment";
public static final String TAB_FULL = "full";
public static final String TAB_VISITED = "visited";
public static final String TAB_NOT_VISITED = "not_visited";
private View mRoot;
private TabHost mTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRoot = inflater.inflate(R.layout.fragment_plan, container, false);
mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
setupTabs();
return mRoot;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
mTabHost.setOnTabChangedListener(this);
mTabHost.setCurrentTab(0);
}
private void setupTabs() {
mTabHost.setup();
mTabHost.addTab(newTab(TAB_FULL, R.string.label_tab_plan_full,
R.id.tab_plan_full));
mTabHost.addTab(newTab(TAB_VISITED,
R.string.label_tab_plan_visited, R.id.tab_plan_visited));
mTabHost.addTab(newTab(TAB_NOT_VISITED,
R.string.label_tab_plan_unvisited, R.id.tab_plan_not_visited));
}
private TabSpec newTab(String tag, int labelId, int tabContentId) {
TabSpec tabSpec = mTabHost.newTabSpec(tag);
tabSpec.setIndicator(getActivity().getString(labelId));
tabSpec.setContent(tabContentId);
return tabSpec;
}
#Override
public void onTabChanged(String tabId) {
}
}
fragment_plan.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp" >
<fragment
android:id="#+id/tab_plan_full"
android:tag="plan_full"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.example.collector.PlanListFragment" />
<fragment
android:id="#+id/tab_plan_visited"
android:tag="plan_visited"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.example.collector.PlanListFragment" />
<fragment
android:id="#+id/tab_plan_not_visited"
android:tag="plan_not_visited"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.example.collector.PlanListFragment" />
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-4dp"
android:layout_weight="0" />
</LinearLayout>
</TabHost>
plan_list.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</FrameLayout>
PlanListFragment.java
public class PlanListFragment extends ListFragment {
protected static final String TAG = "PlanListFragment";
public PlanListFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.plan_list, container, false);
Log.d(TAG,getTag());
return view;
}
}
In child fragment try to access the parent fragment like this:
ParentFragment frag = ((ParentFragment)this.getParentFragment());
frag.sendbutton.setVisibility(View.Visible).invalidate();
Here "sendbutton" is the Button in parent fragment. You should use invalidate() in child fragment to refresh the view if required.
Related
Tabhost + viewpager horizontal scroll. if the fragment is empty or contains a textview, it works, but if the fragment contains a list view,, fragment it's doesn't scroll horizontal !!
if I try to scroll from the textview it works or if the fragment is empty it's work , but from the listview no, noted that the textview its width and height are wrap_content while those in the listview are match_parent
frgament 2 (it contains the fragment of tabhost )
fragment.XML
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main_content1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESEAU"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"/>
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#000000" />
<HorizontalScrollView
android:id="#+id/hScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
</HorizontalScrollView>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
fragment2.java
public class Fragment2 extends android.support.v4.app.Fragment implements OnTabChangeListener, OnPageChangeListener {
private FragmentTabHost mTabHost;
private ViewPager viewPager;
private MyFragmentPagerAdapter myViewPagerAdapter;
View v;
public Fragment2() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment2, container, false);
viewPager = (ViewPager) v.findViewById(R.id.pager);
// init tabhos
this.initializeTabHost(savedInstanceState);
// init ViewPager
this.initializeViewPager();
return v;
}
// fake content for tabhost
class FakeContent implements TabHost.TabContentFactory {
private final Context mContext;
public FakeContent(Context context) {
mContext = context;
}
#Override
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumHeight(0);
v.setMinimumWidth(0);
return v;
}
}
private void initializeViewPager() {
List<android.support.v4.app.Fragment> fragments = new Vector<android.support.v4.app.Fragment>();
fragments.add(new TramHor());
fragments.add(new BusHor());
fragments.add(new Train());
this.myViewPagerAdapter = new MyFragmentPagerAdapter(
getChildFragmentManager(), fragments);
this.viewPager = (ViewPager) v.findViewById(R.id.pager);
this.viewPager.setAdapter(this.myViewPagerAdapter);
this.viewPager.setOnPageChangeListener(this);
}
private void initializeTabHost(Bundle args) {
mTabHost = (FragmentTabHost) v.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("", getResources().getDrawable(R.drawable.tram)),
TramHor.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("", getResources().getDrawable(R.drawable.bus)),
BusHor.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("", getResources().getDrawable(R.drawable.train)),
Train.class, null);
mTabHost.setOnTabChangedListener(this);
}
public void onTabChanged(String tabId) {
int pos = this.mTabHost.getCurrentTab();
this.viewPager.setCurrentItem(pos);
HorizontalScrollView hScrollView = (HorizontalScrollView) v.findViewById(R.id.hScrollView);
View tabView = mTabHost.getCurrentTabView();
int scrollPos = tabView.getLeft()
- (hScrollView.getWidth() - tabView.getWidth()) / 2;
hScrollView.smoothScrollTo(scrollPos, 0);
}
public void onPageScrollStateChanged(int arg0) {
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageSelected(int position) {
this.mTabHost.setCurrentTab(position);
}
}
MyFragmentPageAdapter.java
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
List<android.support.v4.app.Fragment> fragments;
public MyFragmentPagerAdapter(FragmentManager fm, List<android.support.v4.app.Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
}
a fragment of tabhost
<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"
tools:context="com.example.getgpslocation.fragment.TramHor">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LIGNE/DIRECTION"
android:layout_marginTop="70dp"
android:layout_marginLeft="20dp"/>
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#000000"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv_sliding_menu2"
android:background="#FFFFFF"
android:choiceMode="singleChoice"
android:layout_gravity="start"/>
</LinearLayout>
I found the solution, viewpager in xml was not in the right place
<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"
tools:context="com.example.getgpslocation.fragment.Fragment2"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main_content1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESEAU"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"/>
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#000000" />
<HorizontalScrollView
android:id="#+id/hScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none"/>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
So I know this has been answered multiple times, such as Fragment Inside Fragment. I am using the support library for getChildFragmentManager() as advised in the post. The issue is that my viewpager's fragments are not appearing despite being inflated. My tabs (with icons) are showing perfectly fine. Here is the relevant code:
DialogFragment:
public class DialogFragment extends DialogFragment {
private static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
private HotSpot hotSpot;
private Gson gson = new Gson();
private Map nonEmptyPerks;
private TabLayout tabLayout;
private ViewPager viewPager;
public DialogFragment() {
// Empty constructor required for DialogFragment
}
public static PerkDetailDialogFragment newInstance(int page, String hotSpotJson) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
args.putString("HOTSPOTDETAILED", hotSpotJson);
PerkDetailDialogFragment fragment = new PerkDetailDialogFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
String json = getArguments().getString("HOTSPOTDETAILED");
hotSpot = gson.fromJson(json, HotSpot.class);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dialog, parent, false);
// Get the ViewPager and set it's PagerAdapter so that it can display items
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
tabLayout = (TabLayout) view.findViewById(R.id.perkSelector);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
nonEmptyPerks = getNonEmptyPerks(hotSpot.getPerks());
PerkFragmentPagerAdapter pagerAdapter =
new PerkFragmentPagerAdapter(getChildFragmentManager(), nonEmptyPerks.size());
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
tabLayout.setupWithViewPager(viewPager);
// ..... More code here
}
#Override
public void onStart() {
super.onStart();
getDialog().getWindow().setLayout(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
}
public class PerkFragmentPagerAdapter extends FragmentPagerAdapter {
private int pageCount;
public PerkFragmentPagerAdapter(FragmentManager fm, int pageCount) {
super(fm);
this.pageCount = pageCount;
}
#Override
public int getCount() {
return pageCount;
}
#Override
public Fragment getItem(int position) {
String perk = "temp";
return PerkFragment.newInstance(position + 1, perk);
}
}
}
PerkFragment:
public class PerkFragment extends Fragment {
private static final String ARG_PAGE = "ARG_PAGE";
private static final String ARG_PERK_DESCRIPTION = "PERK_DESCRIPTION";
private int mPage;
private String perkDescription;
public static PerkFragment newInstance(int page, String perk) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
args.putString(ARG_PERK_DESCRIPTION, perk);
PerkFragment fragment = new PerkFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
perkDescription = getArguments().getString(ARG_PERK_DESCRIPTION);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_perk, container, false);
TextView perkDescriptionTextView = (TextView) view.findViewById(R.id.perkDescription);
perkDescriptionTextView.setText(perkDescription);
return view;
}
}
fragment_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/hotSpotImg"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:src="#drawable/img_venue_placeholder" />
<ImageView
android:id="#+id/iconBackArrow"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_alignStart="#+id/hotSpotImg"
android:layout_alignTop="#+id/hotSpotImg"
android:src="#drawable/img_back_arrow_shadow" />
<ImageView
android:id="#+id/iconGPS"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_alignBottom="#+id/hotSpotImg"
android:layout_alignStart="#+id/hotSpotImg"
android:src="#drawable/img_gps" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/hotSpotImg">
<TextView
android:id="#+id/memberSpace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="#string/member_space"
android:textAllCaps="true"
android:textColor="#color/grey_2"
android:textSize="16sp" />
<com.test.test.helpers.LetterSpacingTextView
android:id="#+id/hotSpotName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/memberSpace"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:text="That filler info"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#color/black"
android:textSize="22sp" />
<android.support.design.widget.TabLayout
android:id="#+id/perkSelector"
style="#style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/hotSpotName"
android:layout_marginBottom="10dp"
app:tabGravity="center" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/checkIn"
android:layout_below="#id/perkSelector"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingEnd="20dp"
android:paddingStart="20dp">
<TextView
android:id="#+id/hotSpotHours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/hours"
android:textColor="#color/grey"
android:textSize="14sp" />
<TextView
android:id="#+id/hotSpotDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/hotSpotHours"
android:layout_marginTop="20dp"
android:lineSpacingExtra="10dp"
android:text="FILLER DESCRIPTION"
android:textAlignment="center"
android:textColor="#color/grey"
android:textSize="16sp" />
</RelativeLayout>
</ScrollView>
<TextView
android:id="#+id/checkIn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/text_view_button"
android:clickable="true"
android:text="#string/check_in"
android:textAlignment="center"
android:textAllCaps="true" />
</RelativeLayout>
</RelativeLayout>
fragment_perk.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/perkDescription"
style="#style/MyCustomTextAppearance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PERK FILLER"
android:textColor="#color/magnises_grey"
android:textSize="18sp" />
The issue is that the viewpager should include tablayout inside the viewpager tags. This fixes the problem.
e.g:
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/MY.DIALOG"
card_view:tabTextColor="#666666"
card_view:tabSelectedTextColor="#666666" />
</android.support.v4.view.ViewPager>
I have a problem on displaying child fragment(fragment in fragment) with FragmentTabHost in my program. The tab host is displayed perfectly but its content does not been shown...
First, introduce the classes:
Order.java:
public class Order extends Fragment{
private View view;
private FragmentTabHost orderMenu;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view = inflater.inflate(R.layout.order, container, false);
setLayout();
return view;
}
public void setLayout(){
orderMenu = (FragmentTabHost)view.findViewById(R.id.order_tabhost);
orderMenu.setup(getActivity(), getChildFragmentManager(), R.id.order_tabcontent);
Bundle testArg1 = new Bundle();
testArg1.putString("tag", "t1");
Bundle testArg2 = new Bundle();
testArg2.putString("tag", "t2");
Bundle testArg3 = new Bundle();
testArg3.putString("tag", "t3");
orderMenu.addTab(orderMenu.newTabSpec("t1").setIndicator("fruit"), OrderMenuList.class, testArg1);
orderMenu.addTab(orderMenu.newTabSpec("t2").setIndicator("bird"), OrderMenuList.class, testArg2);
orderMenu.addTab(orderMenu.newTabSpec("t3").setIndicator("meat"), OrderMenuList.class, testArg3);
}
}
order.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.app.FragmentTabHost
android:id="#+id/order_tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<FrameLayout
android:id="#+id/order_tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
OrderMenuList.java
public class OrderMenuList extends Fragment{
private final String TAG = this.getClass().getName();
private View view;
private ListView menuList;
private String[] menu1 = {"apple", "orange", "banana", "melon"};
private String[] menu2 = {"duck", "chicken", "turkey"};
private String[] menu3 = {"steak", "pork"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view = inflater.inflate(R.layout.menu_list, container, false);
Log.d(TAG, ""+this.getArguments().getString("tag"));
setLayout();
return view;
}
public void setLayout(){
menuList = (ListView)view.findViewById(R.id.menu_list);
ArrayAdapter<String> adapter = null;
if (this.getArguments()!=null){
if (this.getArguments().getString("tag").equals("t1")){
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, menu1);
} else if (this.getArguments().getString("tag").equals("t2")){
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, menu2);
} else if (this.getArguments().getString("tag").equals("t3")){
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, menu3);
}
}
menuList.setAdapter(adapter);
Log.d(TAG, ""+adapter.getCount());
}
}
menu_list.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/menu_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
I have also tried simply put a textview instead of listview to test it but it also does not work. So, for now stage, my aim is very simple........show the tab content in the fragment!!!!!!
You have FrameLayout with android:id="#android:id/tabcontent" and android:layout_height="match_parent", so it occupies all available height. But you setup your FragmentTabHost with R.id.order_tabcontent which resides below, so you don't see it.
Remove one of FrameLayout's and setup FragmentTabHost with another.
I am following a tutorial to make a tab navigation of two child fragments inside a fragment. But in my case, only two tabindicator is shown but no child fragment is attaching.
Parent Fragment
public class WifisFragment extends SherlockFragment {
private static final String TAG = WifisFragment.class.getName();
private static final String TAG1 = TAG + 1;
private static final String TAG2 = TAG + 2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.wifis_tab_host_layout,
container, false);
FragmentTabHost tabHost = (FragmentTabHost) root
.findViewById(android.R.id.tabhost);
tabHost.setup(getSherlockActivity(), getChildFragmentManager(),
android.R.id.tabcontent);
Bundle arg = new Bundle();
arg.putInt(ChildFragment.POSITION_KEY, 1);
TabSpec tabSpec = tabHost.newTabSpec(TAG1).setIndicator("First");
tabHost.addTab(tabSpec, ChildFragment.class, arg);
arg = new Bundle();
arg.putInt(ChildFragment.POSITION_KEY, 2);
tabSpec = tabHost.newTabSpec(TAG2).setIndicator("Second");
tabHost.addTab(tabSpec, ChildFragment.class, arg);
return root;
}
}
ChildFragment:
public class ChildFragment extends SherlockFragment implements OnClickListener {
public static final String POSITION_KEY = "FragmentPositionKey";
private int position;
public static ChildFragment newInstance(Bundle args) {
ChildFragment fragment = new ChildFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
position = getArguments().getInt(POSITION_KEY);
if(CommonUtils.isDebuggable) {
Log.d("position", "" + position);
}
View root = inflater.inflate(R.layout.fragment_child, container, false);
TextView textview = (TextView) root.findViewById(R.id.textViewPosition);
textview.setText(Integer.toString(position));
textview.setOnClickListener(this);
return root;
}
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked Position: " + position, Toast.LENGTH_LONG).show();
}
}
XML Layout of parent fragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.neopixl.pixlui.components.textview.TextView
android:id="#+id/header_text"
android:background="#color/menu_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="#dimen/textview_height"
android:padding="10dp"
android:text="#string/wifi"
android:textColor="#android:color/white"
android:textSize="18sp"
pixlui:copyandpaste="false"
pixlui:typeface="aleo_bold.ttf" />
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
Can you tell me what's the problem actually?
Nothing was problem in my code. I was using a old android support-v4 library. Then I changed the library version to latest (19.0.1) and everything is working!
Halo guys, I followed the android developer steps:http://developer.android.com/training/animation/screen-slide.html
How can I call get the "btnRight" & "btnLeft" in MainJava to create a onClick function?
i have no idea about that, (???).findViewByID ?
btw, is there an another better coding design to create this effect?
i wanna create 3 page that can slide or maybe click to change screen, did i use this viewpager incorrectly? Is it probably add some new view instants of using the pagenumber trick?
please help me a bit, thanks!!
MainJava.java:
public class MainJava extends FragmentActivity {
private static final int NUM_PAGES = 3;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.hide();
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setCurrentItem(1);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
ScreenSlidePageFragment.java:
public class ScreenSlidePageFragment extends Fragment {
public static final String ARG_PAGE = "page";
private int mPageNumber;
public static ScreenSlidePageFragment create(int pageNumber) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
public ScreenSlidePageFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout containing a title and body text.
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.scroll_view_content, container, false);
//rootView.addView(inflater.inflate(R.layout.scroll_view_content, null));
View pageLeft = (View) rootView.findViewById(R.id.pageLeft);
View pageRight = (View) rootView.findViewById(R.id.pageRight);
View pageMain = (View) rootView.findViewById(R.id.pageMain);
if (mPageNumber==0){
pageLeft.setVisibility(View.VISIBLE);
pageMain.setVisibility(View.GONE);
pageRight.setVisibility(View.GONE);
}else if (mPageNumber==1){
pageLeft.setVisibility(View.GONE);
pageMain.setVisibility(View.VISIBLE);
pageRight.setVisibility(View.GONE);
}else if (mPageNumber==2){
pageLeft.setVisibility(View.GONE);
pageMain.setVisibility(View.GONE);
pageRight.setVisibility(View.VISIBLE);
}
return rootView;
}
public int getPageNumber() {
return mPageNumber;
}
scroll_view_content.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Dummy content. -->
<LinearLayout
android:id="#+id/pageLeft"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
style="?android:textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:lineSpacingMultiplier="1.2"
android:text="AAA" />
</LinearLayout>
<LinearLayout
android:id="#+id/pageRight"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
style="?android:textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:lineSpacingMultiplier="1.2"
android:text="CCC"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/pageMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#123456"
android:orientation="vertical" >
<TextView
android:id="#+id/textB"
style="?android:textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="1.2"
android:background="#654321"
android:text="BBB"/>
<LinearLayout
android:layout_below="#+id/textB"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<Button
android:id="#+id/btnLeft"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#00ffff"
android:text="A"/>
<Button
android:id="#+id/btnRight"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffff00"
android:text="B"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Thats simple really. You have to use the setCurrentItem(int) of the view pager class. The algorithm would be:
int currPage=1 //Points to second page in the viewpager
if left button clicked //listener for the button
2a. currPage--
2b. mPager.setCurrentItem(currPage) //pager is object of your viewpager class
if right button clicked //listener for the button
3a. currPage++
3b. mPpager.setCurrentItem(currPage)
Check out this link for for info.