我有一个listview,当我点击listview中的任何项目时,将打开一个详细的活动。这个布局有很多小部件,如textview,ImageView,分享按钮等。现在我想要滑动项目的这个细节活动来显示列表中下一个项目的详细视图。我正在关注此http://misha.beshkin.lv/android-swipe-gesture-implementation/,但是当我从左向右滑动时,下一个视图不会显示只是向右滑动“向右滑动”消息。任何人都可以指导我如何处理这个问题?我是新来的android Answers将不胜感激。以下是我使用的代码
MainActivity.java(listview页面)
public class MainActivity extends ListActivity {
ArrayList<HashMap<String, String>> songsList;
ListView list;
LazyAdapter adapter;
JSONArray posts;
// All static variables
static final String URL = "http://siteurl/posts/pages";
static final String KEY_URL_FOR_MAP = "url_site";
static final String KEY_POSTS = "posts";
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_SITEURL = "url";
static final String KEY_DATE = "date";
static final String KEY_CONTENT = "content";
static final String KEY_AUTHOR = "author";
static final String KEY_NAME = "name";
static final String KEY_ATTACHMENTS = "attachments";
static final String KEY_SLUG = "slug";
static final String KEY_THUMB_URL = "thumbnail";
static final String KEY_IMAGES = "images";
static final String KEY_URL = "url";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
final JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(URL);
try {
posts = json.getJSONArray(KEY_POSTS);
// looping through all song nodes <song>
for(int i = 0; i < posts.length(); i++){
JSONObject c = posts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(KEY_ID);
String title = c.getString(KEY_TITLE);
String siteurl = c.getString(KEY_SITEURL);
String date = c.getString(KEY_DATE);
String content = c.getString(KEY_CONTENT);
// to remove all <P> </p> and <br /> and replace with ""
content = content.replace("<br />", "");
content = content.replace("<p>", "");
content = content.replace("</p>", "");
//authornumber is agin JSON Object
JSONObject author = c.getJSONObject(KEY_AUTHOR);
String name = author.getString(KEY_NAME);
String url = null;
String slug = null;
try {
JSONArray atta = c.getJSONArray("attachments");
for(int j = 0; j < atta.length(); j++){
JSONObject d = atta.getJSONObject(j);
slug = d.getString(KEY_SLUG);
JSONObject images = d.getJSONObject(KEY_IMAGES);
JSONObject thumbnail = images.getJSONObject(KEY_THUMB_URL);
url = thumbnail.getString(KEY_URL);
}
} catch (Exception e) {
e.printStackTrace();
}
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(KEY_ID, id);
map.put(KEY_TITLE, title);
map.put(KEY_URL_FOR_MAP, siteurl);
map.put(KEY_DATE, date);
map.put(KEY_NAME, name);
map.put(KEY_CONTENT, content);
map.put(KEY_SLUG, slug);
map.put(KEY_URL, url);
// adding HashList to ArrayList
songsList.add(map);
}
}catch (JSONException e) {
e.printStackTrace();
}
final ListView list=(ListView)findViewById(android.R.id.list);
// Getting adapter by passing json data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
// Launching new screen on Selecting Single ListItem
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map = songsList.get(position);
Intent in = new Intent(MainActivity.this, SampleDesp.class);
in.putExtra(KEY_TITLE, map.get(KEY_TITLE));
in.putExtra(KEY_URL_FOR_MAP, map.get(KEY_URL_FOR_MAP));
in.putExtra(KEY_DATE, map.get(KEY_DATE));
in.putExtra(KEY_NAME, map.get(KEY_NAME));
in.putExtra(KEY_CONTENT, map.get(KEY_CONTENT));
in.putExtra(KEY_URL, map.get(KEY_URL));
startActivity(in);
}
});
}
}
SampleDesp.java(详细活动)
public class SampleDesp extends Activity implements SimpleGestureListener {
private SimpleGestureFilter detector;
static String title;
String content;
// Your Facebook APP ID
private static String APP_ID = "308180782571605"; // Replace with your App ID
// Instance of Facebook Class
private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
Button btnFbLogin;
Button btnPostToWall;
// JSON node keys
static final String KEY_URL_FOR_MAP = "url_site";
private static final String KEY_TITLE = "title";
private static final String KEY_SITEURL = "url";
private static final String KEY_DATE = "date";
private static final String KEY_NAME = "name";
private static final String KEY_CONTENT = "content";
private static final String KEY_URL = "url";
static final String KEY_SLUG1= "slug";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sampledes);
detector = new SimpleGestureFilter(this,this);
final LinearLayout line1 = (LinearLayout)findViewById(R.id.ll1);
LinearLayout line2 = (LinearLayout)findViewById(R.id.ll2);
Button btnShare = (Button)findViewById(R.id.share);
btnShare.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
line1.setVisibility(View.VISIBLE);
}
});
// getting intent data
Intent in = getIntent();
final String url1 = in.getStringExtra(KEY_URL);
ImageView imgv = (ImageView) findViewById(R.id.imgdesc);
ImageLoader imageLoader = new ImageLoader(getApplicationContext());
imageLoader.DisplayImage(url1, imgv);
// Get JSON values from previous intent
final String title = in.getStringExtra(KEY_TITLE);
final String siteurl = in.getStringExtra(KEY_URL_FOR_MAP);
String date = in.getStringExtra(KEY_DATE);
String name = in.getStringExtra(KEY_NAME);
final String content = in.getStringExtra(KEY_CONTENT);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblUrl = (TextView) findViewById(R.id.url_label);
TextView lblCost = (TextView) findViewById(R.id.email_label);
TextView lblDesc = (TextView) findViewById(R.id.mobile_label);
TextView lblCont = (TextView) findViewById(R.id.content_label);
lblName.setText(title);
lblUrl.setText(siteurl);
lblCost.setText(date);
lblDesc.setText(name);
lblCont.setText(content);
final ImageView email3 = (ImageView) findViewById(R.id.email);
email3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
//my codes
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + title + "&body=" + content);
intent.setData(data);
startActivity(intent);
}
});
final ImageView sms4 = (ImageView) findViewById(R.id.sms);
sms4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
// Perform action on click
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setData(Uri.parse(Uri.encode(title)+Uri.encode(content)));
shareIntent.setType("text/*");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, content);
startActivity(shareIntent);
}
});
final ImageView twitter4 = (ImageView) findViewById(R.id.twitter);
twitter4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
// Perform action on click
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://twitter.com/?status=" + Uri.encode(title) + "" + Uri.encode(siteurl)));
startActivity(i);
}
});
Button btnFbLogin = (Button) findViewById(R.id.btn_fblogin);
btnFbLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Image Button", "button Clicked");
loginToFacebook();
if (facebook.isSessionValid()) {
postToWall();
}
}
});
}
@Override
public boolean dispatchTouchEvent(MotionEvent me){
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
public void onSwipe(int direction) {
String str = "";
switch (direction) {
case SimpleGestureFilter.SWIPE_RIGHT : str = "Swipe Right";
break;
case SimpleGestureFilter.SWIPE_LEFT : str = "Swipe Left";
break;
case SimpleGestureFilter.SWIPE_DOWN : str = "Swipe Down";
break;
case SimpleGestureFilter.SWIPE_UP : str = "Swipe Up";
break;
}
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
public void onDoubleTap() {
Toast.makeText(this, "Double Tap", Toast.LENGTH_SHORT).show();
}
/**
* Function to login into facebook
* */
private void loginToFacebook() {
// TODO Auto-generated method stub
{
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
facebook.setAccessToken(access_token);
Log.d("FB Sessions", "" + facebook.isSessionValid());
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[] { "email", "publish_stream" },
new DialogListener() {
@Override
public void onCancel() {
// Function to handle cancel event
}
@Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
}
@Override
public void onError(DialogError error) {
// Function to handle error
}
@Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}
});
}else{}
}
}
/**
* Function to post to facebook wall
* */
public void postToWall() {
Bundle parameters = new Bundle();
parameters.putString("title", "visit us");
parameters.putString("link", "http://india.dollardesi.net/ads/metro-logistic-packers-movers/");
// post on user's wall.
facebook.dialog(this, "feed",parameters, new DialogListener() {
@Override
public void onFacebookError(FacebookError e) {
}
@Override
public void onError(DialogError e) {
}
@Override
public void onComplete(Bundle values) {
}
@Override
public void onCancel() {
}
});
}
/**
* Function to Logout user from Facebook
* */
public void logoutFromFacebook() {
mAsyncRunner.logout(this, new RequestListener() {
@Override
public void onComplete(String response, Object state) {
Log.d("Logout from Facebook", response);
if (Boolean.parseBoolean(response) == true) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// make Login button visible
btnFbLogin.setVisibility(View.VISIBLE);
}
});
}
}
@Override
public void onIOException(IOException e, Object state) {
}
@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}
@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
}
@Override
public void onFacebookError(FacebookError e, Object state) {
}
});
}
}
SimpleGestureFilter.java
public class SimpleGestureFilter extends SimpleOnGestureListener{
public final static int SWIPE_UP = 1;
public final static int SWIPE_DOWN = 2;
public final static int SWIPE_LEFT = 3;
public final static int SWIPE_RIGHT = 4;
public final static int MODE_TRANSPARENT = 0;
public final static int MODE_SOLID = 1;
public final static int MODE_DYNAMIC = 2;
private final static int ACTION_FAKE = -13; //just an unlikely number
private int swipe_Min_Distance = 100;
private int swipe_Max_Distance = 350;
private int swipe_Min_Velocity = 100;
private int mode = MODE_DYNAMIC;
private boolean running = true;
private boolean tapIndicator = false;
private Activity context;
private GestureDetector detector;
private SimpleGestureListener listener;
public SimpleGestureFilter(Activity context,SimpleGestureListener sgl) {
this.context = context;
this.detector = new GestureDetector(context, this);
this.listener = sgl;
}
public void onTouchEvent(MotionEvent event){
if(!this.running)
return;
boolean result = this.detector.onTouchEvent(event);
if(this.mode == MODE_SOLID)
event.setAction(MotionEvent.ACTION_CANCEL);
else if (this.mode == MODE_DYNAMIC) {
if(event.getAction() == ACTION_FAKE)
event.setAction(MotionEvent.ACTION_UP);
else if (result)
event.setAction(MotionEvent.ACTION_CANCEL);
else if(this.tapIndicator){
event.setAction(MotionEvent.ACTION_DOWN);
this.tapIndicator = false;
}
}
//else just do nothing, it's Transparent
}
public void setMode(int m){
this.mode = m;
}
public int getMode(){
return this.mode;
}
public void setEnabled(boolean status){
this.running = status;
}
public void setSwipeMaxDistance(int distance){
this.swipe_Max_Distance = distance;
}
public void setSwipeMinDistance(int distance){
this.swipe_Min_Distance = distance;
}
public void setSwipeMinVelocity(int distance){
this.swipe_Min_Velocity = distance;
}
public int getSwipeMaxDistance(){
return this.swipe_Max_Distance;
}
public int getSwipeMinDistance(){
return this.swipe_Min_Distance;
}
public int getSwipeMinVelocity(){
return this.swipe_Min_Velocity;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
final float xDistance = Math.abs(e1.getX() - e2.getX());
final float yDistance = Math.abs(e1.getY() - e2.getY());
if(xDistance > this.swipe_Max_Distance || yDistance > this.swipe_Max_Distance)
return false;
velocityX = Math.abs(velocityX);
velocityY = Math.abs(velocityY);
boolean result = false;
if(velocityX > this.swipe_Min_Velocity && xDistance > this.swipe_Min_Distance){
if(e1.getX() > e2.getX()) // right to left
this.listener.onSwipe(SWIPE_LEFT);
else
this.listener.onSwipe(SWIPE_RIGHT);
result = true;
}
else if(velocityY > this.swipe_Min_Velocity && yDistance > this.swipe_Min_Distance){
if(e1.getY() > e2.getY()) // bottom to up
this.listener.onSwipe(SWIPE_UP);
else
this.listener.onSwipe(SWIPE_DOWN);
result = true;
}
return result;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
this.tapIndicator = true;
return false;
}
@Override
public boolean onDoubleTap(MotionEvent arg0) {
this.listener.onDoubleTap();;
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent arg0) {
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent arg0) {
if(this.mode == MODE_DYNAMIC){ // we owe an ACTION_UP, so we fake an
arg0.setAction(ACTION_FAKE); //action which will be converted to an ACTION_UP later.
this.context.dispatchTouchEvent(arg0);
}
return false;
}
static interface SimpleGestureListener{
void onSwipe(int direction);
void onDoubleTap();
}
}
答案 0 :(得分:0)
我认为它可能有效:
//code from your onItemClick
HashMap<String, String> map = songsList.get(position);
Intent in = new Intent(MainActivity.this, SampleDesp.class);
in.putExtra(KEY_TITLE, map.get(KEY_TITLE));
in.putExtra(KEY_URL_FOR_MAP, map.get(KEY_URL_FOR_MAP));
in.putExtra(KEY_DATE, map.get(KEY_DATE));
in.putExtra(KEY_NAME, map.get(KEY_NAME));
in.putExtra(KEY_CONTENT, map.get(KEY_CONTENT));
in.putExtra(KEY_URL, map.get(KEY_URL));
in.putExtra(LIST_POSITION, position); //passing incremented position
startActivity(in);
然后在您的详细信息活动中,您将获得列表中当前项目的位置,其中详细信息显示在屏幕上。所以现在你可以增加位置,并在onSwipe(int direction)方法中使用相同的代码(上面的代码)。因此,如果您想在左侧滑动中打开下一个项目详细信息,请执行以下操作:
public void onSwipe(int direction) {
switch (direction) {
case SimpleGestureFilter.SWIPE_RIGHT :
break;
case SimpleGestureFilter.SWIPE_LEFT :
Intent in = getIntent();
in.putExtra(KEY_TITLE, map.get(KEY_TITLE));
in.putExtra(KEY_URL_FOR_MAP, map.get(KEY_URL_FOR_MAP));
in.putExtra(KEY_DATE, map.get(KEY_DATE));
in.putExtra(KEY_NAME, map.get(KEY_NAME));
in.putExtra(KEY_CONTENT, map.get(KEY_CONTENT));
in.putExtra(KEY_URL, map.get(KEY_URL));
in.putExtra(LIST_POSITION, position); //passing clicked position
finish();
startActivity(intent);
break;
case SimpleGestureFilter.SWIPE_DOWN :
break;
case SimpleGestureFilter.SWIPE_UP :
break;
}
}
但是对于在您的意图中添加额外内容时SWIPE_LEFT中的此代码,您需要从MainActivity访问HashMap,因此您可以在MainActivity中添加公共静态方法,如:
public static String getSongFromHashMap(String key) {
return songsList.get(key);
}
在SWIPE_LEFT代码中使用此方法。我知道这不是最佳解决方案,但它应该有效:)
答案 1 :(得分:0)
更改
case SimpleGestureFilter.SWIPE_LEFT : str = "Swipe Left";
break;
到
case SimpleGestureFilter.SWIPE_LEFT : str = "Swipe Left";
//Fire intent to your detail activity here
break;
我希望我更接近你的解决方案。