implement swipe to dismiss

This commit is contained in:
Andrew Dolgov 2017-02-17 18:26:09 +03:00
parent efd940c88d
commit b80e383511
6 changed files with 103 additions and 6 deletions

View File

@ -39,6 +39,7 @@ dependencies {
compile 'me.relex:circleindicator:1.1.1@aar'
compile 'com.viewpagerindicator:library:2.4.1'
compile 'com.nhaarman.listviewanimations:lib-core:3.1.0@aar'
compile 'com.nhaarman.listviewanimations:lib-manipulation:3.1.0@aar'
compile 'com.android.support:customtabs:23.0.0'
compile files('libs/nineoldandroids-2.4.0.jar')
compile files('libs/YouTubeAndroidPlayerApi.jar')

View File

@ -17,6 +17,8 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.Fragment;
@ -34,6 +36,7 @@ import android.view.Display;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
@ -57,6 +60,10 @@ import com.amulyakhare.textdrawable.util.ColorGenerator;
import com.google.gson.JsonElement;
import com.nhaarman.listviewanimations.appearance.AnimationAdapter;
import com.nhaarman.listviewanimations.appearance.simple.SwingBottomInAnimationAdapter;
import com.nhaarman.listviewanimations.itemmanipulation.DynamicListView;
import com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.OnDismissCallback;
import com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo.SimpleSwipeUndoAdapter;
import com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo.UndoAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
@ -117,7 +124,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
private int m_maxImageSize = 0;
private boolean m_compactLayoutMode = false;
private int m_listPreviousVisibleItem;
private ListView m_list;
private DynamicListView m_list;
private ImageLoader m_imageLoader = ImageLoader.getInstance();
private View m_listLoadingView;
private View m_topChangedView;
@ -469,10 +476,12 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
}
});
m_list = (ListView)view.findViewById(R.id.headlines_list);
m_list = (DynamicListView) view.findViewById(R.id.headlines_list);
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.headlines_fab);
boolean enableSwipeToDismiss = m_prefs.getBoolean("headlines_swipe_to_dismiss", true);
if (! (getActivity() instanceof DetailActivity)) {
m_list.setOnTouchListener(new ShowHideOnScroll(fab));
@ -482,6 +491,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
refresh(false);
}
});
} else {
fab.setVisibility(View.GONE);
}
@ -520,9 +530,41 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
m_animationAdapter.setAbsListView(m_list);
m_list.setAdapter(m_animationAdapter);
if (enableSwipeToDismiss) {
SimpleSwipeUndoAdapter swipeUndoAdapter = new SimpleSwipeUndoAdapter(m_adapter, m_activity,
new OnDismissCallback() {
@Override
public void onDismiss(final ViewGroup listView, final int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
Article article = m_adapter.getItem(position);
Log.d(TAG, "onSwipeDismiss: " + article);
if (article != null) {
if (article.unread) {
article.unread = false;
m_activity.saveArticleUnread(article);
}
m_adapter.remove(article);
m_adapter.notifyDataSetChanged();
}
}
}
}
);
swipeUndoAdapter.setAbsListView(m_list);
m_list.setAdapter(swipeUndoAdapter);
m_list.enableSimpleSwipeUndo();
}
m_list.setOnItemClickListener(this);
m_list.setOnScrollListener(this);
registerForContextMenu(m_list);
if (!enableSwipeToDismiss) registerForContextMenu(m_list);
if (m_activity.isSmallScreen()) {
m_activity.setTitle(m_feed.title);
@ -838,7 +880,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
public boolean flavorImageEmbedded;
}
private class ArticleListAdapter extends ArrayAdapter<Article> {
private class ArticleListAdapter extends ArrayAdapter<Article> implements UndoAdapter {
private ArrayList<Article> items;
public static final int VIEW_NORMAL = 0;
@ -1571,6 +1613,23 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
tv.setPaintFlags(tv.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
}
}
@NonNull
@Override
public View getUndoView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = convertView;
if (view == null) {
view = LayoutInflater.from(m_activity).inflate(R.layout.headlines_row_undo, parent, false);
}
return view;
}
@NonNull
@Override
public View getUndoClickView(@NonNull View view) {
return view.findViewById(R.id.headlines_row_undo_button);
}
}

View File

@ -11,12 +11,12 @@
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
<com.nhaarman.listviewanimations.itemmanipulation.DynamicListView
android:id="@+id/headlines_list"
android:drawSelectorOnTop="true"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</com.nhaarman.listviewanimations.itemmanipulation.DynamicListView>
</android.support.v4.widget.SwipeRefreshLayout>
<!-- <TextView

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/headlines_row_undo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="?colorPrimaryDark"
android:orientation="horizontal"
android:paddingLeft="16dp"
android:weightSum="1">
<TextView
android:background="@drawable/ripple"
android:id="@+id/top_changed_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/primary_text_dark"
android:text="@string/headline_undo_row_prompt"
android:layout_weight="1" />
<Button
android:background="@drawable/ripple"
android:id="@+id/headlines_row_undo_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/primary_text_dark"
android:text="@string/headline_undo_row_button" />
</LinearLayout>

View File

@ -243,4 +243,6 @@
<string name="open_with">Open with…</string>
<string name="confirm_catchup_above">Mark articles as read?</string>
<string name="dialog_ok">OK</string>
<string name="headline_undo_row_button">UNDO</string>
<string name="headline_undo_row_prompt">Marked as read</string>
</resources>

View File

@ -84,6 +84,12 @@
android:summary="@string/pref_headlines_show_content_long"
android:title="@string/pref_headlines_show_content" />
<org.fox.ttrss.util.LessBrokenSwitchPreference
android:defaultValue="true"
android:key="headlines_swipe_to_dismiss"
android:summary="Disables headlines context menu"
android:title="Swipe to dismiss" />
<!-- <org.fox.ttrss.util.LessBrokenSwitchPreference
android:defaultValue="true"
android:key="headlines_show_flavor_image"