1

我开发了一个包含一些活动的应用程序。现在我将实现一个带有片段和夏洛克的导航。我找到了很多例子,但我不知道如何实现我现有的活动。3天以来,我一直在寻找解决方案......所以如果有人能给我一个解决方案或提示,那就太好了。谢谢!

这些是片段文件: MainActivity

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;


public class MainActivity extends SherlockFragmentActivity {
public static Context appContext;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       //ActionBar gets initiated
        ActionBar actionbar = getSupportActionBar();
      //Tell the ActionBar we want to use Tabs.
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
      //initiating both tabs and set text to it.
        ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A");
        ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B");

     //create the two fragments we want to use for display content
        Fragment PlayerFragment = new AFragment();
        Fragment StationsFragment = new BFragment();

    //set the Tab listener. Now we can listen for clicks.
        PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
        StationsTab.setTabListener(new MyTabsListener(StationsFragment));

   //add the two tabs to the actionbar
        actionbar.addTab(PlayerTab);
        actionbar.addTab(StationsTab);


    }

    class MyTabsListener implements ActionBar.TabListener {
        public Fragment fragment;

        public MyTabsListener(Fragment fragment) {
        this.fragment = fragment;
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        //Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.replace(R.id.fragment_container, fragment);
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(fragment);
        }

        }


}

片段

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class AFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.afragment, container, false);
    }
}

B片段

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class BFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.bfragment, container, false);
    }
}

这是一个现有的活动

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Admin_Activity extends Activity implements OnClickListener {


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin); 


        //Something else
        //Database / Cursor actions
        //...

        //Listener Button
        Button button_xy = (Button) this.findViewById(R.id.button_xy);
        button_xy.setOnClickListener(this);  

        //...
    public void onClick(View v) {
        switch(v.getId()) {

        case R.id.buttonxy:
                  //intent...
        break;


        }

    }

}
4

0 回答 0