问题描述
- 谁能帮我看看这个actionbar tab为什么不能切换页面呐
-
.java
package com.m.myandroid;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;public class SecondActivity extends ActionBarActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Button bt1 = (Button) findViewById(R.id.bottom1); bt1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO 自动生成的方法存根 Intent intent = new Intent(SecondActivity.this, FirstActivity.class); startActivity(intent); } }); Button bt2 = (Button) findViewById(R.id.bottom2); bt2.setBackgroundResource(R.drawable.copy_img2); Button bt3 = (Button) findViewById(R.id.bottom3); bt3.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO 自动生成的方法存根 Intent intent = new Intent(SecondActivity.this, ThirdActivity.class); startActivity(intent); } }); ActionBar bar = getSupportActionBar(); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); ActionBar.Tab tab1 = bar.newTab().setText("智能排序"); ActionBar.Tab tab2 = bar.newTab().setText("离我最近"); Fragment fragment1 = new Second_Tab1(); Fragment fragment2 = new Second_Tab2(); tab1.setTabListener(new MyTabsListener(fragment1, this)); tab2.setTabListener(new MyTabsListener(fragment2, this)); bar.addTab(tab1); bar.addTab(tab2); } protected class MyTabsListener implements ActionBar.TabListener { private Fragment fragment; private FragmentActivity mActivity; FragmentActivity fmt; public MyTabsListener(Fragment fragment, FragmentActivity activity) { this.fragment = fragment; this.mActivity = activity; } @Override public void onTabReselected(Tab arg0, FragmentTransaction arg1) { // TODO 自动生成的方法存根 } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { // TODO 自动生成的方法存根 FragmentTransaction fmt = mActivity.getSupportFragmentManager() .beginTransaction(); fmt.add(R.id.fragment_place2, fragment); fmt.commit(); } @Override public void onTabUnselected(Tab arg0, FragmentTransaction arg1) { // TODO 自动生成的方法存根 FragmentTransaction fmt = mActivity.getSupportFragmentManager() .beginTransaction(); fmt.remove(fragment); fmt.commit(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.first, menu); return true; } // 点击search按钮对应操作 @Override public boolean onOptionsItemSelected(MenuItem item) { // switch (item.getItemId()){ // case R.id.first_search: // } return super.onOptionsItemSelected(item); }
}
.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"><LinearLayout android:id="@+id/fragment_place2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical">
时间: 2024-09-22 18:10:33