Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to change fragment inside same tab under ActionBar?
    primarykey
    data
    text
    <p>My problem is about fragment and ActionBar (android.support.v7.app.ActionBar)</p> <p>I followed this <a href="http://developer.android.com/guide/topics/ui/actionbar.html" rel="nofollow">example</a> in particular the section "Adding Navigation Tabs"</p> <p>The example works fine, but I have a problem when I would like change the content of a tab (thus fragment) with another fragment.</p> <p>In this example, I have added one button at the first fragment "Tab_Fragment_1" (under first tab). When the button is pressed, I would like change content (thus load another fragment), obviously under the same tab.</p> <p>In the MainActivity I added the method "clickbutton", that is defined in tab_fragment_1'layout</p> <p>I don't know the pattern to follow. How can I do it?</p> <p>Below is my code:</p> <p>MainActivity</p> <pre><code> public class MainActivity extends ActionBarActivity { private String TAG="MainActivity"; Tab tab =null; Tab_Fragment_2A tab_Fragment_2A=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "msg-1"); ActionBar actionBar = getSupportActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); actionBar.setDisplayShowTitleEnabled(false); tab = actionBar.newTab() .setText(R.string.tab_1) .setTabListener(new TabListener&lt;Tab_Fragment_1&gt;( this, "tab_1", Tab_Fragment_1.class)); actionBar.addTab(tab); tab = actionBar.newTab() .setText(R.string.tab_2) .setTabListener(new TabListener&lt;Tab_Fragment_2&gt;( this, "tab_2", Tab_Fragment_2.class)); actionBar.addTab(tab); tab = actionBar.newTab() .setText(R.string.tab_3) .setTabListener(new TabListener&lt;Tab_Fragment_3&gt;( this, "tab_3", Tab_Fragment_3.class)); actionBar.addTab(tab); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. Log.d(TAG, "msg-2"); getMenuInflater().inflate(R.menu.main, menu); Log.d(TAG, "msg-3"); return true; } public static class TabListener&lt;T extends Fragment&gt; implements ActionBar.TabListener { private Fragment mFragment; private final Activity mActivity; private final String mTag; private final Class&lt;T&gt; mClass; /** Constructor used each time a new tab is created. * @param activity The host Activity, used to instantiate the fragment * @param tag The identifier tag for the fragment * @param clz The fragment's Class, used to instantiate the fragment */ public TabListener(Activity activity, String tag, Class&lt;T&gt; clz) { mActivity = activity; mTag = tag; mClass = clz; } /* The following are each of the ActionBar.TabListener callbacks */ public void onTabSelected(Tab tab, FragmentTransaction ft) { // Check if the fragment is already initialized if (mFragment == null) { // If not, instantiate and add it to the activity mFragment = Fragment.instantiate(mActivity, mClass.getName()); ft.add(android.R.id.content, mFragment, mTag); } else { // If it exists, simply attach it in order to show it ft.attach(mFragment); } } public void onTabUnselected(Tab tab, FragmentTransaction ft) { if (mFragment != null) { // Detach the fragment, because another one is being attached ft.detach(mFragment); } } public void onTabReselected(Tab tab, FragmentTransaction ft) { // User selected the already selected tab. Usually do nothing. } } public void clickbutton(View v) { Log.d(TAG, "button clicked"); //to do? } } </code></pre> <p>activity_main.xml</p> <pre><code> &lt;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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" &gt; &lt;/RelativeLayout&gt; </code></pre> <p>Tab_Fragment_1 class:</p> <pre><code>public class Tab_Fragment_1 extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.tab_fragment_1,container, false); return view; } } </code></pre> <p>tab_fragment_1:</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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="@color/Azure"&gt; &lt;Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:onClick="clickbutton" /&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload