2014-11-22 118 views
-1
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(); 
     } 
    } 


} 

我想为此类添加右到左导航抽屉。我如何添加抽屉式导航到这个活动吗?谁来帮帮我!我不能在此活动实施抽屉式导航栏。 :-( 我希望你能帮助我,对不起我的可怜的英语/导航抽屉(从右到左)

回答

0
<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> 

定义layout_gravityend所以它是在右侧非阿拉伯手机。如果你想让它粘在右边,然后使用right