0
我在Android上新的,我想按照本指南: facebook style newsfeed我可以擴展片段內的活動嗎?
但該指南使用純/正常活動(mainactivity)發生裂解(我用左菜單抽屜頁面片段)。這裏是我的代碼:
package com.infotech.hotbods;
import com.infotech.hotbods.app.AppController;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.infotech.hotbods.adapter.FeedListAdapter;
import com.infotech.hotbods.data.FeedItem;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Menu;
import android.widget.ListView;
import com.android.volley.Cache;
import com.android.volley.Cache.Entry;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
public class PhotosFragment extends Fragment {
private static final String TAG = PhotosFragment.class.getSimpleName();
private ListView listView;
private FeedListAdapter listAdapter;
private List<FeedItem> feedItems;
private String URL_FEED = "http://api.androidhive.info/feed/feed.json";
public PhotosFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_photos, container, false);
return rootView;
}
}
我可以使用這樣的代碼,使之出現內部片段?
public class PhotosFragment extends Fragment {
public class FeedPageActivity extends Activity {
private final String TAG = FeedPageActivity.class.getSimpleName();
private ListView listView;
private FeedListAdapter listAdapter;
private List<FeedItem> feedItems;
private String URL_FEED = "http://api.androidhive.info/feed/feed.json";
//follow the code from guide
}
}
如何讓它顯示裏面的片段? (不是從Android清單初始啓動集)
問候。
這是不可能在片段內顯示活動的。您應該通過覆蓋片段中的類似方法(如onCreate,onResume等)將代碼從Activity移植到片段。 – Leo
你爲什麼需要這樣做?從文檔中:「片段必須始終嵌入到活動中,並且片段的生命週期直接受到宿主活動生命週期的影響。如果將活動嵌入到片段中,則片段需要負責該活動的生命週期。這不是它的工作原理。「請參閱http://developer.android.com/guide/components/fragments.html中的」創建片段「 – Nactus