@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); itemAdapter = new ItemAdapter(); itemAdapter.setDataSource(this); itemAdapter.setDelegate(this); Bundle arguments = getArguments(); if (arguments == null) { return; } long feedRowId = arguments.getLong(BUNDLE_EXTRA_RSS_FEED); BloclyApplication.getSharedDataSource() .fetchFeedWithId( feedRowId, new DataSource.Callback<RssFeed>() { @Override public void onSuccess(RssFeed rssFeed) { currentFeed = rssFeed; } @Override public void onError(String errorMessage) {} }); }
@Override public void onItemClicked(ItemAdapter itemAdapter, RssItem rssItem) { int positionToExpand = -1; int positionToContract = -1; if (itemAdapter.getExpandedItem() != null) { positionToContract = currentItems.indexOf(itemAdapter.getExpandedItem()); View viewToContract = recyclerView.getLayoutManager().findViewByPosition(positionToContract); if (viewToContract == null) { positionToContract = -1; } } if (itemAdapter.getExpandedItem() != rssItem) { positionToExpand = currentItems.indexOf(rssItem); itemAdapter.setExpandedItem(rssItem); } else { itemAdapter.setExpandedItem(null); } if (positionToContract > -1) { itemAdapter.notifyItemChanged(positionToContract); } if (positionToExpand > -1) { itemAdapter.notifyItemChanged(positionToExpand); delegate.get().onItemExpanded(this, itemAdapter.getExpandedItem()); } else { delegate.get().onItemContracted(this, rssItem); return; } View viewToExpand = recyclerView.getLayoutManager().findViewByPosition(positionToExpand); int lessToScroll = 0; if (positionToContract > -1 && positionToContract < positionToExpand) { lessToScroll = itemAdapter.getExpandedItemHeight() - itemAdapter.getCollapsedItemHeight(); } recyclerView.smoothScrollBy(0, viewToExpand.getTop() - lessToScroll); }