Skip to content

Commit

Permalink
fix: Jump to top of the list when navigating back to the Parent BookS…
Browse files Browse the repository at this point in the history
…earchActivity

Moved registration of Adapter Item Click listener to the BookSearchActivity so that we can save the position of the Item clicked, when launching the BookDetailActivity, which prevents the scroll to top when navigating back from the BookDetailActivity.
  • Loading branch information
kaushiknsanji committed Oct 20, 2019
1 parent 0cdcd0c commit 4ca2979
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import com.example.kaushiknsanji.bookslibrary.dialogs.NetworkErrorDialogFragment;
import com.example.kaushiknsanji.bookslibrary.dialogs.PaginationNumberPickerDialogFragment;
import com.example.kaushiknsanji.bookslibrary.models.BookInfo;
import com.example.kaushiknsanji.bookslibrary.observers.OnAdapterItemClickListener;
import com.example.kaushiknsanji.bookslibrary.observers.OnAdapterItemDataSwapListener;
import com.example.kaushiknsanji.bookslibrary.observers.OnPagerFragmentVerticalScrollListener;
import com.example.kaushiknsanji.bookslibrary.providers.RecentBookSearchProvider;
Expand All @@ -81,7 +82,7 @@ public class BookSearchActivity
LoaderManager.LoaderCallbacks<List<BookInfo>>,
OnAdapterItemDataSwapListener, OnPagerFragmentVerticalScrollListener,
SharedPreferences.OnSharedPreferenceChangeListener,
OnClickListener {
OnAdapterItemClickListener, OnClickListener {

//Constant used for logs
private static final String LOG_TAG = BookSearchActivity.class.getSimpleName();
Expand Down Expand Up @@ -731,11 +732,13 @@ public boolean onCreateOptionsMenu(Menu menu) {
//(AdapterViews for the ViewPager will be available only at this point during the initial launch)

//Retrieving the current ViewPager position
int position = mViewPager.getCurrentItem();
int tabPosition = mViewPager.getCurrentItem();
//Retrieving the current RecyclerViewFragment
RecyclerViewFragment fragment = getFragmentByPositionFromViewPager(position);
RecyclerViewFragment fragment = getFragmentByPositionFromViewPager(tabPosition);
//Registering the OnAdapterItemDataSwapListener
fragment.registerItemDataSwapListener(this, position);
fragment.registerItemDataSwapListener(this, tabPosition);
//Registering the OnAdapterItemClickListener
fragment.registerItemClickListener(this, tabPosition);
//Registering the OnPagerFragmentVerticalScrollListener
fragment.setOnPagerFragmentVerticalScrollListener(this);
}
Expand Down Expand Up @@ -912,6 +915,8 @@ public void onTabSelected(TabLayout.Tab tab) {
RecyclerViewFragment fragment = getFragmentByPositionFromViewPager(newPosition);
//Registering the OnAdapterItemDataSwapListener for the current tab
fragment.registerItemDataSwapListener(this, newPosition);
//Registering the OnAdapterItemClickListener for the current tab
fragment.registerItemClickListener(this, newPosition);
//Registering the OnPagerFragmentVerticalScrollListener for the current tab
fragment.setOnPagerFragmentVerticalScrollListener(this);

Expand Down Expand Up @@ -949,6 +954,8 @@ public void onTabUnselected(TabLayout.Tab tab) {

//Unregistering the OnAdapterItemDataSwapListener on this tab unselected
fragment.clearItemDataSwapListener(oldPosition);
//Unregistering the OnAdapterItemClickListener on this tab unselected
fragment.clearItemClickListener(oldPosition);
//Unregistering the OnPagerFragmentVerticalScrollListener on this tab unselected
fragment.clearOnPagerFragmentVerticalScrollListener();
}
Expand Down Expand Up @@ -1266,6 +1273,24 @@ private void showNetworkErrorDialog() {

}

/**
* Method invoked when an Item on the Adapter is clicked
*
* @param itemBookInfo is the corresponding {@link BookInfo} object of the Item view
* clicked in the Adapter
* @param itemPosition is the adapter position of the corresponding Item view in the Adapter
*/
@Override
public void onItemClick(BookInfo itemBookInfo, int itemPosition) {
//Passing the selected Item's data as an Intent to the BookDetailActivity
Intent itemIntent = new Intent(this, BookDetailActivity.class);
itemIntent.putExtra(BookDetailActivity.BOOK_INFO_ITEM_STR_KEY, itemBookInfo);
startActivity(itemIntent);

//Save the Adapter Item View position
mVisibleItemViewPosition = itemPosition;
}

/**
* Custom {@link Handler} class for displaying the Network Error when it occurs
* during requesting data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public void onClick(View v) {
//Verifying the validity of the position before proceeding

//Propagating the call to the Listener with the selected Item's data
mItemClickListener.onItemClick(mBookInfoList.get(adapterPosition));
mItemClickListener.onItemClick(mBookInfoList.get(adapterPosition), adapterPosition);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ public void onClick(View v) {
//Verifying the validity of the position before proceeding

//Propagating the call to the Listener with the selected Item's data
mItemClickListener.onItemClick(mBookInfoList.get(adapterPosition));
mItemClickListener.onItemClick(mBookInfoList.get(adapterPosition), adapterPosition);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.example.kaushiknsanji.bookslibrary.adapterviews;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
Expand All @@ -34,7 +33,6 @@
import android.view.View;
import android.view.ViewGroup;

import com.example.kaushiknsanji.bookslibrary.BookDetailActivity;
import com.example.kaushiknsanji.bookslibrary.R;
import com.example.kaushiknsanji.bookslibrary.adapters.RecyclerGridAdapter;
import com.example.kaushiknsanji.bookslibrary.adapters.RecyclerListAdapter;
Expand All @@ -58,8 +56,7 @@
*
* @author Kaushik N Sanji
*/
public class RecyclerViewFragment extends Fragment
implements OnAdapterItemClickListener {
public class RecyclerViewFragment extends Fragment {

//Annotation constants that define the possible values for LayoutMode
public static final int LIST_MODE = 0;
Expand All @@ -71,7 +68,7 @@ public class RecyclerViewFragment extends Fragment
//Stores the reference to the RecyclerView inflated
private RecyclerView mRecyclerView;
//Stores the reference to the Listener OnPagerFragmentVerticalScrollListener
private OnPagerFragmentVerticalScrollListener mListener;
private OnPagerFragmentVerticalScrollListener mScrollListener;

/**
* Static constructor of the Fragment {@link RecyclerViewFragment}
Expand Down Expand Up @@ -101,14 +98,14 @@ public static RecyclerViewFragment newInstance(@LayoutMode int layoutMode) {
* @param listener is the instance of the Activity implementing the {@link OnPagerFragmentVerticalScrollListener}
*/
public void setOnPagerFragmentVerticalScrollListener(OnPagerFragmentVerticalScrollListener listener) {
mListener = listener;
mScrollListener = listener;
}

/**
* Method that unregisters the {@link OnPagerFragmentVerticalScrollListener}
*/
public void clearOnPagerFragmentVerticalScrollListener() {
mListener = null;
mScrollListener = null;
}

/**
Expand Down Expand Up @@ -170,9 +167,6 @@ private void setLayoutForGridView() {
//Initializing the Adapter for the Grid view
RecyclerGridAdapter recyclerGridAdapter = new RecyclerGridAdapter(requireContext(), R.layout.books_grid_item, bookInfoList);

//Registering the OnAdapterItemClickListener on the Adapter
recyclerGridAdapter.setOnAdapterItemClickListener(this);

//Setting the Adapter on the RecyclerView
mRecyclerView.setAdapter(recyclerGridAdapter);

Expand Down Expand Up @@ -201,9 +195,6 @@ private void setLayoutForListView() {
//Initializing the Adapter for the List view
RecyclerListAdapter recyclerListAdapter = new RecyclerListAdapter(requireContext(), R.layout.books_list_item, bookInfoList);

//Registering the OnAdapterItemClickListener on the Adapter
recyclerListAdapter.setOnAdapterItemClickListener(this);

//Setting the Adapter on the RecyclerView
mRecyclerView.setAdapter(recyclerListAdapter);

Expand Down Expand Up @@ -287,7 +278,7 @@ public void scrollToItemPosition(int position, boolean scrollImmediate) {
* to register the {@link OnAdapterItemDataSwapListener} on the Fragment position specified
*
* @param itemDataSwapListener is the instance of the Activity implementing the {@link OnAdapterItemDataSwapListener}
* @param position is the Fragment position on which this Listener is to be Registered
* @param position is the Fragment Tab position on which this Listener is to be Registered
*/
public void registerItemDataSwapListener(@Nullable OnAdapterItemDataSwapListener itemDataSwapListener, int position) {
//Retrieving the RecyclerView's Adapter
Expand All @@ -310,13 +301,48 @@ public void registerItemDataSwapListener(@Nullable OnAdapterItemDataSwapListener
* Method exposed for the {@link com.example.kaushiknsanji.bookslibrary.BookSearchActivity}
* to clear the {@link OnAdapterItemDataSwapListener} previously registered for the Fragment position specified
*
* @param position is the Fragment position on which this Listener is to be Unregistered
* @param position is the Fragment Tab position on which this Listener is to be Unregistered
*/
public void clearItemDataSwapListener(int position) {
//Propagating the call to #registerItemDataSwapListener with null to unregister
registerItemDataSwapListener(null, position);
}

/**
* Method exposed for the {@link com.example.kaushiknsanji.bookslibrary.BookSearchActivity}
* to register the {@link OnAdapterItemClickListener} on the Fragment position specified
*
* @param itemClickListener is the instance of the Activity implementing the {@link OnAdapterItemClickListener}
* @param position is the Fragment Tab position on which this Listener is to be Registered
*/
public void registerItemClickListener(@Nullable OnAdapterItemClickListener itemClickListener, int position) {
//Retrieving the RecyclerView's Adapter
RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
//Registering the listener on the corresponding adapter based on the Fragment position passed
if (position == LIST_MODE) {
//When the Fragment position is of the LIST_MODE Fragment

RecyclerListAdapter listAdapter = (RecyclerListAdapter) adapter;
listAdapter.setOnAdapterItemClickListener(itemClickListener);
} else if (position == GRID_MODE) {
//When the Fragment position is of the GRID_MODE Fragment

RecyclerGridAdapter gridAdapter = (RecyclerGridAdapter) adapter;
gridAdapter.setOnAdapterItemClickListener(itemClickListener);
}
}

/**
* Method exposed for the {@link com.example.kaushiknsanji.bookslibrary.BookSearchActivity}
* to clear the {@link OnAdapterItemClickListener} previously registered for the Fragment position specified
*
* @param position is the Fragment Tab position on which this Listener is to be Unregistered
*/
public void clearItemClickListener(int position) {
//Propagating the call to #registerItemClickListener with null to unregister
registerItemClickListener(null, position);
}

/**
* Method to load the data into the Adapter
*
Expand Down Expand Up @@ -357,20 +383,6 @@ public void clearData() {
swapAdapterData(bookInfos);
}

/**
* Method invoked when an Item on the Adapter is clicked
*
* @param itemBookInfo is the corresponding {@link BookInfo} object of the item view
* clicked in the Adapter
*/
@Override
public void onItemClick(BookInfo itemBookInfo) {
//Passing the selected Item's data as an Intent to the BookDetailActivity
Intent itemIntent = new Intent(getActivity(), BookDetailActivity.class);
itemIntent.putExtra(BookDetailActivity.BOOK_INFO_ITEM_STR_KEY, itemBookInfo);
startActivity(itemIntent);
}

//Defining the LayoutMode IntDef annotation with Retention only at SOURCE
@Retention(RetentionPolicy.SOURCE)
@IntDef({LIST_MODE, GRID_MODE})
Expand Down Expand Up @@ -479,7 +491,7 @@ private class RecyclerViewScrollListener extends BaseRecyclerViewScrollListener
@Override
public void onBottomReached(int verticalScrollAmount) {
//Propagating the call to the listener OnPagerFragmentVerticalScrollListener
mListener.onBottomReached(verticalScrollAmount);
mScrollListener.onBottomReached(verticalScrollAmount);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public interface OnAdapterItemClickListener {
/**
* Method invoked when an Item on the Adapter is clicked
*
* @param itemBookInfo is the corresponding {@link BookInfo} object of the item view
* @param itemBookInfo is the corresponding {@link BookInfo} object of the Item view
* clicked in the Adapter
* @param itemPosition is the adapter position of the corresponding Item view in the Adapter
*/
void onItemClick(BookInfo itemBookInfo);
void onItemClick(BookInfo itemBookInfo, int itemPosition);

}

0 comments on commit 4ca2979

Please sign in to comment.