RecycerView直到滚动后才显示

时间:2016-05-17 19:16:32

标签: android android-recyclerview

在滚动之前,我有一个不显示任何元素的回收站视图。一旦我滚动它,任何完全在屏幕外的项目都会显示,但任何未经过的项目都不会显示。

片段类:

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;
    }
}

适配器

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;
    }

}

片段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>

为了进一步娱乐 - 在我将项目滚动到视图中并且它出现后,单击一个(调用notifyDataSetChanged)将显示所有这些项目。但是在不滚动的情况下调用它(即使有延迟)也无法解决问题。

4 个答案:

答案 0 :(得分:3)

如果还没有,建议您更新支持lib / recyclerView。它可能是“wrap_content”问题。 'com.android.support:recyclerview-v7:23.2.1'

答案 1 :(得分:2)

事实证明,我在RecyclerView中的元素需要一个固定的高度才能工作。将该高度设置为40dp会导致视图按预期工作。

答案 2 :(得分:0)

通过滚动到新位置

来解决这个问题
layoutManager.scrollToPosition

答案 3 :(得分:0)

这对我有用。确保您正在UI线程上执行此操作。

runOnUiThread {
    adapter.notifuDatasetChanged()
}