【发布时间】:2014-05-27 21:16:49
【问题描述】:
我是为 Android 开发应用程序的新手,现在我正在开发我的第一个应用程序,但我遇到了问题。
按照本教程,我已经成功地让自己成为了一个导航抽屉: http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
我找到了这个例子,它完全符合我的需要 - 解析 XML 并将其显示为一个带有图像的列表,打开一个更详细的视图: http://techiedreams.com/android-rss-reader-part-two-offline-reading-swipe-through-detail-views/
在我的应用中实现最后一个示例(包含第一个链接)时我遇到了巨大的问题,因为该示例使用 FragmentActivity 而我的应用从我的 MainActivity 创建新的 Fragments(我知道 FragmentActivity 和 Fragments 是不同的)。
MainActivity 如何创建新片段:
private void displayView(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new NewsFragment();
break; }
我需要制作一个由 SplashActivity 内部的内容组成的片段。
我需要做什么才能将 SplashActivity 实现到我的 MainActivity 中(从而创建它的新片段)?我需要将 FragmentActivity 转换为 Fragment,还是需要找到一个全新的解决方案?
如果您需要并想自己尝试一下,所有内容都可以从上面的链接中获得。由于我是一个初学者,我真的希望我可以使用上面的示例,因为它非常适合我的应用程序。
SplashActivity:
public class SplashActivity extends Activity {
private String RSSFEEDURL = "http://www.nordichardware.se/feed/rss.html";
RSSFeed feed;
String fileName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
fileName = "TDRSSFeed.td";
File feedFile = getBaseContext().getFileStreamPath(fileName);
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() == null) {
// No connectivity. Check if feed File exists
if (!feedFile.exists()) {
// No connectivity & Feed file doesn't exist: Show alert to exit
// & check for connectivity
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(
"Unable to reach server, \nPlease check your connectivity.")
.setTitle("TD RSS Reader")
.setCancelable(false)
.setPositiveButton("Exit",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
// No connectivty and file exists: Read feed from the File
Toast toast = Toast.makeText(this,
"No connectivity! Reading last update...",
Toast.LENGTH_LONG);
toast.show();
feed = ReadFeed(fileName);
startLisActivity(feed);
}
} else {
// Connected - Start parsing
new AsyncLoadXMLFeed().execute();
}
}
private void startLisActivity(RSSFeed feed) {
Bundle bundle = new Bundle();
bundle.putSerializable("feed", feed);
// launch List activity
Intent intent = new Intent(SplashActivity.this, ListActivity.class);
intent.putExtras(bundle);
startActivity(intent);
// kill this activity
finish();
}
private class AsyncLoadXMLFeed extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// Obtain feed
DOMParser myParser = new DOMParser();
feed = myParser.parseXml(RSSFEEDURL);
if (feed != null && feed.getItemCount() > 0)
WriteFeed(feed);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
startLisActivity(feed);
}
}
// Method to write the feed to the File
private void WriteFeed(RSSFeed data) {
FileOutputStream fOut = null;
ObjectOutputStream osw = null;
try {
fOut = openFileOutput(fileName, MODE_PRIVATE);
osw = new ObjectOutputStream(fOut);
osw.writeObject(data);
osw.flush();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Method to read the feed from the File
private RSSFeed ReadFeed(String fName) {
FileInputStream fIn = null;
ObjectInputStream isr = null;
RSSFeed _feed = null;
File feedFile = getBaseContext().getFileStreamPath(fileName);
if (!feedFile.exists())
return null;
try {
fIn = openFileInput(fName);
isr = new ObjectInputStream(fIn);
_feed = (RSSFeed) isr.readObject();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return _feed;
}
}
【问题讨论】:
-
将 FragmentActivity 转换为 Fragment 有点像将鸡转换为鸡蛋。我建议你在做这样的特技之前先阅读Fragments tutorial。
-
感谢您的意见,但我知道,这更像是一个有经验的问题。我的问题是如何将示例实现到我的应用程序中。有什么建议吗?
标签: java android view fragment android-fragmentactivity