1

所以我希望有 3 个按钮始终出现在片段中。我有一个带有 3 个按钮的活动和一个片段布局,其中包含一个内部带有 actionbarsherlock 的片段。问题是,活动中的按钮不可点击,并且在 OnClickListener 中无法执行任何操作;但是,如果我将包含 actionbarsherlock 的片段替换为内部没有 actionbarsherlock 的常规片段,则按钮运行得很好。我想知道怎么了。这是我的代码。

活动主.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="2dp"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <FrameLayout
        android:id="@+id/fragment_content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/button1" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/fragment_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        android:text="mission" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_centerHorizontal="true"
        android:text="feed" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_alignRight="@+id/fragment_content"
        android:text="Profile" />
    </RelativeLayout>

我的活动.java:

    public class MyActivity extends SherlockFragmentActivity {
    /**
     * Called when the activity is first created.
     */
    Fragment frg;
    ActionBarFragment b;
    Button btnmission;
    Button btnfeed;
    Button btnprofile;
    boolean small;
    static int previousItem;
    int tabname;
    private Fragment mVisible = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        previousItem=1;
        btnmission = (Button) findViewById(R.id.button1);
        btnfeed = (Button) findViewById(R.id.button2);
        btnprofile = (Button) findViewById(R.id.button3);
        btnmission.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("clicked", "button");
                if(previousItem!=1){

                    Intent intent=new Intent(getBaseContext(),MyActivity.class);
                    startActivity(intent);
                }
                previousItem=1;      
            }
        });

        btnfeed.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(previousItem!=2){
                    Intent intent=new Intent(getBaseContext(),MissionFragment.class);
                    intent.putExtra("class", "Feed");   
                    startActivity(intent);
                }
                previousItem=2;

            }
        });        
        btnprofile.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    private void setupFragments() {
        final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        frg = (ActionBarFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_content);
        if (frg == null) {
            frg = new ActionBarFragment();
            ft.add(R.id.fragment_content, frg);
        }
            ft.hide(frg);
            ft.commit();
    }
    private void showFragment(Fragment fragmentIn) {
        if (fragmentIn == null) return;

        final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);

        if (mVisible != null) ft.hide(mVisible);

        ft.show(fragmentIn).commit();
        mVisible = fragmentIn;
    }

它将片段添加到片段内的活动和操作栏。

ActionBarFragment.java:

    public class ActionBarFragment extends SherlockFragment {
    int tabname;
    @Override
     public void onStart() 
     {
        // TODO Auto-generated method stub
        super.onStart();
     }
       @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.empty_view, container, false);
        // Getting an instance of action bar
        ActionBar actionBar = this.getSherlockActivity().getSupportActionBar();

        // Enabling Tab Navigation mode for this action bar
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Enabling Title
        actionBar.setDisplayShowTitleEnabled(true);

        // Creating Android Tab
        Tab tab1 = actionBar.newTab()
                .setText("Nova Missoes")
                .setTabListener(new CustomTabListener<ListMission>(this.getSherlockActivity(), "mission list", ListMission.class));

        // Adding Android Tab to action bar
        actionBar.addTab(tab1);

        // Creating Apple Tab
        Tab tab2 = actionBar.newTab()
                .setText("Missoes Concluidas")
                .setTabListener(new CustomTabListener<MissionAccomplished>(this.getSherlockActivity(), "mission accomplished", MissionAccomplished.class));

        // Adding Apple Tab to action bar
        actionBar.addTab(tab2);
        Intent in=getSherlockActivity().getIntent();
        tabname=in.getIntExtra("tabname", 0);
        Log.i("intent", Integer.toString(tabname));
        if(tabname==2){
            actionBar.selectTab(tab2);
        }
        // Orientation Change Occurred
        if(savedInstanceState!=null){
            int currentTabIndex = savedInstanceState.getInt("tab_index");
            actionBar.setSelectedNavigationItem(currentTabIndex);
        }
        return view;
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
       int currentTabIndex = getSherlockActivity().getSupportActionBar().getSelectedNavigationIndex();
       outState.putInt("tab_index", currentTabIndex);
       super.onSaveInstanceState(outState);
      }
    }

显示按钮和选项卡没有问题,只是程序运行时没有调用 OnClickListener。

4

0 回答 0