修改Sherlock Master Detail Flow

时间:2012-11-12 15:03:27

标签: android android-layout android-fragments master-detail

如果您使用过sherlock主细节流程,请帮助我。

我已经添加了标签并删除了详细信息片段/活动中的数据,但是当我尝试给标签内的按钮充气时,它不起作用。

你能帮帮我吗? 这是我在双窗格模式下修改以显示标签的列表活动。

package com.example.sample;

import android.content.Intent;
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;

/**
 * An activity representing a list of Courses. This activity has different
 * presentations for handset and tablet-size devices. On handsets, the activity
 * presents a list of items, which when touched, lead to a
 * {@link CourseDetailActivity} representing item details. On tablets, the
 * activity presents the list of items and item details side-by-side using two
 * vertical panes.
 * <p>
 * The activity makes heavy use of fragments. The list of items is a
 * {@link CourseListFragment} and the item details (if present) is a
 * {@link CourseDetailFragment}.
 * <p>
 * This activity also implements the required
 * {@link CourseListFragment.Callbacks} interface to listen for item selections.
 */
public class CourseListActivity extends SherlockFragmentActivity implements
    CourseListFragment.Callbacks {

/**
 * Whether or not the activity is in two-pane mode, i.e. running on a tablet
 * device.
 */
private boolean mTwoPane;
private boolean once = true;

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

    if (findViewById(R.id.course_detail_container) != null) {
        // The detail container view will be present only in the
        // large-screen layouts (res/values-large and
        // res/values-sw600dp). If this view is present, then the
        // activity should be in two-pane mode.
        mTwoPane = true;

        // In two-pane mode, list items should be given the
        // 'activated' state when touched.
        ((CourseListFragment) getSupportFragmentManager().findFragmentById(
                R.id.course_list)).setActivateOnItemClick(true);
    }

    // TODO: If exposing deep links into your app, handle intents here.
}

/**
 * Callback method from {@link CourseListFragment.Callbacks} indicating that
 * the item with the given ID was selected.
 */
@Override
public void onItemSelected(String id) {
    if (mTwoPane) {
        // In two-pane mode, show the detail view in this activity by
        // adding or replacing the detail fragment using a
        // fragment transaction.
        CourseDetailFragment fragment = new CourseDetailFragment();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.course_detail_container, fragment).commit();
        if (once) {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        // initiating both tabs and set text to it.
        ActionBar.Tab assignTab = actionBar.newTab().setText("Assignments");
        ActionBar.Tab schedTab = actionBar.newTab().setText("Schedule");
        ActionBar.Tab contactTab = actionBar.newTab().setText("Contact");

        // Create three fragments to display content
        Fragment assignFragment = new Assignments();
        Fragment schedFragment = new Schedule();
        Fragment contactFragment = new Contact();

        assignTab.setTabListener(new MyTabsListener(assignFragment));
        schedTab.setTabListener(new MyTabsListener(schedFragment));
        contactTab.setTabListener(new MyTabsListener(contactFragment));

        actionBar.addTab(assignTab);
        actionBar.addTab(schedTab);
        actionBar.addTab(contactTab);
        once = false;
        }
    } else {
        // In single-pane mode, simply start the detail activity
        // for the selected item ID.
        Intent detailIntent = new Intent(this, CourseDetailActivity.class);
        startActivity(detailIntent);
    }

}
class MyTabsListener implements ActionBar.TabListener {
    public Fragment fragment;

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

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

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

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

这是默认情况下保存文本视图的片段课程详细信息布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/twopanecontainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/course_detail"
        style="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp"
        tools:context=".CourseDetailFragment" />

</LinearLayout>

我应该修改哪些内容,以便为每个标签提供不同的视图?

由于

1 个答案:

答案 0 :(得分:0)

在您的活动布局中放置一个ViewPager,然后实现FragmentPagerAdapter