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.
Related
My problem is different. With the below xml layouts, I am showing viewpager pages fluently in activity. There is no problem in scroll view and also contents shows. But when I am using the same code in fragment in navigation drawer, then scroll bar showing but contents not showing. Blank page shows.
I used android:fillViewport="true". but no result. below is the image.I also used fixed height for scrollview. but no result.
This is Main layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTitleStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
</LinearLayout>
This is Child layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:clickable="true"
android:focusable="true"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/text1"
android:textSize="20sp"/>
</LinearLayout>
</ScrollView>
This is Main java
public class SwipeNav extends Fragment {
private MyPagerAdapter myPagerAdapter;
private ViewPager viewPager;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.nav_swipe, container, false);
myPagerAdapter = new MyPagerAdapter(getChildFragmentManager(),getContext());
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
viewPager.setAdapter(myPagerAdapter);
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
This is MyPagerAdapter Class
public class MyPagerAdapter extends FragmentStatePagerAdapter {
private Context context;
public MyPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context= context;
}
#Override
public Fragment getItem(int i) {
ArrayList<String> listItem = new ArrayList<String>();
SQLiteDatabase sqLiteDatabase = SqliteDatabase.getInstance(context).getWritableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("SELECT NAME FROM ARATRIKA", new String[]{});
if (cursor.getCount() == 0) {
} else {
while (cursor.moveToNext()) {
listItem.add(cursor.getString(0));
}
}
Object[] mStringArray = listItem.toArray();
Fragment fragment = new AFragment();
Bundle args = new Bundle();
args.putString(AFragment.ARG_OBJECT, (String)mStringArray[i]);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
////
ArrayList<String> listItem = new ArrayList<>();
SQLiteDatabase sqLiteDatabase = SqliteDatabase.getInstance(context).getWritableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("SELECT NAME FROM ARATRIKA", new String[]{});
while (cursor.moveToNext()) {
listItem.add(cursor.getString(cursor.getColumnIndex("NAME")));
}
return listItem.size();
}
#Override
public CharSequence getPageTitle(int position) {
//return tabTitleArray[position];
return "OBJECT " + (position + 1);
}
}
My problem is solved. android:hardwareAccelerated in the Manifest was 'true'. I changed to 'false'
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<application android:hardwareAccelerated="false"/>
</manifest>
Anyway Thanks to You All for helping me to solve the problem.
My application has a couple tabs of fragments with a viewpager and it does not start on top and I do not know why. Even when I scroll the fragment to the top, refreshing(going back and forth tabs) resets the fragment's start position right above the recyclerview.
fragment2.xml
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:isScrollContainer="false"
android:layout_gravity="fill_vertical"
android:clipToPadding="false"
android:fillViewport="true"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="79dp"
android:scaleType="fitXY"
android:src="#drawable/sub4_visual"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="79dp"
android:alpha="0.3"
android:background="#000000"/>
</RelativeLayout>
<TextView
android:id="#+id/frag2_desc"
android:layout_margin="11dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#color/color_333333"
android:text=""/>
<Spinner
android:id="#+id/frag2_dropbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="ifContentScrolls"
android:scrollbars="none"
android:gravity="left"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_subfrag2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Fragment2.java
public class Fragment2 extends Fragment{
...
public static Frag_BoardContent[] fragsList;
public static RecyclerView mRecyclerView;
public static Frag234_Adapter4 board_adapter;
public Fragment2() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View v = inflater.inflate(R.layout.fragment2, container, false);
tv_frag2Desc = (TextView) v.findViewById(R.id.frag2_desc);
spinner = (Spinner) v.findViewById(R.id.frag2_dropbox);
ArrayList<String> categoryList = new ArrayList<>();
if (connect) makeDynamicArray(categoryList);
adapter = new ArrayAdapter<> (getActivity(), R.layout.activity_board_spinner,categoryList);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(final AdapterView<?> parent, final View view, int position, long id) {
mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.context));
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setNestedScrollingEnabled(false);
board_adapter = new Frag234_Adapter4(MainActivity.context, fragsList);
mRecyclerView.setAdapter(board_adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_subfrag2);
mRecyclerView.setAdapter(board_adapter);
return v;
}
public ArrayList<String> makeDynamicArray(){...}
}
It is a problem about focus. you need to do requestFocus() to your scrollview and
setFocusable(false)
to fragment;
I am trying to implete listview into fragment.But my app is crashing here is my fragment code
public class ConversationFragment extends Fragment {
public View rootView=null;
private ListView lv;
private ArrayList<String> strArr;
private ArrayAdapter<String> adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String name = getArguments().getString("name");
rootView = inflater.inflate(R.layout.fragment_conversation, container, false);
TextView username=(TextView)rootView.findViewById(R.id.user_name);
username.setText(name);
strArr=new ArrayList<String>();
adapter=new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,strArr);
lv.setAdapter(adapter);
rootView.findViewById(R.id.get_from_user).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId() == R.id.get_from_user) {
getFromUser(v);
}
}
});
return rootView;
}
This is the fragment layout 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:background="#878787" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="dfgdfgdf"
android:textSize="20dp"
android:layout_centerInParent="true"
android:id="#+id/user_name"/>
<EditText
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/get_from_user"
android:text="Gönder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
/>
<ListView
android:id="#+id/message_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
And there is the logcat:http://prntscr.com/4a5svo
How can I fix it ? What do you suggest ?
lv value is not set. add code of findviewbyid on lv you should be good
initialize your Listview lv like you did for Textview and make sure your strArr Not empty
TextView username=(TextView)rootView.findViewById(R.id.user_name);
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!
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.