FragmentTabHost and ChildFragment - ChildFragment is not showing - android

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!

Related

Android fragment tab host + viewpager swipe?

I currently have a fragment tab host, made by this tutorial
http://maxalley.wordpress.com/2013/05/18/android-creating-a-tab-layout-with-fragmenttabhost-and-fragments/
The only difference is that I'm using this inside a fragment.
So I have a fragment with the tabs, each tab has another fragment..
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
The fragment class
public class AddServiceFragment extends Fragment {
public AddServiceFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_service, container, false);
FragmentTabHost mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
String[] tabs = new String[]{"text1", "text2"};
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator(tabs[0], null),
MedicalHistoryFragment.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator(tabs[1], null),
MedicalHistoryFragment.class, null);
return view;
}
This works, I can see my tabs.
Now I want to implement a viewpager, in order to be able to swipe left or right.
I've read this from the android developer page
http://developer.android.com/training/animation/screen-slide.html
But I can't figure out, where should I put my viewpager?
I want my fragment to be independent of my activity...
TabHost doesn't provide ViewPager support.
Also, I suggest you to not use TabHost – this is old style.
Use PagerTabStrip
Look this Gist.
UPD:
I add some code, for case, if this Gist be deleted;
MainActivity.java
package ch.pboos.android.sample.viewpager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new SampleFragmentPagerAdapter());
}
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 5;
public SampleFragmentPagerAdapter() {
super(getSupportFragmentManager());
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
return PageFragment.create(position + 1);
}
}
public static class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static PageFragment create(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page, container, false);
TextView textView = (TextView) view;
textView.setText("Fragment #" + mPage);
return view;
}
}
}
activity_main.xml (without PagerTabStrip)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
activity_main_2.xml (with PagerTabStrip)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#000"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
fragment_page.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
getPageTitle
public CharSequence getPageTitle(int position) {
return "Page " + (position + 1);
}
UPD 2
With Fragment:
public class MainFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_main_2, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new SampleFragmentPagerAdapter());
}
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 5;
public SampleFragmentPagerAdapter() {
super(getSupportFragmentManager());
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
return PageFragment.create(position + 1);
}
}
public static class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static PageFragment create(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page, container, false);
TextView textView = (TextView) view;
textView.setText("Fragment #" + mPage);
return view;
}
}
}
Hope it helps
use the following solution i have put the view page like below.. and my work done..
<RelativeLayout 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=".MainActivity" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:overScrollMode="never" />
</LinearLayout>
</TabHost>
You can implement the viewpager as a library.
First thing you need to import viewpager project to the eclipse workspace.
[ I think you are modifying the project :Tabhost example]
so right click your Tabhost example project thn build path -> configure build path..
thn click Android ...when you click android you can see 2 section Project build Target & Library.So in Library you can add the viewpager Library.
Inorder to include viewpager library to the project ,you should import the library
"Existing Project” to your current one.
Step 1:-Download source code from GitHub.(https://github.com/JakeWharton/Android-ViewPagerIndicator)
Step2 :-In your Android Studio Project:
File -> Project Structure -> add (+ symbol) -> Import Existing Project. Import just the folder called ”library”, not the entire project (leave the import options as Android Studio suggests).
step 3:-If the "compileSdkVersion" specified in your build.gradle doesn’t match with the one specified in the Android-ViewPagerIndicator project, change the second one.
The same apply with any other property, such as "minSdkVersion" or even the current support library.
Step 4:-Add Android-ViewPagerIndicator project as a dependency to your build.gradle module:
dependencies {
compile project(':library')
}
Step 5:- Sync project with gradle files.

FragmentTabHost not showing tab content

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.

How to use same fragment in a viewPager?

I develope currently a small sample app with fragments and a viewPager. The viewPager shows 3 pages. In each page i instantiate a fragment of the same type. The fragment contains a textView and a button. On button click I want to replace the current fragment with another one. Now my problem is, no matter which button I press only the fragment of page 1 gets replaced. I dont know what I have to do in my pageAdapter class but I guess it has to do with using the same fragment and layout. I think I have to make sure, that my pageAdapter updates the correct page, but how do I achieve that?
For a better understanding why I want to achieve that, that I receive a json string within 3 node of type menu and I want to use each of them as a page in my viewPager.
Can someone show me a short and easy example for such a behavior? I think its a basic approach, so it cant be so difficult.
--------Edit---------
Here is the code:
public class FragmentPagerSupport extends FragmentActivity {
static final int NUM_ITEMS = 4;
MyAdapter mAdapter;
ViewPager mPager;
#Override
public void onBackPressed() {
FragmentManager fm = getFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
fm.popBackStack();
} else {
super.onBackPressed();
}
}
public MyAdapter getmAdapter() {
return mAdapter;
}
public ViewPager getmPager() {
return mPager;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
mAdapter = new MyAdapter(getFragmentManager(), this);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setOffscreenPageLimit(NUM_ITEMS + 2);
mPager.setAdapter(mAdapter);
Button button = (Button) findViewById(R.id.goto_first);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mPager.setCurrentItem(0);
}
});
button = (Button) findViewById(R.id.goto_last);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mPager.setCurrentItem(NUM_ITEMS - 1);
}
});
}
}
MyAdapter:
public MyAdapter(FragmentManager fm, FragmentPagerSupport fragmentPagerSupport) {
super(fm);
this.fragmentPagerSupport = fragmentPagerSupport;
}
#Override
public int getCount() {
return NUM_ITEMS;
}
#Override
public Fragment getItem(int position) {
Fragment newInstance = null;
switch (position) {
case 0:
newInstance = frag1.newInstance(position);
break;
case 1:
newInstance = frag1.newInstance(position);
break;
case 2:
newInstance = frag2.newInstance(position);
break;
case 3:
newInstance = frag2.newInstance(position);
break;
}
return newInstance;
}
Frag1 & Frag2 & ListItemFrag:
public static class frag1 extends ListFragment {
int mNum;
static frag1 newInstance(int num) {
frag1 f = new frag1();
Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
v.setId(mNum);
View tv = v.findViewById(R.id.text);
((TextView) tv).setText("Fragment #" + mNum);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] cheeses = { "Edamer", "Gauda", "Cheddar", "Mozarella", "Maasdamer" };
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, cheeses));
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);
String itemName = (String) l.getItemAtPosition(position);
Fragment listItemFragment = ListItemFragment.newInstance(itemName);
FragmentTransaction trans = getActivity().getFragmentManager().beginTransaction();
trans.replace(R.id.root, listItemFragment, listItemFragment.getClass().getName() + "_" + mNum);
trans.addToBackStack(itemName);
trans.commit();
}
}
public static class frag2 extends ListFragment {
int mNum;
static frag2 newInstance(int num) {
frag2 f = new frag2();
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
v.setId(mNum);
View tv = v.findViewById(R.id.text);
((TextView) tv).setText("Fragment #" + mNum);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] cheeses = { "Edamer", "Gauda", "Cheddar", "Mozarella", "Maasdamer" };
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, cheeses));
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);
String itemName = (String) l.getItemAtPosition(position);
Fragment listItemFragment = ListItemFragment.newInstance(itemName);
FragmentTransaction trans = getActivity().getFragmentManager().beginTransaction();
trans.replace(R.id.root, listItemFragment, listItemFragment.getClass().getName() + "_" + mNum);
trans.addToBackStack(itemName);
trans.commit();
}
}
public static class ListItemFragment extends Fragment {
String itemName;
static ListItemFragment newInstance(String itemName) {
ListItemFragment i = new ListItemFragment();
Bundle args = new Bundle();
args.putString("text", itemName);
i.setArguments(args);
return i;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
itemName = getArguments() != null ? getArguments().getString("text") : "NULL";
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_item, container, false);
View tv = v.findViewById(R.id.textView1);
((TextView) tv).setText("Cheese: " + itemName + " selected!");
return v;
}
}
Pager Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
<LinearLayout android:orientation="horizontal"
android:gravity="center" android:measureWithLargestChild="true"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="0">
<Button
android:id="#+id/goto_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first" />
<Button android:id="#+id/goto_last"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="last">
</Button>
</LinearLayout>
Frag1 Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#99EE11"
android:id="#+id/test">
<TextView android:id="#+id/text"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/hello_world"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="#+id/root" >
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"/>
</FrameLayout>
Frag2 Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#99EE11"
android:id="#+id/test2">
<TextView android:id="#+id/text"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/hello_world"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="#+id/root2" >
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"/>
</FrameLayout>
ListItemFrag Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="230dp"
android:background="#AA33EE"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Hi I was facing same kind of issue. I fixed the issue by using
getChildFragmentManager().beginTransaction()
instead of
getActivity().getSupportFragmentManager().beginTransaction()
As in this case we are trying to make transaction from within a fragment (one out of the list of fragments which are attached to the ViewPager, thus the Activity holding the ViewPager) so we have to use getChildFragmentManager() here for desired results.
NOTE: I am using android support v4 library and thus corresponding FragmentManager.

Accessing parent Fragment layout from child Fragment

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.

Android How can i call view pager's button?

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.

Categories

Resources