我在哪里遇到fragmenttransaction.replace()(ActionbarSherlock)

时间:2013-05-11 20:56:40

标签: android android-fragments actionbarsherlock android-listfragment

我正在使用两个类,我的主要SherlockFragmentActivity和一个SherlockListFragment。我试图使用操作栏导航栏从一个移动到另一个。

问题是当我点击每个标签时,没有任何反应。它只选择具有相同视图的其他选项。我知道它因为我的Log.i()

而进入ontabselected

这是我的两个班级: 主要活动:

public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener {

    private ActionBar actionbar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.actionbar = getSupportActionBar();
        this.actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        this.actionbar.addTab(this.actionbar.newTab().setText("Balance").setTabListener(this));
        this.actionbar.addTab(this.actionbar.newTab().setText("Transactions").setTabListener(this));

    }


    protected void onResume()
    {
        super.onResume();
    }

    protected void onPause()
    {
        super.onPause();
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        if(tab.getText().equals("Balance")){}
        else if(tab.getText().equals("Transactions"))
        {
            SherlockListFragment f = new TransactionList();
            ft.replace(android.R.id.content, f, "TransactionList");
        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction f) {
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

}

列出活动:

public class TransactionList extends SherlockListFragment{

      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          return inflater.inflate(R.layout.activity_transaction_list, container, false);
      }

      public void onActivityCreated(Bundle savedInstanceState){
          super.onActivityCreated(savedInstanceState);
          DataSource ds = new DataSource(getSherlockActivity());
          ds.open();
          List<Transaction> transactions = ds.findAll();
          ds.close();
          ArrayAdapter<Transaction> adapter = new ArrayAdapter<Transaction>(getSherlockActivity(), android.R.layout.simple_list_item_1, transactions.toArray(new Transaction[transactions.size()]));
          setListAdapter(adapter);
      }

}

列表布局:     

        <ListView
            android1:id="@android:id/list"
            android1:layout_width="match_parent"
            android1:layout_height="wrap_content">
        </ListView>
</LinearLayout>

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dawkinstan.simplebalance"
    android:versionCode="4"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="18" />

    <uses-permission android:name="com.android.vending.CHECK_LICENSE" />

    <application
        android:icon="@drawable/wallet_icon"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock.Light"
        android:logo="@drawable/wallet_icon">
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MTSettings"
            android:label="@string/title_activity_mtsettings" >
            <intent-filter>
                <action android:name="com.dawkinstan.MTSettings" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".TransactionList"
            android:label="@string/title_activity_transaction" >
            <intent-filter>
                <action android:name="com.dawkinstan.TransactionList" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

你不想要:

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    if(tab.getText().equals("Balance"))
    {
        SherlockListFragment b = new BalanceFragment();
        ft.replace(android.R.id.content, b, "BalanceFragment");
    }
    else if(tab.getText().equals("Transactions"))
    {
        SherlockListFragment f = new TransactionList();
        ft.replace(android.R.id.content, f, "TransactionList");
    }
}