horizontal ViewPager inside horizontal RecyclerView
I have a recyclerview with GridLayoutManager and inside the item I'm using a ViewPager to show photos and I get the crash below:
02-28 14:58:56.618 30212-30212/ofeksprojects.ofek.com.nightout E/AndroidRuntime: FATAL EXCEPTION: main Process: ofeksprojects.ofek.com.nightout, PID: 30212 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.LinearLayout.getChildAt(int)' on a null object reference at eu.fiskur.simpleviewpager.SimpleViewPager.setIndicator(SimpleViewPager.java:217) at eu.fiskur.simpleviewpager.SimpleViewPager.onLayout(SimpleViewPager.java:243) at android.view.View.layout(View.java:16694) at android.view.ViewGroup.layout(ViewGroup.java:5481) at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1495) at android.view.View.layout(View.java:16694) at android.view.ViewGroup.layout(ViewGroup.java:5481) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16694) at android.view.ViewGroup.layout(ViewGroup.java:5481) at android.supp ort.v7.widget.RecyclerView$LayoutManager.layoutDecoratedWithMargins(RecyclerView.java:8994) at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:685) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1518) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585) at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3719) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3436) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3988) at android.view.View.layout(View.java:16694) at android.view.ViewGroup.layout(ViewGroup.java:5481) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079) at android.view.View.layout(View.java:16694) at android.view.ViewGroup.layout(ViewGroup.java:5481) at android.widget.FrameLayout.layoutChil dren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16694) at android.view.ViewGroup.layout(ViewGroup.java:5481) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079) at android.view.View.layout(View.java:16694) at android.view.ViewGroup.layout(ViewGroup.java:5481) at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:132) at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1361) at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:874) at android.view.View.layout(View.java:16694) at android.view.ViewGroup.layout(ViewGroup.java:5481) at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1172) at android.view.View.layout(View.java:16694) at android.view.ViewGroup.la yout(ViewGroup.java:5481) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16694) at android.view.ViewGroup.layout(ViewGroup.java:5481) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) at android.view.View.layout(View.java:16694) at android.view.ViewGroup.layout(ViewGroup.java:5481) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16694) at android.view.ViewGroup.layout(ViewGroup.java:5481) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) at android.view .View.layout(View.java:16694) at android.view.ViewGroup.layout(ViewGroup.java:5481
I remember I read it is a known issue because both the recyclerview and viewpager are scrolled horizontally but I cant find the solution. anyone who can put some light on it?
here is the code -
ublic class PlacesSearchAdapter extends BaseQuickAdapter<Place,PlacesSearchAdapter.PlaceViewHolder> {
private static String PLACE_PHOTOS_BASE_URL = "https://maps.googleapis.com/maps/api/place/photo?";
public PlacesSearchAdapter(@Nullable List<Place> data) {
super(R.layout.place_item_base_layout,data);
}
@Override
protected void convert(PlaceViewHolder helper, Place item) {
helper.nameTV.setText(item.getName());
helper.addressTV.setText(item.getTextualAddress());
loadThumbs(helper,item);
}
private void loadThumbs(PlaceViewHolder helper, Place item) {
ArrayList<String> photosUrl = new ArrayList<>();
for (Place.PlacePhoto photo:item.getPhotos()) {
StringBuilder builder = new StringBuilder(PLACE_PHOTOS_BASE_URL);
builder.append("maxwidth="+photo.getWidth());
builder.append("&");
builder.append("maxheight="+photo.getHeight());
builder.append("&");
builder.append("photoreference="+photo.getReference());
photosUrl.add(builder.toString());
}
String[] photosArr = photosUrl.toArray(new String[photosUrl.size()]);
helper.photosPager.setImageUrls(photosArr, new ImageURLLoader() {
@Override
public void loadImage(ImageView imageView, String url) {
Picasso.with(imageView.getContext()).load(url).fit().into(imageView);
}
});
}
public class PlaceViewHolder extends BaseViewHolder {
SimpleViewPager photosPager;
TextView nameTV,addressTV;
MaterialFavoriteButton favoriteButton;
public PlaceViewHolder(View view) {
super(view);
nameTV = view.findViewById(R.id.nameTV_placeAdapter);
addressTV = view.findViewById(R.id.addressTV_placeAdapter);
photosPager = view.findViewById(R.id.placeThumb_placeAdapter);
int indicatorColor = Color.parseColor("#ffffff");
int selectedIndicatorColor = Color.parseColor("#fff000");
photosPager.showIndicator(indicatorColor, selectedIndicatorColor);
favoriteButton = view.findViewById(R.id.favoriteButton_placeAdapter);
}
}
}
and my onViewCreated() -
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final CardView searchCard = view.findViewById(R.id.searchCard_searchFrag);
placesRV = view.findViewById(R.id.resultList_searchFrag);
placesRV.setLayoutManager(new GridLayoutManager(placesRV.getContext(),1,GridLayoutManager.HORIZONTAL,false));
assert getActivity()!=null;
placesRV.setNestedScrollingEnabled(true);
placesRV.getLayoutManager().setAutoMeasureEnabled(true);
链接地址: http://www.djcxy.com/p/93166.html