我想在ExpandablelistView
中滑动一个项目并将其推离屏幕。我现在可以这样做,但是当我的滑动手势结束时,此动作会执行,并且当您刷一封电子邮件时,我希望它完全用我的手指移动,例如gmail new app。 (我也希望随着翻译的增加而具有透明度)
你能帮我提一些建议或教程吗?
我有一个扩展BaseExpandableAdapter
的适配器,一个用于检测滑动手势的类和一个mainActivity
MainActivity类
public class MainActivity extends Activity implements OnScrollListener{
ExpandableListView lv1;
private CustomListAdapter cla;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv1 = (ExpandableListView) findViewById(R.id.list);
final SwipeDetector swipeDetector = new SwipeDetector();
lv1.setOnTouchListener(swipeDetector);
cla = new CustomListAdapter(this);
addData();
lv1.setAdapter(cla);
lv1.setOnScrollListener(this);
lv1.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Object o = cla.getGroup(groupPosition);
MatchResult m =((trnmSeparator) o).getMatches().get(childPosition);
//int type = (Integer) rowType.get(position);
if (swipeDetector.swipeDetected()){
// do the onSwipe action
//Toast.makeText(MainActivity.this, "SWIPE :" + " " + m , Toast.LENGTH_SHORT).show();
float dest = 0;
v = v.findViewById(R.id.match);
float place = v.getX();
if (swipeDetector.getAction() == Action.LR){
if (place == 0) dest = v.getWidth();
else if (place > 0) dest = v.getWidth();
else if (place < 0) dest = 0;
}
else if (swipeDetector.getAction() == Action.RL){
if (place == 0) dest = -v.getWidth();
else if (place > 0) dest = 0;
else if (place < 0) dest = -v.getWidth();
}
ObjectAnimator oa = ObjectAnimator.ofFloat(v, "x", dest);
oa.setDuration(700);
oa.start();
Log.v("view type", v.toString());
return false;
} else {
// do the onItemClick action
Toast.makeText(MainActivity.this, "Selected :" + " " + m, Toast.LENGTH_SHORT).show();
return true;
}
}
});
lv1.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
if (swipeDetector.swipeDetected()){
// do the onSwipe action
Object o = lv1.getItemAtPosition(groupPosition);
Toast.makeText(MainActivity.this, "SWIPE :" + " " + o , Toast.LENGTH_SHORT).show();
return true;
}
if (lv1.isGroupExpanded(groupPosition)){
Object o = cla.getGroup(groupPosition);
Log.v("type", o.getClass().toString());
for (int i = 0; i < ((trnmSeparator) o).getMatches().size(); i++){
lv1.getChildAt(i).setX(0f);
}
}
return false;
}
});
lv1.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(MainActivity.this, "Long Press" + lv1.getItemAtPosition(arg2), Toast.LENGTH_SHORT).show();
return false;
}
});
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount; //maybe add a padding
if(loadMore) {
addData();
//cla.count += visibleItemCount; // or any other amount
cla.notifyDataSetChanged();
}
}
}
和我的滑动探测器
package com.example.jadidtar;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class SwipeDetector implements View.OnTouchListener {
public static enum Action {
LR, // Left to Right
RL, // Right to Left
None // when no action was detected
}
private static final String logTag = "SwipeDetector";
private static final int HORIZONTAL_MIN_DISTANCE = 100;
private float downX, downY, upX, upY;
private Action mSwipeDetected = Action.None;
public boolean swipeDetected() {
return mSwipeDetected != Action.None;
}
public Action getAction() {
return mSwipeDetected;
}
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
mSwipeDetected = Action.None;
return false; // allow other events like Click to be processed
}
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) > HORIZONTAL_MIN_DISTANCE) {
// left or right
if (deltaX < 0) {
mSwipeDetected = Action.LR;
return true;
}
if (deltaX > 0) {
mSwipeDetected = Action.RL;
return true;
}
}
return false;
}
}
return false;
}
}
答案 0 :(得分:0)
这是我用来做你想做的同样代码的一部分。它在(linearLayout)对象上使用onTouchListner
(我的列表视图行):
VelocityTracker _VelocityTracker;
int _slop;
ViewConfiguration vc = ViewConfiguration.get(context);
_slop = vc.getScaledTouchSlop();
[.........code ......]
_dividerRow.setOnTouchListener(new MyGestureListener()
);
class MyGestureListener implements OnTouchListener{
private PointF _init;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
_init = new PointF(event.getRawX(), event.getRawY());
if(!(v == null))
{
_VelocityTracker = VelocityTracker.obtain();
_VelocityTracker.addMovement(event);
}
break;
case MotionEvent.ACTION_UP :
break;
case MotionEvent.ACTION_MOVE :
if(event.getX() > 0)
{
_VelocityTracker.addMovement(event);
float diffX = event.getRawX() - _init.x;
float diffY = event.getRawY() - _init.y;
if (Math.abs(diffX) > _slop && Math.abs(diffX) > Math.abs(diffY)) {
_swipping = true;
((ViewGroup) v).requestDisallowInterceptTouchEvent(true);
MotionEvent cancelEvent = MotionEvent.obtain(event);
cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
v.onTouchEvent(cancelEvent);
}
if(_swipping == true)
{
ViewHelper.setTranslationX(v, diffX);
}
}
break;
}
return false;
}
}
希望有所帮助