Search code examples
androidtabsfragmentonclicklistenerfragmentpageradapter

Onclick() in a Parent Fragment does not get called


I have a FragmentPager for implementing 3 tabs. Inside one of the tabs I need to do this: If the user clicks on Button A I want to load Fragment A inside the tab and if the user clicks on Button B I want to load Fragment B in the page.

But I'm stuck at the first phase. When I click on the button the onClick method does not even get called. I am following the steps given in Slidenerd.com video #10 about Fragments.

I have implemented onClicksListener and added the onClick method like the example. But the Toast message does not fire.

Below is the code to my MainActivity and my parent Fragment (the mentioned tab) and the XML.

Thanks in advance.

MainActivity.java

public class MainActivity extends ActionBarActivity implements
    ActionBar.TabListener {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a {@link FragmentPagerAdapter}
 * derivative, which will keep every loaded fragment in memory. If this
 * becomes too memory intensive, it may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle(Farsi.Convert(getString(R.string.app_name_farsi)));
    setContentView(R.layout.activity_main);
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    // Create the adapter that will return a fragment for each of the three primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    // When swiping between different sections, select the corresponding tab. We can also use ActionBar.Tab#select() to do this if we have a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by the adapter. Also specify this Activity object, which implements the TabListener interface, as the callback (listener) for when this tab is selected.
        actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
    }
    //ff code: To make the right most tab selected at the beginning.
    mViewPager.setCurrentItem(mSectionsPagerAdapter.getCount()-1);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will automatically handle clicks on the Home/Up button, so long as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}
//******************************************************************** inner class   
/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
        if (position == 0)
            return QuickCreatePropertyListingFragment.newInstance(position+1);
        else if (position ==1)
            return SearchForRentFragment.newInstance(position+1);
        else 
            return SearchForSaleFragment.newInstance(position+1);
    }
    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }
    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return Farsi.Convert(getString(R.string.label_create));
        case 1:
            return Farsi.Convert(getString(R.string.label_rent));
        case 2:
            return Farsi.Convert(getString(R.string.label_sell));
        }
        return null;
    }
}

}

Parent Fragment

   import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract.StatusUpdates;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;


public class QuickCreatePropertyListingFragment extends Fragment implements OnClickListener{
    private Activity activity;
    public static QuickCreatePropertyListingFragment newInstance(int sectionNumber){
        QuickCreatePropertyListingFragment fragment = new QuickCreatePropertyListingFragment();
        Bundle args = new Bundle();
        args.putInt("section_number", sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.quick_create_property_listing, container,false);

        return rootView;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        Button here = (Button) getActivity().findViewById(R.id.radio32);
        here.setOnClickListener(this);
    }


    public void onClick1(View arg0) {
        // TODO Auto-generated method stub
        Toast.makeText(getActivity().getApplicationContext(), "this is my Toast message!!! =)" , Toast.LENGTH_LONG).show();
        TextView tv = (TextView) getActivity().findViewById(R.id.textViewSale);
        tv.setText("test");
        Toast.makeText(getActivity().getApplicationContext(), "this is my Toast message!!! =)" , Toast.LENGTH_LONG).show();


    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }
}

    }
}

activiti_main.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapp.myapp.MainActivity"/>

fragment_main.xml

    <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="com.myapp.myapp.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

child fragment.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" >

    <TextView
    android:id="@+id/TextView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_margin="5dp"
    android:text="category"
    android:textAppearance="?android:attr/textAppearanceMedium"/>

    <HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right" 
    android:paddingRight="10dp">
        <RadioGroup
        android:id="@+id/radioGroupInTab3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp" >
            <RadioButton
            android:id="@+id/radio31"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:background="@drawable/yourbuttonbackground"
            android:button="@android:color/transparent"
            android:checked="true"
            android:gravity="center"
            android:text="3" />
            <RadioButton
            android:id="@+id/radio32"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:background="@drawable/yourbuttonbackground"
            android:button="@android:color/transparent"
            android:checked="false"
            android:gravity="center"
            android:text="2" 
            android:onClick="onClick1"
/>
            <RadioButton
            android:id="@+id/radio33"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:background="@drawable/yourbuttonbackground"
            android:button="@android:color/transparent"
            android:checked="false"
            android:gravity="center"
            android:text=" 1"/>
        </RadioGroup>
    </HorizontalScrollView>
</LinearLayout>

Solution

  • public void onClick(View arg0) {
        // TODO Auto-generated method stub
        onClick1(arg0);
    }