こんにちは、ビュー ページャーを既存のコードに追加したいと思います。追加する最良の方法は何ですか。ここに私の RssTabActivitty.java と RssChannelActivity.java があります。
RssTabActivity は、タブを初期化するための私の主なアクティビティです。RssChannelActivity は、RSS フィードをロードします。
ここに完全なソースコードがあり ます 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); }
}
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)); } }
}