将ViewPager添加到现有TabHost中

时间:2013-09-20 16:04:12

标签: android android-viewpager android-tabhost

您好我想将查看寻呼机添加到现有代码中。添加的最佳方式是什么?这是我的RssTabActivitty.java和RssChannelActivity.java。

RssTabActivity是我初始化标签的主要活动。 RssChannelActivity正在加载rss feed。

这里是完整的源代码 https://github.com/itcuties/Android-Multicategory-RSS-Reader

public class RssTabsActivity extends TabActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // First, set the content view
    setContentView(R.layout.activity_rss_tabs);

    // Then get the TabHost
    TabHost tabHost = getTabHost();

    /* *****************
     * Art tab
     */
    Intent artIntent = new Intent().setClass(this, RssChannelActivity.class);
    // Set Art category RSS URL
    artIntent.putExtra("rss-url", "http://feeds.reuters.com/news/artsculture?format=xml");

    // The name of the art tab taken from the String resources
    String artTabName = getResources().getString(R.string.tab_art);
    TabSpec artTabSpec = tabHost.newTabSpec(artTabName)
                                .setIndicator(artTabName, getResources().getDrawable(R.drawable.rss_tab_art))
                                .setContent(artIntent);
    // Add art tab to the TabHost
    tabHost.addTab(artTabSpec);

    /* *****************
     * Tech tab
     */
    Intent techIntent = new Intent().setClass(this, RssChannelActivity.class);
    // Set Tech category RSS URL
    techIntent.putExtra("rss-url", "http://feeds.reuters.com/reuters/technologyNews?format=xml");

    // Tech tab name taken from the string resources
    String techTabName = getResources().getString(R.string.tab_tech);
    TabSpec techTabSpec = tabHost.newTabSpec(techTabName)
                                 .setIndicator(techTabName, getResources().getDrawable(R.drawable.rss_tab_tech))
                                 .setContent(techIntent);
    // Add tech tab to the TabHost
    tabHost.addTab(techTabSpec);


    /* *****************
     * Sports tab
     */
    Intent sportsIntent = new Intent().setClass(this, RssChannelActivity.class);
    // Set Sports category RSS URL
    sportsIntent.putExtra("rss-url", "http://feeds.reuters.com/reuters/sportsNews?format=xml");

    // Sports tab name - string resources
    String sportsTabName = getResources().getString(R.string.tab_sports);
    TabSpec sportsTabSpec = tabHost.newTabSpec(sportsTabName)
                                   .setIndicator(sportsTabName, getResources().getDrawable(R.drawable.rss_tab_sports))
                                   .setContent(sportsIntent);
    // Add sports tab to the TabHost
    tabHost.addTab(sportsTabSpec);


    // Set current tab to Technology
    tabHost.setCurrentTab(1);
}

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // First, set the content view setContentView(R.layout.activity_rss_tabs); // Then get the TabHost TabHost tabHost = getTabHost(); /* ***************** * Art tab */ Intent artIntent = new Intent().setClass(this, RssChannelActivity.class); // Set Art category RSS URL artIntent.putExtra("rss-url", "http://feeds.reuters.com/news/artsculture?format=xml"); // The name of the art tab taken from the String resources String artTabName = getResources().getString(R.string.tab_art); TabSpec artTabSpec = tabHost.newTabSpec(artTabName) .setIndicator(artTabName, getResources().getDrawable(R.drawable.rss_tab_art)) .setContent(artIntent); // Add art tab to the TabHost tabHost.addTab(artTabSpec); /* ***************** * Tech tab */ Intent techIntent = new Intent().setClass(this, RssChannelActivity.class); // Set Tech category RSS URL techIntent.putExtra("rss-url", "http://feeds.reuters.com/reuters/technologyNews?format=xml"); // Tech tab name taken from the string resources String techTabName = getResources().getString(R.string.tab_tech); TabSpec techTabSpec = tabHost.newTabSpec(techTabName) .setIndicator(techTabName, getResources().getDrawable(R.drawable.rss_tab_tech)) .setContent(techIntent); // Add tech tab to the TabHost tabHost.addTab(techTabSpec); /* ***************** * Sports tab */ Intent sportsIntent = new Intent().setClass(this, RssChannelActivity.class); // Set Sports category RSS URL sportsIntent.putExtra("rss-url", "http://feeds.reuters.com/reuters/sportsNews?format=xml"); // Sports tab name - string resources String sportsTabName = getResources().getString(R.string.tab_sports); TabSpec sportsTabSpec = tabHost.newTabSpec(sportsTabName) .setIndicator(sportsTabName, getResources().getDrawable(R.drawable.rss_tab_sports)) .setContent(sportsIntent); // Add sports tab to the TabHost tabHost.addTab(sportsTabSpec); // Set current tab to Technology tabHost.setCurrentTab(1); }

}

