I am using recyclerView (TwowayView) and it behaves strange (in scrolling) when data changes. I am using following code to set adapter to it (each time data changes).
// sites = list , rvlinks = twowayview
adapter = new SitesAdapter(getActivity(), rvLinks, LinksFragment.this, sites);
rvLinks.setAdapter(adapter);
I've made video of error for better understanding.
As seen , recyclerview is having large top and bottom area (see image) inspite of having only 2 items in list.
only using notifyDatasetChanged()
produces the same result. Stucked in this.
Recyclerview has fixed size set.
rvLinks.setHasFixedSize(true);
Any help would be great.
Here is my xml layout :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/dummyview"
android:layout_below="@+id/sp_category"
android:layout_marginTop="15dp"
android:background="@drawable/white_round">
<Widget.EmptyRecyclerView
android:id="@+id/rv_links"
android:layout_width="match_parent"
android:layout_height="300dp"
android:drawSelectorOnTop="false"
android:orientation="vertical"
app:twowayview_layoutManager="GridLayoutManager"
app:twowayview_numColumns="2"
tools:context=".MainActivity" />
<TextView
android:id="@+id/tv_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="No Sites Found" />
</RelativeLayout>
Here is My RecyclerView with implementation of emptyView.
public class EmptyRecyclerView extends TwoWayView {
private View emptyView;
final private AdapterDataObserver observer = new AdapterDataObserver() {
@Override
public void onChanged() {
checkIfEmpty();
}
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
checkIfEmpty();
}
@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
checkIfEmpty();
}
};
public EmptyRecyclerView(Context context) {
super(context);
}
public EmptyRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EmptyRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
void checkIfEmpty() {
if (emptyView != null && getAdapter() != null) {
final boolean emptyViewVisible = getAdapter().getItemCount() == 0;
emptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE);
setVisibility(emptyViewVisible ? GONE : VISIBLE);
}
}
@Override
public void setAdapter(Adapter adapter) {
final Adapter oldAdapter = getAdapter();
if (oldAdapter != null) {
oldAdapter.unregisterAdapterDataObserver(observer);
}
super.setAdapter(adapter);
if (adapter != null) {
adapter.registerAdapterDataObserver(observer);
}
checkIfEmpty();
}
public void setEmptyView(View emptyView) {
this.emptyView = emptyView;
checkIfEmpty();
}
}