I have a recycler view that isn't showing any elements until I scroll it. Once I scroll it, any item that has been fully offscreen will show, but anything that hasn't been will not.
Fragment class:
public class ImageAndNavHeaderFragment extends Fragment {
private NetworkImageView mBackgroundImage;
private RecyclerView mScrollingTextNavBar;
private ScrollingTextNavBarAdapter mScrollingBarAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.image_and_nav_header, container, false);
mBackgroundImage = (NetworkImageView) view.findViewById(R.id.bgImage);
mBackgroundImage.setImageUrl("http://crookedcreekguides.com/wp-content/uploads/2016/01/dinner-03.jpg", VolleySingleton.getInstance(getContext()).getImageLoader());
mScrollingTextNavBar = (RecyclerView) view.findViewById(R.id.scrollTextNavBar);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mScrollingTextNavBar.setLayoutManager(layoutManager);
ArrayList<String> list = new ArrayList<>();
for(int i=0; i< 20; i++) {
list.add("Test " + i);
}
mScrollingBarAdapter = new ScrollingTextNavBarAdapter(getContext(), list);
mScrollingTextNavBar.setAdapter(mScrollingBarAdapter);
view.invalidate();
return view;
}
}
Adapter
public class ScrollingTextNavBarAdapter extends RecyclerView.Adapter<ScrollingTextNavBarAdapter.ViewHolder>{
private List<String> mStrings;
private int mSelectedIndex = 0;
private int mSelectedColor;
private int mDefaultColor;
private ItemClickedListener mListener;
private Drawable mSelectedDrawable;
private class PositionClickListener implements View.OnClickListener{
private int mPosition;
public PositionClickListener() {
}
@Override
public void onClick(View view){
if(mPosition != mSelectedIndex) {
mSelectedIndex = mPosition;
notifyDataSetChanged();
if(mListener != null) {
mListener.onClick(mPosition);
}
}
}
public void setPosition(int position) {
mPosition = position;
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public PositionClickListener listener;
public ViewHolder(TextView view) {
super(view);
textView = view;
listener = new PositionClickListener();
}
}
public ScrollingTextNavBarAdapter(Context context) {
mStrings = new ArrayList<>();
init(context);
}
public ScrollingTextNavBarAdapter(Context context, List<String> items) {
mStrings = items;
init(context);
}
@SuppressWarnings( "deprecation" )
private void init(Context context){
mSelectedColor = context.getResources().getColor(R.color.scrolling_text_nav_bar_text_selected_color);
mDefaultColor = context.getResources().getColor(R.color.scrolling_text_nav_bar_text_default_color);
mSelectedDrawable = context.getResources().getDrawable(R.drawable.scrolling_text_nav_bar_seected_drawable);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
TextView view = (TextView)LayoutInflater.from(parent.getContext()).inflate(R.layout.scrolling_text_nav_bar_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText(mStrings.get(position));
holder.textView.setTextColor(position == mSelectedIndex ? mSelectedColor : mDefaultColor);
holder.listener.setPosition(position);
holder.textView.setOnClickListener(holder.listener);
holder.textView.setBackground(position == mSelectedIndex? mSelectedDrawable : null);
}
@Override
public int getItemCount() {
return mStrings.size();
}
public void addString(String text) {
mStrings.add(text);
notifyDataSetChanged();
}
public void setItemClickedListener(ItemClickedListener listener) {
mListener = listener;
}
}
fragment xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/bgImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/scrollTextNavBar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignBottom="@id/bgImage"
/>
</RelativeLayout>
For further amusement- after I scroll an item into view and it appears, clicking on one (which calls notifyDataSetChanged) will display all of them. But calling that without scrolling (even with a delay) will not fix it.
This worked for me. Make sure you are doing it on UI thread.
runOnUiThread {
adapter.notifuDatasetChanged()
}
It turns out that my element inside the RecyclerView needed a fixed height in order to work. Setting that height to 40dp caused the view to work as expected.
I suggest update your support lib/recyclerView if you have not. It maybe "wrap_content" problem. 'com.android.support:recyclerview-v7:23.2.1'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With