[英]How to start activities by swiping different listview items in android?
[英]Android item swiping on listview
幾天以來,我一直在與Android Listview
上的這個主題苦苦掙扎,但似乎做得不好。 我只是不明白該怎么做。 我已經對Adapters
(特別是BaseAdapter
)學到了最好的知識,但仍然想不出辦法。
我在網上搜索了一些信息,但並沒有完全了解。
ListView
。 我只是不能做到這一點,所以請你幫忙。 非常感謝您的時間和精力
將這樣的動畫添加到ListView沒問題,我在幾分鍾內為普通的非自定義ListView構建了此解決方案,通常這種情況適用於任何開箱即用的ListView,它們全都在適配器中。 我的答案中唯一缺少的是滑動檢測,很遺憾,我現在沒有時間對其進行測試。 但是滑動檢測並不困難,如果您使用Google進行滑動檢測,則有很多示例。 無論如何,如果您有任何疑問,請隨時提出。
結果:
我使用的是帶有ViewHolder模式的簡單BaseAdapter,沒什么特別的,無論如何我都會發布getView方法以進行說明:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(getItemId(position) == TEST_VIEW_ID) {
TestViewModel viewModel = (TestViewModel) getItem(position);
TestRow row;
if(convertView == null) {
convertView = this.inflater.inflate(TestRow.LAYOUT, parent, false);
row = new TestRow(convertView); // Here the magic happens
convertView.setTag(row);
}
row = (TestRow) convertView.getTag();
row.bind(viewModel);
}
return convertView;
}
在我的ViewHolder類(這里稱為TestRow
我為動畫創建了一些幫助器方法,下面我將進一步解釋它們,但首先,我的代碼來自TestRow
:
public class TestRow {
public static final int LAYOUT = R.layout.list_item_test;
public ImageView ivLogo;
public TextView tvFadeOut;
public TextView tvSlideIn;
public TestRow(View view) {
this.ivLogo = (ImageView) view.findViewById(R.id.ivLogo);
this.tvFadeOut = (TextView) view.findViewById(R.id.tvFadeOut);
this.tvSlideIn = (TextView) view.findViewById(R.id.tvSlideIn);
this.ivLogo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// When the ImageView is clicked the animations are applied to the TextViews.
if(tvFadeOut.getVisibility() == View.VISIBLE) {
fadeOutView(tvFadeOut);
slideInView(tvSlideIn);
} else {
fadeInView(tvFadeOut);
slideOutView(tvSlideIn);
}
}
});
}
public void bind(TestViewModel viewModel) {
// Nothing to do here
}
}
這是我用於動畫的輔助方法:
private void fadeOutView(View view) {
Animation fadeOut = AnimationUtils.loadAnimation(view.getContext(), R.anim.fade_out);
if (fadeOut != null) {
fadeOut.setAnimationListener(new ViewAnimationListener(view) {
@Override
protected void onAnimationStart(View view, Animation animation) {
}
@Override
protected void onAnimationEnd(View view, Animation animation) {
view.setVisibility(View.GONE);
}
});
view.startAnimation(fadeOut);
}
}
private void fadeInView(View view) {
Animation fadeIn = AnimationUtils.loadAnimation(view.getContext(), R.anim.fade_in);
if (fadeIn != null) {
fadeIn.setAnimationListener(new ViewAnimationListener(view) {
@Override
protected void onAnimationStart(View view, Animation animation) {
view.setVisibility(View.VISIBLE);
}
@Override
protected void onAnimationEnd(View view, Animation animation) {
}
});
view.startAnimation(fadeIn);
}
}
private void slideInView(View view) {
Animation slideIn = AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_in_right);
if (slideIn != null) {
slideIn.setAnimationListener(new ViewAnimationListener(view) {
@Override
protected void onAnimationStart(View view, Animation animation) {
view.setVisibility(View.VISIBLE);
}
@Override
protected void onAnimationEnd(View view, Animation animation) {
}
});
view.startAnimation(slideIn);
}
}
private void slideOutView(View view) {
Animation slideOut = AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_out_right);
if (slideOut != null) {
slideOut.setAnimationListener(new ViewAnimationListener(view) {
@Override
protected void onAnimationStart(View view, Animation animation) {
}
@Override
protected void onAnimationEnd(View view, Animation animation) {
view.setVisibility(View.GONE);
}
});
view.startAnimation(slideOut);
}
}
private abstract class ViewAnimationListener implements Animation.AnimationListener {
private final View view;
protected ViewAnimationListener(View view) {
this.view = view;
}
@Override
public void onAnimationStart(Animation animation) {
onAnimationStart(this.view, animation);
}
@Override
public void onAnimationEnd(Animation animation) {
onAnimationEnd(this.view, animation);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
protected abstract void onAnimationStart(View view, Animation animation);
protected abstract void onAnimationEnd(View view, Animation animation);
}
這些是我使用的動畫xml:
淡入:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="700"/>
</set>
淡出:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha
android:fromAlpha="1"
android:toAlpha="0"
android:duration="700"/>
</set>
滑入:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="100%" android:toXDelta="0%"
android:duration="700"/>
</set>
滑出:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%" android:toXDelta="100%"
android:duration="700"/>
</set>
在res / anim /中使用此xml(用於動畫目的)
這是從左到右的動畫:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700"/>
</set>
這是從右到左的動畫:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%" android:toXDelta="100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700" />
</set>
在編碼中,請使用從左到右的意圖:
this.overridePendingTransition(R.anim.animation_enter,
R.anim.animation_leave);
在編碼中使用意圖,例如從右到左
this.overridePendingTransition(R.anim.animation_leave,
R.anim.animation_enter);
對於自定義列表視圖,您可以使用以下代碼: http : //www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
只需捕捉手勢事件並應用動畫,然后制作一種布局即可在手勢事件中消失。
如果您將其用於正常用途,而不是您想要的實際工作示例,請訪問: https : //github.com/47deg/android-swipelistview
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.