1
我想滑動ExpandablelistView
中的項目並將其推離屏幕。我現在可以做到這一點,但是當我的滑動手勢結束時這個動作會執行,並且當您滑動電子郵件時,我希望它像我的手指一樣移動。 (我也喜歡透明度隨着翻譯的增加而增加) 您能幫我提供一些建議或教程鏈接嗎?沿手指滑動列表視圖中的項目
我具有延伸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;
}
}
感謝您的回答。你能解釋一下你的代碼和你的變量嗎? (如_slop,離開等) –
對不起,假期是一箇舊條件,我刪除它。其餘的:即時更新我以前的帖子 – marshallino16
我用你的代碼,但現在我的應用程序不響應任何觸摸事件! –