水平RecyclerView内部的水平ViewPager
我有一个与GridLayoutManager的recyclerview,并在项目内使用ViewPager显示照片,我得到下面的崩溃:
02-28 14:58:56.618 30212-30212 / ofeksprojects.ofek.com.tw E / AndroidRuntime:致命例外:主进程:ofeksprojects.ofek.com.twoutout,PID:30212 java.lang.NullPointerException:尝试调用virtual上在eu.fiskur.simpleviewpager.SimpleViewPager.onLayout在eu.fiskur.simpleviewpager.SimpleViewPager.setIndicator(SimpleViewPager.java:217)空对象引用方法 'android.view.View android.widget.LinearLayout.getChildAt(INT)' (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。在Android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)at android.view.ViewGroup.layout(ViewGroup.java:5481)在android.view.View.layout(View.java:16694) android.view.View.layout(View.java:16694)android.view.ViewGroup.layout(ViewGroup.java:5481)在android.supp中的android.widget.FrameLayout.onLayout(FrameLayout.java:273) ort.v7.widget.RecyclerView $ LayoutManager.layoutDecoratedWithMargins(RecyclerView.java:8994)在android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:685)在android.support.v7.widget.LinearLayoutManager.fill( LinearLayoutManager.java:1518)在android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)在android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)在android.support.v7 .widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3719)在android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3436)在android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3988 )在android.view.View.layout(View.java:16694)在android.view.ViewGroup.layout(ViewGroup.java:5481)在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)在android.view .View.layout(View.java:16694)在android.view.ViewGroup.layout(ViewGroup.java:5481)android.widget.FrameLayout.layoutChil 在android.view.View.Group.layout(ViewGroup.java)android.view.View.layout(View.java:16694)android.widget.FrameLayout.onLayout(FrameLayout.java:273)dren(FrameLayout.java:336) :5481)android.view.ViewGroup.layout(ViewGroup.java:5481)在android.view.View.layout(View.java:16694)android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079) 。android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)at android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onLayoutChild(HeaderScrollingViewBehavior.java:132).support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:132) (AppBarLayout.java:1361)在android.view.View.layout(View.java:16694)android.view.ViewGroup.layout(android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:874) ViewGroup.java:5481)在android.view.View.layout(View.java:16694)android.view.ViewGroup.la上的android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1172) 在android.view.View.layout(View.java)android.widget.FrameLayout.onLayout(FrameLayout.java:273)android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)处的视图(ViewGroup.java:5481) :16694)在android.view.ViewGroup.layout(ViewGroup.java:5481)在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)在机器人.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。在android.view.ViewGroup.layout(ViewGroup.java)的android.view.View.layout(View.java:16694)android.widget.FrameLayout.onLayout(FrameLayout.java:273)layoutchildren(FrameLayout.java:336) :5481)在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)在android.widget.LinearLayout.onLayout(LinearLayout.java:1495)在机器人。视图 .View.layout(View.java:16694)at android.view.ViewGroup.layout(ViewGroup.java:5481
我记得我读过它是一个已知的问题,因为recyclerview和viewpager水平滚动,但我无法找到解决方案。 任何人都可以对此有所了解?
这里是代码 -
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);
}
}
}
和我的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/93165.html
上一篇: horizontal ViewPager inside horizontal RecyclerView
下一篇: Android: IndexOutOfBounds Error when deleting row in ListView