这是Listview 滑动时显示按钮的扩展问题
我从互联网上找到了一个 Onswipetouchlistener 并已添加到课程中,现在我的问题是当我滑动创建按钮时,但我的要求是应该为每一行创建按钮,或者更准确地说,应该在被刷过的行。请帮忙
我附上了下面的代码
我的 MainActivity.java
package com.example.listviewswipe;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.view.GestureDetector;// files for using Swipe functions
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View;
public class Mainpage extends Activity {
String[] name;
ListView listview;
swipedetector swipe;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainpage);
name = getResources().getStringArray(R.array.list);
listview = (ListView) findViewById(R.id.list);
btn = (Button) findViewById(R.id.btn);
ArrayAdapter<String> adp = new ArrayAdapter<String>(Mainpage.this, android.R.layout.simple_list_item_1, name);
listview.setAdapter(adp);
listview.setOnTouchListener(swipe);
listview.setOnTouchListener(new onswipelistener(){
public void onSwipeRight(){
btn.setVisibility(View.VISIBLE);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainpage, menu);
return true;
}
}
我的 OnSwipeTouchListener 类
package com.example.listviewswipe;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
/**
* Detects swipes on views. See {@link http
* ://stackoverflow.com/questions/4139288
* /android-how-to-handle-right-to-left-swipe-gestures}
*/
public class onswipelistener implements OnTouchListener {
private static final String LOG_TAG = "OnSwipeTouchListener";
@SuppressWarnings("deprecation")
private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
@Override
public boolean onTouch(final View v, final MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
/**
* {@link SimpleOnGestureListener} implementation which detects left, right,
* up and down swipes.
*/
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public void onLongPress(MotionEvent motionEvent) {
super.onLongPress(motionEvent);
onLongPressDetected(motionEvent);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// by default, don't consume the event
boolean consumeEvent = false;
try {
if (e1 != null && e2 != null) {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY) && Math.abs(diffX) > SWIPE_THRESHOLD
&& Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
consumeEvent = true;
}
}
} catch (Exception exception) {
Log.d(LOG_TAG, "onFling", exception);
}
return consumeEvent;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onLongPressDetected(MotionEvent motionEvent) {
}
public void setIsLongpressEnabled(boolean enabled) {
gestureDetector.setIsLongpressEnabled(enabled);
}
}
我的 XML 文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Mainpage" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp" >
</ListView>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:gravity="right"
android:layout_alignParentRight="true"/>
</RelativeLayout>