New layouts in TabHost

坚强是说给别人听的谎言 提交于 2019-12-12 00:48:14

问题


I'm using tabHost in my application but in one of the views (corresponding to one of the tabs) I have a button that have to take me to another activity and then another layout. The question is: how do I get this new layout can continue to have access to the tabs? or better say, How do I load this new layout inside the FrameLayout ?.

Here I have uploaded what I'm trying to do: http://imageshack.us/photo/my-images/541/exampleu.png/

Thanks in advance.!

Pd: I'm new in Android, maybe is there a better way to achieve my purpouse without using TabActivity. I'm open to any suggestion.

EDITED: so I decided to use Fragments as I was suggested. And now I have the following:

  • AplicationActivity extends FragmentActivity
  • ClientActivity extends Fragment
  • SettingsActivity extends Fragment
  • DataClientActivity extends Fragment

and the following layouts:

  • activity_aplicacion
  • activity_client
  • activity_settings
  • activity_data_client

The activity_aplicacion.xml has TabHost, FrameLayout and TabWidget and from these I can go to ClientActivity and SettingsActivity using tabs.

In ClientActivity I have a button called "new" and when I press this button I want to go to DataClientActivity. So, in ClientActivity I have te following:

public void onClickNew(View view){
    DataClientActivity fragmentDataClient = new DataClientActivity ();
    final FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(android.R.id.tabcontent,fragmentDataClient , "fragmentDataClient "); 
    ft.addToBackStack(null);
    ft.commit(); 
 }

But when I run my app, I got the folling error:

05-04 21:55:04.780: E/AndroidRuntime(7515): java.lang.IllegalStateException: Could not find a method onClickNew(View) in the activity class com.n.r.AplicationActivity for onClick handler on view class android.widget.Button with id 'buttonNew'

So I'm a little confuse rigth now. Why should I have the onClickNew method in AplicationActivity and not in ClientActivity where I have the button?

EDITED 2: I found the solution for this:

public class ClientActivity extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    View view = inflater.inflate(R.layout.activity_clientes, container, false);


    **// Register for the Button.OnClick event
    Button b = (Button)view.findViewById(R.id.buttonNew);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
           // Toast.makeText(Tab1Fragment.this.getActivity(), "OnClickMe button clicked", Toast.LENGTH_LONG).show();
            Log.e("onClickNuevo2 ", "inicio");
            DataClientActivity fragmentDataClient= new DataClientActivity();
            final FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(android.R.id.tabcontent,fragmentDataClient, "fragmentDataClient"); 
            ft.addToBackStack(null);
            ft.commit(); 
        }
    });**
    return view;
}

}

I just needed to register the onClick listener to my button inside my ClientActivity. Now every works perfectly!. Thanks so much Divya Motiwala :) and thanks to this link: http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/#comment-410


回答1:


You can use Fragments instead of activites inside Tab. And on click of button, you can replace existing fragment with a new one like this :

    final FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.realtabcontent,newFrag, "New Fragment"); 
    ft.addToBackStack(null);
    ft.commit(); 

In ft.replace 1st parameter is the frameLayout to which fragment is to be attached, second is the fragment class object to be instatiated and third is the tag name.



来源:https://stackoverflow.com/questions/16375044/new-layouts-in-tabhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!