Tab片段中的Android视图页面

时间:2012-12-03 09:50:36

标签: android android-fragments actionbarsherlock android-viewpager android-tabs

我正在尝试在TagFramgment中实现View Pager,如下所示

enter image description here

PlaceActivity.java

public class PlaceActivity extends SherlockFragmentActivity implements
        OnShareTargetSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_place);

        ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.Tab tab1 = bar.newTab();
        ActionBar.Tab tab2 = bar.newTab();
        tab1.setText("Details");
        tab2.setText("Maps");
        tab1.setTabListener(new MyTabListener());
        tab2.setTabListener(new MyTabListener());
        bar.addTab(tab1);
        bar.addTab(tab2);
    }

    private class MyTabListener implements ActionBar.TabListener {

        @Override
        public void onTabSelected(Tab tab,
                android.support.v4.app.FragmentTransaction ft) {
            // TODO Auto-generated method stub
            if (tab.getPosition() == 0) {
                DetailsFragment frag = new DetailsFragment();
                ft.replace(android.R.id.content, frag);

            } else {
                MapsFragment frag = new MapsFragment();
                ft.replace(android.R.id.content, frag);
            }

        }

        @Override
        public void onTabUnselected(Tab tab,
                android.support.v4.app.FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }

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

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.activity_place, menu);

        MenuItem actionItem = menu.findItem(R.id.action_share);
        ShareActionProvider actionProvider = (ShareActionProvider) actionItem
                .getActionProvider();
        actionProvider.setShareHistoryFileName(null);
        actionProvider.setOnShareTargetSelectedListener(this);
        actionProvider.setShareIntent(createShareIntent());
        return true;
    }

    public boolean onShareTargetSelected(ShareActionProvider source,
            Intent intent) {
        // TODO Auto-generated method stub
        this.startActivity(createShareIntent());

        return true;
    }

    private Intent createShareIntent() {
        String shareText = "Here is the share content body";
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        // sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject Here");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareText);

        return sharingIntent;
    }

    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case R.id.action_share:
            Toast.makeText(this, "Share Via", Toast.LENGTH_LONG).show();
            break;

        default:
            // return super.onOptionsItemSelected(item);
            break;
        }
        return true;
    }
}

DetailsFragment.java

public class DetailsFragment extends SherlockFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_details, container,
                false);

        return view;
    }

}

我想实现一个显示在DetailsFragment

中的ViewPager

0 个答案:

没有答案