私はリストビューでショップリストを使ってアプリケーションを開発しています。 listview
の項目を右(または左)にスワイプすると、この項目がリストビューから削除されます。Slide-Like Gmailでアイテムリストビューを削除する
私は自分のリストビューを持っていて、それを行うための関数が必要です。
ありがとうございます。
私はリストビューでショップリストを使ってアプリケーションを開発しています。 listview
の項目を右(または左)にスワイプすると、この項目がリストビューから削除されます。Slide-Like Gmailでアイテムリストビューを削除する
私は自分のリストビューを持っていて、それを行うための関数が必要です。
ありがとうございます。
これは私がこの効果を実現する方法です。 ListView lvSimple
があり、onTouchListenerをlvSimple
に追加しています。これは私の作業コードです。
float historicX = Float.NaN, historicY = Float.NaN;
static final int DELTA = 50;
enum Direction {LEFT, RIGHT;}
...
ListView lvSimple = (ListView) findViewById(R.id.linLayout);
...
lvSimple.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
historicX = event.getX();
historicY = event.getY();
break;
case MotionEvent.ACTION_UP:
if (event.getX() - historicX < -DELTA) {
FunctionDeleteRowWhenSlidingLeft();
return true;
}
else if (event.getX() - historicX > DELTA) {
FunctionDeleteRowWhenSlidingRight();
return true;
}
break;
default:
return false;
}
return false;
}
});
我々は左にスライドするとき、ときに関数FunctionDeleteRowWhenSlidingLeft()
を呼び出している、FunctionDeleteRowWhenSlidingRight()
- それぞれ右へ。この関数では、アニメーションのペーストコードが必要です。
ああ、2日間の検索とデバッグの後、それは私を助ける! ありがとう –
しかし、スライド行のインデックスを取得する方法! –
list.setOnItemClickListener(新AdapterView.OnItemClickListener(){ ます。public void onItemClick(AdapterView親、ビューV、int型の位置、長いID){ // DO STUFF HERE SwipedPosition =位置;} }); – pukingminion
Answer Android-Developerは、Nurikのコードをgist.github.comに示しています。このコードは古くなっています。彼はオープンソースのプロジェクトDash Clockでこのスワイプを却下してリスナーを使います。
Gist.github.comでコードを使用する前に知っておくべきいくつかのことがあります。
更新されたコードを使用することをおすすめします。更新されたソースhereが見つかります。
あなたが推奨するリンクにある更新されたコードを使用していますが、宣言する際に問題があります。私が "SwipeDismissListViewTouchListener.OnDismissCallback(){"という行を呼び出すと、 "SwipeDismissListViewTouchListener.OnDismissCallbackは型に解決できません"解決策がわからない – Silmarilos
@Silmarilos代わりに新しいSwipeDismissListViewTouchListener.DismissCallbacks()を使用してください。 – Blacklight
https://github.com/romannurik/Android-SwipeToDismiss/blob/master/src/com/example/android/swipedismiss/SwipeDismissTouchListener。 java – user3167086
もう1つの選択肢は、Tim RoesのEnhancedListViewライブラリを使用することです。[更新日:8/1/2015]RecycleViewの導入により、このライブラリは推奨されなくなりました。
上記のRoman NurikのSwipeToDismissリスナーには、APIレベル12以上が必要です。 Jake Whartonはこのコードを移植して、すべてのAPIレベルをSwipeToDismissNOAにサポートしました。
Tim Roesは、このライブラリをさらにサポートするように拡張しました元に戻す機能も同様です。
INFO:EnhancedListViewは推奨されていません。 – iraSenthil
@iraSenthilだから今何を使っていますか、スワイプして削除しましたか? SwipetoDismissNOAなど何か? – Darpan
私はマックロビングが書いたものを使って答えを出しました。現時点では機能していますが、すべての子供が同じ高さになっている場合にのみ機能します。
listView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
historicX = event.getX();
historicY = event.getY();
return false;
case MotionEvent.ACTION_UP:
if (listView.getChildAt(0) != null) {
int heightOfEachItem = haveListView.getChildAt(0).getHeight();
int heightOfFirstItem = -haveListView.getChildAt(0).getTop() + haveListView.getFirstVisiblePosition()*heightOfEachItem;
//IF YOU HAVE CHILDS IN LIST VIEW YOU START COUNTING
//listView.getChildAt(0).getTop() will see top of child showed in screen
//Dividing by height of view, you get how many views are not in the screen
//It needs to be Math.ceil in this case because it sometimes only shows part of last view
final int firstPosition = (int) Math.ceil(heightOfFirstItem/heightOfEachItem); // This is the same as child #0
//Here you get your List position, use historic Y to get where the user went first
final int wantedPosition = (int) Math.floor((historicY - haveListView.getChildAt(0).getTop())/heightOfEachItem) + firstPosition;
//Here you get the actually position in the screen
final int wantedChild = wantedPosition - firstPosition;
//Depending on delta, go right or left
if (event.getX() - historicX < -DELTA) {
//If something went wrong, we stop it now
if (wantedChild < 0 || wantedChild >= List.size()|| wantedChild >= listView.getChildCount()) {
return true;
}
//Start animation with 500 miliseconds of time
listView.getChildAt(wantedChild).startAnimation(outToLeftAnimation(500));
//after 500 miliseconds remove from List the item and update the adapter.
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
List.remove(wantedPosition);
updateAdapter();
}
},
500
);
return true;
} else if (event.getX() - historicX > DELTA) {
//If something went wrong, we stop it now
if (wantedChild < 0 || wantedChild >= List.size() || wantedChild >= listView.getChildCount()) {
return true;
}
//Start animation with 500 miliseconds of time
listView.getChildAt(wantedChild).startAnimation(outToRightAnimation(500));
//after 500 miliseconds remove from List the item and update the adapter.
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
List.remove(wantedPosition);
updateAdapter();
}
},
500
);
return true;
}
}
return true;
default:
return false;
}
}
});
アニメーションは、この機能を持っている:
private Animation outToLeftAnimation(int duration) {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoLeft.setDuration(duration);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
private Animation outToRightAnimation(int duration) {
Animation outtoRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoRight.setDuration(duration);
outtoRight.setInterpolator(new AccelerateInterpolator());
return outtoRight;
}
私はこれをしようとしていますし、今まで私は、エラーを見ていない、誰かが同様に試すことができれば良いでしょう。
タブレットのように、タスクを現在のタスクリストから左右にスワイプして削除します。 –
これを行う方法を表示/案内するリンクがあります:[スワイプで却下](https://gist.github.com/2980593) 。 – hardartcore