首先我要在从左向右滑动时显示删除按钮。我检测到从左向右滑动但我的按钮不可见。
屏幕截图显示问题
layout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
// deleteBtn.setVisibility(View.VISIBLE);
mSwipeDetected = Action.None;
return true; // allow other events like Click to be processed
}
// MotionEvent.A
case MotionEvent.ACTION_MOVE: {
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// horizontal swipe detection
if (Math.abs(deltaX) > MIN_DISTANCE) {
// left or right
if (deltaX < 0) {
Log.e("lr","Swipe Left to Right");//this is working
deleteBtn.setVisibility(View.VISIBLE);
mSwipeDetected = Action.LR;
return true;
}
if (deltaX > 0) {
Log.e("Swipe Right to Left","Swipe Right to Left");
deleteBtn.setVisibility(View.GONE);
// Logger.show(Log.INFO,logTag, "Swipe Right to Left");
mSwipeDetected = Action.RL;
return true;
}
} else {deleteBtn.setVisibility(View.GONE);}
return true;
}
}
return false;
}
});
第二个问题是我还想在 onitemclicklistener
上做一些任务,所以我如何将其刺入 OnTouchListener
。
这是我完整的课程代码
package com.example.rehankhan.parkken_app.Adapter;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filterable;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.rehankhan.parkken_app.Database.DBhelper;
import com.example.rehankhan.parkken_app.Fragments.FavoriteFragment;
import com.example.rehankhan.parkken_app.MainActivity;
import com.example.rehankhan.parkken_app.Model.Favorite;
import com.example.rehankhan.parkken_app.Model.LionStreet;
import com.example.rehankhan.parkken_app.R;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Rehan Khan on 3/29/2017.
*/
public class FavoriteAdapter extends BaseAdapter implements Filterable {
public Context aContext;
Button deleteBtn;
View v;
///////////////
public static enum Action {
LR, // Left to Right
RL, // Right to Left
TB, // Top to bottom
BT, // Bottom to Top
None // when no action was detected
}
private static final String logTag = "OnSwipeTouchListener";
private static final int MIN_DISTANCE = 100;
private float downX, downY, upX, upY;
private Action mSwipeDetected = Action.None;
//////////////
float historicX = Float.NaN, historicY = Float.NaN;
static final int DELTA = 50;
enum Direction {LEFT, RIGHT;}
private List<Favorite> favoriteList,filterlist, lionStreets,lionStreetfirsttime, fullList;
public FavoriteAdapter(Context aContext, List<Favorite> lionStreets,List<Favorite> lionStreetfirsttime){
this.aContext=aContext;
// this.context = context;
this.lionStreets = lionStreets;
this.favoriteList = new ArrayList<Favorite>();
this. favoriteList.addAll(lionStreets);
this.fullList =new ArrayList<Favorite>();
fullList.addAll(lionStreetfirsttime);
}
public int getCount() {
return lionStreets.size();
}
@Override
public Object getItem(int position) {
return lionStreets.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
String CurrentlocationName, PreviouslocationName = null;
// int hiddenId;
View v = View.inflate(aContext, R.layout.fav_loc_view, null);
deleteBtn = (Button) v.findViewById(R.id.deletebtn);
LinearLayout layout = (LinearLayout)v.findViewById(R.id.fav_list_layout);
final TextView location_name = (TextView) v.findViewById(R.id.fav_location_name);
// final TextView hiddenIdTextView = (TextView) v.findViewById(R.id.hiddenid);
//String fav_loc_name = favoriteList.get(position).getFav_loc_name();
// TextView headding=(TextView) v.findViewById(R.id.heading);
Log.e("positionbefre", String.valueOf(position));
Log.e("beforlistsize", String.valueOf(lionStreets.size()));
////////////////for touch listnor
layout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
// deleteBtn.setVisibility(View.VISIBLE);
mSwipeDetected = Action.None;
return true; // allow other events like Click to be processed
}
// MotionEvent.A
case MotionEvent.ACTION_MOVE: {
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// horizontal swipe detection
if (Math.abs(deltaX) > MIN_DISTANCE) {
// left or right
if (deltaX < 0) {
Log.e("lr","Swipe Left to Right");//this is working
deleteBtn.setVisibility(View.VISIBLE);
mSwipeDetected = Action.LR;
return true;
}
if (deltaX > 0) {
Log.e("Swipe Right to Left","Swipe Right to Left");
deleteBtn.setVisibility(View.GONE);
// Logger.show(Log.INFO,logTag, "Swipe Right to Left");
mSwipeDetected = Action.RL;
return true;
}
} else {deleteBtn.setVisibility(View.GONE);}
return true;
}
}
return false;
}
});
/////////////////////////////// for slide end
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DBhelper data = DBhelper.getInstance(aContext);
data.deleteFavoite(lionStreets.get(position).getFav_loc_name());
((MainActivity) aContext ).getFavoriteFragment().resetList();
// notifyDataSetChanged();
}
});
////////////////////for touch listnor
if(lionStreets.size()!= 0 && position <= lionStreets.size()) {
Log.e("positionafter", String.valueOf(position));
Log.e("listsizeafter", String.valueOf(lionStreets.size()));
CurrentlocationName = lionStreets.get(position).getFav_loc_name();
// hiddenId = lionStreets.get(position).getID();
if (CurrentlocationName != null) {
if (CurrentlocationName.length() == 1) {
location_name.setTypeface(Typeface.DEFAULT_BOLD);
}
location_name.setText(CurrentlocationName);
// hiddenIdTextView.setText(String.valueOf(hiddenId));
}
} else{
Log.e("zeroo", String.valueOf(position));
notifyDataSetChanged();
// position=0;
}
return v;
}
public class filter_here extends android.widget.Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
// TODO Auto-generated method stub
//filterarr=lionStreets;
FilterResults Result = new FilterResults();
// if constraint is empty return the original names
/* if(constraint.length() == 0 ){
Result.values = filterlist;
Result.count = filterlist.size();
return Result;
}*/
// Filtered_Names = new ArrayList<String>();
String filterString = constraint.toString().toLowerCase();
String filterableString;
// filterlist.clear();
filterlist = new ArrayList<>();
for(int i = 0; i< favoriteList.size(); i++){
filterableString = favoriteList.get(i).getFav_loc_name();
// ||filterableString.toLowerCase().equals(filterString)
if(filterableString.toLowerCase().contains(filterString) ){
if(favoriteList.get(i).getFav_loc_name().length()!=1) {
// Filtered_Names.add(filterableString);
filterlist.add(favoriteList.get(i));
}
else {
Log.e("singleword",favoriteList.get(i).getFav_loc_name());
}
}
}
if(constraint.equals("") ){
lionStreets.clear();
filterlist.clear();
filterlist.addAll(fullList);
//lionStreets=filterlist;
Result.values =filterlist;
Result.count = filterlist.size();
}else
Result.values = filterlist;
Result.count = filterlist.size();
return Result;
}
/*
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
ArrayList<String> name = (ArrayList<String>) filterResults.values;
Log.e("filter words", String.valueOf(name.size()));
Log.e("filter list",name.toString());
notifyDataSetChanged();
}*/
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
// TODO Auto-generated method stub
// filterlist = (List<LionStreet>) results.values;
if(filterlist.size() < favoriteList.size()) {
Log.e("filltersize", String.valueOf(filterlist.size()));
// lionStreets = filterlist;
lionStreets.clear();
lionStreets.addAll(filterlist);
// fire the event
notifyDataSetChanged();
} else {
lionStreets.addAll( favoriteList);
notifyDataSetChanged();
}
}
}
public android.widget.Filter getFilter() {
// TODO Auto-generated method stub
android.widget.Filter fill = new filter_here();
return fill;
}}
/////////////////////// make touch lisner
/*private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 450;
private static final int SWIPE_VELOCITY_THRESHOLD = 400;
@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;
}
}
}*/
////////////////////end
答案 0 :(得分:1)
if (deltaX < 0) {
aContext.runOnUiThread(new Runnable()
{
@Override
public void run()
{
Log.e("lr","Swipe Left to Right");//this is working
deleteBtn.setVisibility(View.VISIBLE);
mSwipeDetected = Action.LR;
return true;
}
});
}
<强> runOnUiThread
强>
在UI线程上运行指定的操作。如果当前线程是 UI线程,然后立即执行操作。如果是当前的 线程不是UI线程,操作被发布到事件队列 UI线程。