Android弹出菜单不可见
我无法查看弹出式菜单。当我从左向右滑动时,需要将ListView向右推,并显示“使”弹出菜单,但不显示 我能知道原因吗 代码如下所示: activity_sample.xmlAndroid弹出菜单不可见,android,android-listview,Android,Android Listview,我无法查看弹出式菜单。当我从左向右滑动时,需要将ListView向右推,并显示“使”弹出菜单,但不显示 我能知道原因吗 代码如下所示: activity_sample.xml <com.sri.vaave2.FlyOutContainer xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="m
<com.sri.vaave2.FlyOutContainer xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#444488"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="News" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Jobs" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Internships" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#888888"
android:orientation="vertical"
android:gravity="center" >
<!-- <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Toggle Menu" /> -->
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null" />
</LinearLayout>
</com.sri.vaave2.FlyOutContainer>
MainActivity.java
package com.sri.vaave2;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.Cache;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.Cache.Entry;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.JsonObjectRequest;
import com.sri.vaave2.MainActivity;
import com.sri.vaave2.app.AppController;
import com.sri.vaave2.adapter.FeedListAdapter;
import com.sri.vaave2.data.FeedItem;
import com.sri.vaave2.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ListView;
public class MainActivity extends Activity implements OnTouchListener {
private static final String TAG = MainActivity.class.getSimpleName();
private ListView listView;
private FeedListAdapter listAdapter;
private List<FeedItem> feedItems;
private String URL_FEED = "http://coherendz.net/vaavefeed1.json";
FlyOutContainer root;
Iterator<?> itr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
this.root = (FlyOutContainer) this.getLayoutInflater().inflate(R.layout.activity_sample, null);
this.setContentView(root);
root.setOnTouchListener(this);
feedItems = new ArrayList<FeedItem>();
feedItems= getData(0);
listAdapter = new FeedListAdapter(this, feedItems);
listView = (ListView) findViewById(R.id.list);
listView.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.profile) {
Intent p = new Intent(getApplicationContext(),ActionProfile.class);
startActivity(p);
}
else if(id== R.id.feed) {
Intent f = new Intent (getApplicationContext(),Feed.class);
startActivity(f);
}
else if (id == R.id.posting){
Intent po = new Intent (getApplicationContext(),Posting.class);
startActivity(po);
}
return super.onOptionsItemSelected(item);
}
public void toggleMenu(View v){
this.root.toggleMenu();
}
/**
* Detects left and right swipes across a view.
*/
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
toggleMenu(v);
return false;
}
private List<FeedItem> getData(int nodeType) {
// TODO Auto-generated method stub
// We first check for cached request
Cache cache = null;
cache = AppController.getInstance().getRequestQueue().getCache();
Entry entry = cache.get(URL_FEED);
String data = null;
if (entry != null) {
// fetch the data from cache
try {
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);
}
return feedItems;
}
/**
* Parsing json reponse and passing the data to feed view list adapter
* */
private void parseJsonFeed(JSONObject response) {
FeedItem item;
itr = response.keys();
try
{
while(itr.hasNext())
{
String key = itr.next().toString();
JSONObject entry = response.getJSONObject(key);
JSONObject phone = entry.getJSONObject("basic");
String name = phone.getString("title");
String description = phone.getString("description");
int nodetype = phone.getInt("node_type");
JSONObject comments = entry.getJSONObject("comments");
String comments_count = comments.getString("count");
JSONObject like = entry.getJSONObject("likes");
String like_count = like.getString("count");
String readable_date = phone.getString("readable_date");
item = new FeedItem();
item.setNode_type(nodetype);
item.setName(String.valueOf(name));
item.setStatus(String.valueOf(description));
item.setReadable_date(String.valueOf(readable_date));
item.setComments_count(String.valueOf(comments_count));
item.setLike_count(String.valueOf(like_count));
feedItems.add(item);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}
package com.sri.vaave2;
导入java.io.UnsupportedEncodingException;
导入java.util.ArrayList;
导入java.util.Iterator;
导入java.util.List;
导入org.json.JSONException;
导入org.json.JSONObject;
导入com.android.volley.Cache;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.VolleyLog;
导入com.android.volley.Cache.Entry;
导入com.android.volley.Request.Method;
导入com.android.volley.toolbox.JsonObjectRequest;
导入com.sri.vaave2.main活动;
导入com.sri.vaave2.app.AppController;
导入com.sri.vaave2.adapter.FeedListAdapter;
导入com.sri.vaave2.data.FeedItem;
导入com.sri.vaave2.R;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.GestureDetector;
导入android.view.GestureDetector.SimpleOnGestureListener;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.MotionEvent;
导入android.view.view;
导入android.view.view.OnTouchListener;
导入android.widget.ListView;
公共类MainActivity将活动实现扩展到TouchListener{
私有静态最终字符串标记=MainActivity.class.getSimpleName();
私有列表视图列表视图;
专用FeedListAdapter;
私人物品清单;
专用字符串URL_提要=”http://coherendz.net/vaavefeed1.json";
飞出容器根;
迭代器itr;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
this.root=(弹出式容器)this.getLayoutFlater().inflate(R.layout.activity\u示例,null);
此.setContentView(根目录);
setOnTouchListener(this);
feedItems=新的ArrayList();
feedItems=getData(0);
listAdapter=新的FeedListAdapter(此为feedItems);
listView=(listView)findViewById(R.id.list);
setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
if(id==R.id.profile){
Intent p=新的Intent(getApplicationContext(),ActionProfile.class);
星形触觉(p);
}
else if(id==R.id.feed){
Intent f=新的Intent(getApplicationContext(),Feed.class);
星触觉(f);
}
else if(id==R.id.posting){
Intent po=新的Intent(getApplicationContext(),Posting.class);
星触觉;
}
返回super.onOptionsItemSelected(项目);
}
公共无效切换菜单(视图v){
this.root.toggleMenu();
}
/**
*检测视图中的左右滑动。
*/
公共类OnSwipeTouchListener实现OnTouchListener{
私人最终手势检测器手势检测器;
公共OnSwipeTouchListener(上下文){
gestureDetector=new gestureDetector(上下文,new GestureListener());
}
公共void onswitpeleft(){
}
公共权利{
}
公共布尔onTouch(视图v,运动事件){
返回gestureDetector.onTouchEvent(事件);
}
私有最终类GestureListener扩展了SimpleOnGestureListener{
专用静态最终整数滑动距离阈值=100;
专用静态最终整数滑动速度阈值=100;
@凌驾
公共布尔onDown(运动事件e){
返回true;
}
@凌驾
公共布尔onFling(MotionEvent e1、MotionEvent e2、float-velocityX、float-velocityY){
浮点距离x=e2.getX()-e1.getX();
浮点距离y=e2.getY()-e1.getY();
if(Math.abs(distanceX)>Math.abs(distanceY)&&Math.abs(distanceX)>滑动距离(SWIPE\u DISTANCE\u THRESHOLD)&&Math.abs(velocityX)>滑动速度(SWIPE\u THRESHOLD){
如果(距离X>0)
onswipright();
其他的
onswipleft();
返回true;
}
返回false;
}
}
}
@凌驾
公共布尔onTouch(视图v,运动事件){
//TODO自动生成的方法存根
切换菜单(v);
返回false;
}
私有列表getData(int节点类型){
//TODO自动生成的方法存根
//我们首先检查缓存的请求
Cache=null;
cache=AppController.getInstance().getRequestQueue().getCache();
Entry=cache.get(URL\u提要);
字符串数据=null;
如果(条目!=null){
//从缓存中获取数据
试试{
数据=新字符串(entry.data,“UTF-8”);
试试{
parseJsonFeed(新的JSONObject(数据));
}捕获(JSONException e){
e、 printStackTrace();
@Override
public void onSwipeRight() {
// Whatever
Toast.makeText(MainActivity.this, "Right", Toast.LENGTH_SHORT).show();
toggleMenu(listView);
}