Skip to content

Commit

Permalink
scroll down when selecting a category in an image selection quest (ro…
Browse files Browse the repository at this point in the history
…ad surface, building type, ...) (#170, #770)
  • Loading branch information
westnordost committed Sep 12, 2018
1 parent bc0e709 commit c62ec1e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package de.westnordost.streetcomplete.quests;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
Expand Down Expand Up @@ -32,10 +35,14 @@ public abstract class GroupedImageListQuestAnswerFragment extends AbstractQuestF
{
public static final String OSM_VALUE = "osm_value";

private final Handler uiThread = new Handler(Looper.getMainLooper());

protected GroupedImageSelectAdapter imageSelector;
private Button showMoreButton;
private RecyclerView valueList;

private NestedScrollView scrollView;

private List<Item> allItems;
private List<Item> topItems;

Expand All @@ -54,6 +61,8 @@ public abstract class GroupedImageListQuestAnswerFragment extends AbstractQuestF
{
View view = super.onCreateView(inflater, container, savedInstanceState);

scrollView = view.findViewById(R.id.scrollView);

View contentView = setContentView(R.layout.quest_generic_list);

valueList = contentView.findViewById(R.id.listSelect);
Expand All @@ -73,6 +82,27 @@ public abstract class GroupedImageListQuestAnswerFragment extends AbstractQuestF

imageSelector = new GroupedImageSelectAdapter(lm);
imageSelector.addOnItemSelectionListener(item -> checkIsFormComplete());
imageSelector.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver()
{
@Override public void onItemRangeInserted(int positionStart, int itemCount)
{
super.onItemRangeInserted(positionStart, itemCount);

View item = lm.getChildAt(Math.max(0,positionStart-1));
if(item != null)
{
int[] itemPos = new int[2];
item.getLocationInWindow(itemPos);
int[] scrollViewPos = new int[2];
scrollView.getLocationInWindow(scrollViewPos);

uiThread.postDelayed(() ->
{
scrollView.smoothScrollTo(0,itemPos[1] - scrollViewPos[1]);
}, 250);
}
}
});
checkIsFormComplete();

return view;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,6 @@ private void toggle(int index)
selectedItem = null;
}

if(selectedItem != null)
{
int selectedIndex = items.indexOf(selectedItem);
notifyItemChanged(selectedIndex);

if(selectedItem.isGroup())
{
if(prevSelectedItem == null || getGroup(items.indexOf(prevSelectedItem)) != selectedIndex )
{
expandGroup(selectedIndex);
}
}
}
if(prevSelectedItem != null)
{
int prevSelectedIndex = items.indexOf(prevSelectedItem);
Expand All @@ -122,6 +109,19 @@ private void toggle(int index)
}
}
}
if(selectedItem != null)
{
int selectedIndex = items.indexOf(selectedItem);
notifyItemChanged(selectedIndex);

if(selectedItem.isGroup())
{
if(prevSelectedItem == null || getGroup(items.indexOf(prevSelectedItem)) != selectedIndex )
{
expandGroup(selectedIndex);
}
}
}
for (OnItemSelectionListener listener : listeners)
{
listener.onItemSelected(selectedItem);
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/fragment_quest_answer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@
</LinearLayout>

<!-- Necessary because even if the bottom sheet is already expanded, the content might
be still too large to fit onto the screen withot scrolling (it often is) -->
be still too large to fit onto the screen without scrolling (it often is) -->
<android.support.v4.widget.NestedScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">

Expand Down

0 comments on commit c62ec1e

Please sign in to comment.