(very basic) swipe to dismiss for recycler view

This commit is contained in:
Andrew Dolgov 2017-06-01 19:01:55 +03:00
parent 3c130c019e
commit e8fe00eecd
2 changed files with 49 additions and 6 deletions

View File

@ -26,6 +26,7 @@ import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.Html;
import android.transition.Fade;
import android.transition.Transition;
@ -340,6 +341,42 @@ public class HeadlinesFragment extends Fragment {
boolean enableSwipeToDismiss = m_prefs.getBoolean("headlines_swipe_to_dismiss", true);
if (enableSwipeToDismiss) {
ItemTouchHelper swipeHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition() - m_adapter.getHeaderCount();
try {
Article article = getArticleAtPosition(position);
if (article != null) {
if (article.unread) {
article.unread = false;
m_activity.saveArticleUnread(article);
}
}
m_articles.remove(position);
m_adapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
});
swipeHelper.attachToRecyclerView(m_list);
};
if (! (getActivity() instanceof DetailActivity)) {
m_list.setOnTouchListener(new ShowHideOnScroll(fab));
@ -451,7 +488,7 @@ public class HeadlinesFragment extends Fragment {
}
});
if (!enableSwipeToDismiss) registerForContextMenu(m_list);
//if (!enableSwipeToDismiss) registerForContextMenu(m_list);
if (m_activity.isSmallScreen()) {
m_activity.setTitle(m_feed.title);
@ -800,13 +837,13 @@ public class HeadlinesFragment extends Fragment {
//holder.position = position;
holder.view.setOnLongClickListener(new View.OnLongClickListener() {
/*holder.view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
m_activity.openContextMenu(v);
return false;
}
});
});*/
holder.view.setOnClickListener(new OnClickListener() {
@Override
@ -1572,7 +1609,13 @@ public class HeadlinesFragment extends Fragment {
}
public Article getArticleAtPosition(int position) {
return m_articles.get(position);
try {
return m_articles.get(position);
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
return null;
}
public Article getArticleById(int id) {

View File

@ -84,12 +84,12 @@
android:summary="@string/pref_headlines_show_content_long"
android:title="@string/pref_headlines_show_content" />
<!-- <org.fox.ttrss.util.LessBrokenSwitchPreference
<org.fox.ttrss.util.LessBrokenSwitchPreference
android:defaultValue="true"
android:dependency="headlines_mark_read_scroll"
android:key="headlines_swipe_to_dismiss"
android:summary="@string/pref_headlines_swipe_to_dismiss_long"
android:title="@string/pref_headlines_swipe_to_dismiss" /> -->
android:title="@string/pref_headlines_swipe_to_dismiss" />
<org.fox.ttrss.util.LessBrokenSwitchPreference
android:defaultValue="false"