我无法查看弹出菜单。当我从左向右滑动时,需要将ListView推向右侧,然后出现make Flyout菜单,但它不会出现。
我能知道原因吗?
代码如下所示:
activity_sample.xml
<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();
}
}
}
答案 0 :(得分:0)
我已经解决了这个问题。
以下代码:
listView.setOnTouchListener(new OnSwipeTouchListener(this){
@Override
public void onSwipeRight() {
// Whatever
Toast.makeText(MainActivity.this, "Right", Toast.LENGTH_SHORT).show();
toggleMenu(listView);
}
});