Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecycerView not showing until after scrolling

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.

like image 425
Gabe Sechan Avatar asked May 17 '16 19:05

Gabe Sechan


3 Answers

This worked for me. Make sure you are doing it on UI thread.

runOnUiThread {
    adapter.notifuDatasetChanged()
}
like image 100
Shivaraj Patil Avatar answered Sep 18 '22 13:09

Shivaraj Patil


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.

like image 37
Gabe Sechan Avatar answered Sep 21 '22 13:09

Gabe Sechan


I suggest update your support lib/recyclerView if you have not. It maybe "wrap_content" problem. 'com.android.support:recyclerview-v7:23.2.1'

like image 41
eralp Avatar answered Sep 19 '22 13:09

eralp