ViewPager中的第一个元素显示不正确

时间:2015-10-27 19:24:27

标签: android android-fragments android-viewpager android-scrollview

我正在使用RecyclerView来显示元素列表,您可以从该列表转到详细屏幕,即查看分页器片段。我的问题是我导航到的第一个元素(来自RecyclerView)显示不正确 - 它有NestedScrollView,我无法完全向下滚动。如果我向左或向右转到下一页,它们很好,它总是首先加载的。

我的适配器类正在关注;

public class ApodViewAdapter extends FragmentStatePagerAdapter {

private ArrayList<APOD> mDataset;

public ApodViewAdapter(FragmentManager fm, ArrayList<APOD> apodsList) {
    super(fm);
    setData(apodsList);

}

public void setData(ArrayList<APOD> apodsList){

    if(apodsList!=null){
        this.mDataset= apodsList;
        notifyDataSetChanged();
    }
}
@Override
public Fragment getItem(int position) {
    return new ApodViewFragment().newInstance(mDataset.get(position), position);


}

@Override
public int getCount() {

    return mDataset.size();
}
}

片段活动:

        public class ApodViewFragment extends Fragment implements DataInterface,        AppBarLayout.OnOffsetChangedListener {
        private ImageView mApodImageView;
        private TextView mTextView;
        private Toolbar mToolbar;
        private AppBarLayout mAppBarLayout;
    private TextView mTitle;
        private FrameLayout mContentFl;
        private APOD mApodElement;


        private static final String KEY_CONTENT = "ApodViewFragment:Content";
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
                mApodElement = (APOD) savedInstanceState.getSerializable(KEY_CONTENT);
            }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(KEY_CONTENT, mApodElement);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        ViewGroup rootView = (ViewGroup) inflater.inflate(
                R.layout.fragment_apod_material, container, false);
        mApodImageView = (ImageView) rootView.findViewById(R.id.apod_view_apod_iv);
        mTextView = (TextView) rootView.findViewById(R.id.apod_view_text_tv);
        mTitle = (TextView) rootView.findViewById(R.id.apod_fragment_title_tv);
        mContentFl = (FrameLayout) rootView.findViewById(R.id.fragment_apod_fl);
        mToolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        mAppBarLayout = (AppBarLayout) rootView.findViewById(R.id.app_bar_layout);
        mAppBarLayout.addOnOffsetChangedListener(this);
        ViewTreeObserver vto = mContentFl.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {

                setHeroImageMaxHeight();

                ViewTreeObserver obs = mContentFl.getViewTreeObserver();

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    obs.removeOnGlobalLayoutListener(this);
                } else {
                    obs.removeGlobalOnLayoutListener(this);
                }
            }

        });
        setData();
        return rootView;
    }
    public static ApodViewFragment newInstance(APOD apodElement, int position) {
        ApodViewFragment fragment = new ApodViewFragment();
        fragment.mApodElement = apodElement;
        return fragment;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void saveData(APOD apodElement) {
    }

    @Override
    public void readData() throws IOException {
    }

    private void setData() {
        if(mApodElement.getMedia_type().equals("video")){
            Picasso.with(getActivity()).load(R.drawable.videoplaceholder).into(mApodImageView);
        }else{
            Picasso.with(getActivity()).load(mApodElement.getUrl()).into(mApodImageView);
        }

        mTextView.setText(mApodElement.getExplanation());
        mTitle.setText(mApodElement.getTitle());
    }

    private void setHeroImageMaxHeight(){

        int screenHeight = ImageHelper.getDisplayHeight(getActivity());
        mToolbar.getLayoutParams().height = screenHeight - mContentFl.getHeight();
        mToolbar.requestLayout();

    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int offset) {
        float minimalTextSize = getActivity().getResources().getDimensionPixelSize(R.dimen.apod_view_title_scrolling_text_size);
        float maximumTextSize = getActivity().getResources().getDimensionPixelSize(R.dimen.apod_view_title_default_text_size);
        float absolut_offset = Math.abs(offset);

        float text_size_difference = maximumTextSize - minimalTextSize;
        float scale = (absolut_offset) / (appBarLayout.getHeight() - absolut_offset);

        if (offset < 0) {
            float result = maximumTextSize - (scale * text_size_difference);
            mTitle.setTextSize(FontHelper.pixelsToSp(getActivity(), result));


        } else {

            mTitle.setTextSize(FontHelper.pixelsToSp(getActivity(), maximumTextSize));


        }
    }
}

和XML布局文件                      

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/apod_fragment_ctl"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:expandedTitleMarginStart="16dp"
                android:fitsSystemWindows="true">
                <ImageView
                    android:id="@+id/apod_view_apod_iv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scaleType="centerCrop"
                    android:fitsSystemWindows="true"
                    app:layout_collapseMode="parallax"/>
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:fitsSystemWindows="true"
                    android:layout_height="wrap_content">
                    <TextView
                        android:id="@+id/apod_fragment_title_tv"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@android:color/white"
android:textSize="@dimen/apod_view_title_default_text_size"
                        android:layout_alignParentBottom="true"
                        app:layout_collapseMode="pin"
                        />    
                </RelativeLayout>
                </android.support.v7.widget.Toolbar>
            </android.support.design.widget.CollapsingToolbarLayout>    
        </android.support.design.widget.AppBarLayout>
        <android.support.v4.widget.NestedScrollView
            android:id="@+id/scroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:background="@android:color/holo_purple"
            android:fitsSystemWindows="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
            <FrameLayout
                android:id="@+id/fragment_apod_fl"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <android.support.v7.widget.CardView
                    android:id="@+id/cardview"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="8dp"
                    android:layout_gravity="center_vertical"
                    app:cardUseCompatPadding="true">
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">
                    <TextView
                            android:id="@+id/title"
                            android:text="TITLE"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"                            android:textAppearance="@style/TextAppearance.AppCompat.Headline"/>
                        <TextView
                            android:id="@+id/apod_view_text_tv"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>    
                    </LinearLayout>
                </android.support.v7.widget.CardView>
            </FrameLayout>
        </android.support.v4.widget.NestedScrollView>    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            app:layout_anchor="@id/cardview"
            app:layout_anchorGravity="top|right|end"
            android:src="@drawable/ic_rocket_em_blank"
            android:layout_margin="16dp"
            app:backgroundTint="@android:color/holo_purple"
            style="@style/FabStyle"/>
    </android.support.design.widget.CoordinatorLayout>

我会说原因必须在Adapter或fragment的方法中,它会创建新的Fragment,但却找不到它。任何帮助赞赏,欢呼!

0 个答案:

没有答案