public class RssChannelActivity extends Activity {
// A reference to this activity private RssChannelActivity local; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_rss_channel); // Get the RSS URL that was set in the RssTabActivity String rssUrl = (String)getIntent().getExtras().get("rss-url"); // Set reference to this activity local = this; GetRSSDataTask task = new GetRSSDataTask(); // Start process RSS task task.execute(rssUrl); } /** * This class downloads and parses RSS Channel feed. * * @author itcuties * */ private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem> > { @Override protected List<RssItem> doInBackground(String... urls) { try { // Create RSS reader RssReader rssReader = new RssReader(urls[0]); // Parse RSS, get items return rssReader.getItems(); } catch (Exception e) { Log.e("RssChannelActivity", e.getMessage()); } return null; } @Override protected void onPostExecute(List<RssItem> result) { // Get a ListView from the RSS Channel view ListView itcItems = (ListView) findViewById(R.id.rssChannelListView); // Create a list adapter ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(local,android.R.layout.simple_list_item_1, result); // Set list adapter for the ListView itcItems.setAdapter(adapter); // Set list view item click listener itcItems.setOnItemClickListener(new ListListener(result, local)); } }

// A reference to this activity private RssChannelActivity local; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_rss_channel); // Get the RSS URL that was set in the RssTabActivity String rssUrl = (String)getIntent().getExtras().get("rss-url"); // Set reference to this activity local = this; GetRSSDataTask task = new GetRSSDataTask(); // Start process RSS task task.execute(rssUrl); } /** * This class downloads and parses RSS Channel feed. * * @author itcuties * */ private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem> > { @Override protected List<RssItem> doInBackground(String... urls) { try { // Create RSS reader RssReader rssReader = new RssReader(urls[0]); // Parse RSS, get items return rssReader.getItems(); } catch (Exception e) { Log.e("RssChannelActivity", e.getMessage()); } return null; } @Override protected void onPostExecute(List<RssItem> result) { // Get a ListView from the RSS Channel view ListView itcItems = (ListView) findViewById(R.id.rssChannelListView); // Create a list adapter ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(local,android.R.layout.simple_list_item_1, result); // Set list adapter for the ListView itcItems.setAdapter(adapter); // Set list view item click listener itcItems.setOnItemClickListener(new ListListener(result, local)); } }

1 个答案:

答案 0 :(得分:0)

向您的活动添加视图寻呼机:

  1. 将ViewPager资源添加到您的活动资源:

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" />
    

  2. 创建寻呼机的片段资源:

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

  3. 创建片段的片段类:

    public static class DummySectionFragment extends Fragment {         / **          * fragment参数表示此节的节号          *片段。          * /         public static final String ARG_SECTION_NUMBER =“section_number”;

        public DummySectionFragment() {
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main_dummy,
                    container, false);
            TextView dummyTextView = (TextView) rootView
                    .findViewById(R.id.section_label);
            dummyTextView.setText(Integer.toString(getArguments().getInt(
                    ARG_SECTION_NUMBER)));
            return rootView;
        }
    }
    
  4. 创建寻呼机的适配器类:

    公共类SectionsPagerAdapter扩展了FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.
            Fragment fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        }
    
        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
            }
            return null;
        }
    }
    
  5. 在您的活动中使用寻呼机(您的活动必须扩展FragmentActivity):

    SectionsPagerAdapter mSectionsPagerAdapter; ViewPager mViewPager; mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); //使用sections适配器设置ViewPager。 mViewPager =(ViewPager)findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter);