Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

android - New layouts in TabHost

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...