Android 导航抽屉(从右到左) import adapter.FeedListAdapter; 导入app.AppController; 输入数据.FeedItem; 导入java.io.UnsupportedEncodingException; 导入java.ut
导航抽屉(从右到左)Android 导航抽屉(从右到左) import adapter.FeedListAdapter; 导入app.AppController; 输入数据.FeedItem; 导入java.io.UnsupportedEncodingException; 导入java.ut,android,android-studio,navigation-drawer,Android,Android Studio,Navigation Drawer,导航抽屉(从右到左) import adapter.FeedListAdapter; 导入app.AppController; 输入数据.FeedItem; 导入java.io.UnsupportedEncodingException; 导入java.util.ArrayList; 导入java.util.List; 导入org.json.JSONArray; 导入org.json.JSONException; 导入org.json.JSONObject; 导入android.annotatio
import adapter.FeedListAdapter;
导入app.AppController;
输入数据.FeedItem;
导入java.io.UnsupportedEncodingException;
导入java.util.ArrayList;
导入java.util.List;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.Window;
导入android.view.WindowManager;
导入android.widget.ListView;
导入android.widget.Toast;
导入com.android.volley.Cache;
导入com.android.volley.Cache.Entry;
导入com.android.volley.Request.Method;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.VolleyLog;
导入com.android.volley.toolbox.JsonObjectRequest;
公共类MainActivity扩展了活动{
私有静态最终字符串标记=MainActivity.class.getSimpleName();
私有列表视图列表视图;
专用FeedListAdapter;
私人物品清单;
专用字符串URL_提要=”http://myozawoo.esy.es/data.php";
专用字符串URL_FEED2=”http://api.androidhive.info/feed/feed.json";
//字符串页=getIntent().getExtras().getString(“页”);
@SuppressLint(“新API”)
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,0);
setContentView(R.layout.activity_main);
//字符串页=getIntent().getExtras().getString(“页”);
listView=(listView)findViewById(R.id.list);
feedItems=新的ArrayList();
listAdapter=新的FeedListAdapter(此为feedItems);
setAdapter(listAdapter);
//这两条线不需要,,
//只是为了获得facebook的外观(更改背景颜色并隐藏图标)
//getActionBar().setBackgroundDrawable(新的ColorDrawable(Color.parseColor(“#3b5998”));
//getActionBar().setIcon(
//新的ColorDrawable(getResources().getColor(android.R.color.transparent));
//我们首先检查缓存的请求
//缓存缓存=AppController.getInstance().getRequestQueue().getCache();
//第一页
字符串页=getIntent().getExtras().getString(“页”);
如果(第等于页(“1”)){
缓存缓存=AppController.getInstance().getRequestQueue().getCache();
Entry=cache.get(URL\u提要);
if(条目!=null){
//从缓存中获取数据
试一试{
字符串数据=新字符串(entry.data,“UTF-8”);
试一试{
parseJsonFeed(新的JSONObject(数据));
}捕获(JSONException e){
e、 printStackTrace();
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
}否则{
//发出新的截击请求并获取json
JsonObjectRequest jsonReq=新的JsonObjectRequest(Method.GET,
URL_提要,空,新响应。侦听器(){
@凌驾
公共void onResponse(JSONObject响应){
d(标记“Response:+Response.toString());
if(响应!=null){
parseJsonFeed(响应);
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(标记“Error:+Error.getMessage());
}
});
//将请求添加到截击请求队列
AppController.getInstance().addToRequestQueue(jsonReq);
}
}
//第二页
否则,如果(第等于页(“2”)){
缓存缓存=AppController.getInstance().getRequestQueue().getCache();
Entry=cache.get(URL\u FEED2);
if(条目!=null){
//从缓存中获取数据
试一试{
字符串数据=新字符串(entry.data,“UTF-8”);
试一试{
parseJsonFeed(新的JSONObject(数据));
}捕获(JSONException e){
e、 printStackTrace();
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
}否则{
//发出新的截击请求并获取json
JsonObjectRequest jsonReq=新的JsonObjectRequest(Method.GET,
URL_FEED2,null,新响应。侦听器(){
@凌驾
公共void onResponse(JSONObject响应){
d(标记“Response:+Response.toString());
if(响应!=null){
parseJsonFeed(响应);
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(标记“Error:+Error.getMessage());
}
});
//将请求添加到截击请求队列
AppController.getInstance().addToRequestQueue(jsonReq);
}
}
//其他四页
否则{
缓存缓存=AppController.getInstance().getRequestQueue().getCache();
Entry=cache.get(URL\u提要);
if(条目!=null){
//从缓存中获取数据
试一试{
字符串数据=新Str
import adapter.FeedListAdapter;
import app.AppController;
import 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.os.Bundle;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.Toast;
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 MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private ListView listView;
private FeedListAdapter listAdapter;
private List<FeedItem> feedItems;
private String URL_FEED = "http://myozawoo.esy.es/data.php";
private String URL_FEED2 = "http://api.androidhive.info/feed/feed.json";
// String page = getIntent().getExtras().getString("page");
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 0);
setContentView(R.layout.activity_main);
// String page = getIntent().getExtras().getString("page");
listView = (ListView) findViewById(R.id.list);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(this, feedItems);
listView.setAdapter(listAdapter);
// These two lines not needed,
// just to get the look of facebook (changing background color & hiding the icon)
// getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3b5998")));
// getActionBar().setIcon(
// new ColorDrawable(getResources().getColor(android.R.color.transparent)));
// We first check for cached request
// Cache cache = AppController.getInstance().getRequestQueue().getCache();
// Page One
String page = getIntent().getExtras().getString("page");
if(page.equals("1")) {
Cache cache = AppController.getInstance().getRequestQueue().getCache();
Entry entry = cache.get(URL_FEED);
if (entry != null) {
// fetch the data from cache
try {
String data = new String(entry.data, "UTF-8");
try {
parseJsonFeed(new JSONObject(data));
} catch (JSONException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
// making fresh volley request and getting json
JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
URL_FEED, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
VolleyLog.d(TAG, "Response: " + response.toString());
if (response != null) {
parseJsonFeed(response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to volley request queue
AppController.getInstance().addToRequestQueue(jsonReq);
}
}
//Page Two
else if (page.equals("2")) {
Cache cache = AppController.getInstance().getRequestQueue().getCache();
Entry entry = cache.get(URL_FEED2);
if (entry != null) {
// fetch the data from cache
try {
String data = new String(entry.data, "UTF-8");
try {
parseJsonFeed(new JSONObject(data));
} catch (JSONException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
// making fresh volley request and getting json
JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
URL_FEED2, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
VolleyLog.d(TAG, "Response: " + response.toString());
if (response != null) {
parseJsonFeed(response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to volley request queue
AppController.getInstance().addToRequestQueue(jsonReq);
}
}
// Other Four Pages
else {
Cache cache = AppController.getInstance().getRequestQueue().getCache();
Entry entry = cache.get(URL_FEED);
if (entry != null) {
// fetch the data from cache
try {
String data = new String(entry.data, "UTF-8");
try {
parseJsonFeed(new JSONObject(data));
} catch (JSONException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
// making fresh volley request and getting json
JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
URL_FEED, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
VolleyLog.d(TAG, "Response: " + response.toString());
if (response != null) {
parseJsonFeed(response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to volley request queue
AppController.getInstance().addToRequestQueue(jsonReq);
}
}
}
/**
* Parsing json reponse and passing the data to feed view list adapter
* */
public void parseJsonFeed(JSONObject response) {
try {
// String page = getIntent().getExtras().getString("page");
// if (page.equals("1"))
JSONArray feedArray = response.getJSONArray("feed");
for (int i = 0; i < feedArray.length(); i++) {
JSONObject feedObj = (JSONObject) feedArray.get(i);
final FeedItem item = new FeedItem();
item.setId(feedObj.getInt("id"));
item.setName(feedObj.getString("name"));
// Image might be null sometimes
String image = feedObj.isNull("image") ? null : feedObj
.getString("image");
item.setImge(image);
item.setStatus(feedObj.getString("status"));
item.setProfilePic(feedObj.getString("profilePic"));
item.setTimeStamp(feedObj.getString("timeStamp"));
// url might be null sometimes
String feedUrl = feedObj.isNull("url") ? null : feedObj
.getString("url");
item.setUrl(feedUrl);
feedItems.add(item);
}
// notify data changes to list adapater
listAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/right_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>