スクロールするまで要素が表示されないリサイクラビューがあります。一度スクロールすると、完全にオフスクリーンのアイテムが表示されますが、表示されていないアイテムは表示されません。スクロールするまでRecycerViewが表示されない
フラグメントクラス:
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:さらにamusement-については
<?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>
私はビューに項目をスクロールすると、それは1つをクリック、表示された後、 (notifyDataSetChangedを呼び出す)は、それらのすべてを表示します。しかし、スクロールせずに(遅れても)それを呼び出すことはそれを修正しません。
をスクロールすることによって、私に解決しました。また、階層表示を試して、あらかじめスクロールされたコンテンツがそこにあるが見えない、または欠落していないかどうかを確認しましたか? – CommonsWare
最初の11個(20個中)の機能が呼び出されています。 –
HierarchyViewerの場合、スクリーンショットを撮るとすべてが表示されます。 –