OnOffsetChanged不起作用

时间:2018-02-02 15:11:06

标签: android android-layout android-fragments imageview android-appbarlayout

尝试使头像pic变小,直到使用OnOffsetChanged消失,但它在片段中不起作用。我用同样的方式尝试了它的工作。

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/collapsing_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    app:contentScrim="?attr/colorPrimary"
    app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Title"
    app:layout_scrollFlags="scroll|exitUntilCollapsed"
    app:titleEnabled="false">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="220dp"
        android:fitsSystemWindows="true"
        android:scaleType="centerCrop"
        android:src="@drawable/bg_polygon"
        app:layout_collapseMode="parallax" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:contentInsetStartWithNavigation="0dp"
        app:layout_collapseMode="pin"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView>
**some code** 
</android.support.v4.widget.NestedScrollView>

<com.mikhaellopez.circularimageview.CircularImageView
   android:id="@+id/profileImage"
   android:layout_width="100dp"
   android:layout_height="100dp"
   android:src="@drawable/photo_female_6"
   app:civ_border="true"
   app:civ_border_width="2dp"
   app:layout_anchor="@id/app_bar_layout"
   app:layout_anchorGravity="bottom|center" />

我尝试使用OnOffsetChangedListener两种方式使用AppBarLayout) v.findViewById(R.id.app_bar_layout)).addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener()) 第一个是调用(public class ProfileFragment extends Fragment implements AppBarLayout.OnOffsetChangedListener { public ProfileFragment() { // Required empty public constructor } public static ProfileFragment newInstance() { ProfileFragment fragment = new ProfileFragment(); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.app_bar_layout); } @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_profile, container, false); return inflater.inflate(R.layout.fragment_profile, container, false); } private void initComponent(View v) { final CircularImageView image = v.findViewById(R.id.profileImage); final CollapsingToolbarLayout collapsing_toolbar = v.findViewById(R.id.collapsing_toolbar); ((AppBarLayout) v.findViewById(R.id.app_bar_layout)).addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { int min_height = ViewCompat.getMinimumHeight(collapsing_toolbar) * 2; float scale = (float) (min_height + verticalOffset) / min_height; image.setScaleX(scale >= 0 ? scale : 0); image.setScaleY(scale >= 0 ? scale : 0); } }); } @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { final CircularImageView image; image = (CircularImageView) getActivity().findViewById(R.id.profileImage); final CollapsingToolbarLayout collapsing_toolbar = (CollapsingToolbarLayout) getActivity().findViewById(R.id.collapsing_toolbar); int min_height = ViewCompat.getMinimumHeight(collapsing_toolbar) * 2; float scale = (float) (min_height + verticalOffset) / min_height; image.setScaleX(scale >= 0 ? scale : 0); image.setScaleY(scale >= 0 ? scale : 0); } } 来覆盖方法 第二个是在fragmet类中实现appbarlayout

CollapsingToolbarLayout

当我调试代码时,我发现即使我使用join也没有调用该方法。

1 个答案:

答案 0 :(得分:1)

我通过添加“onactivitycreated”方法解决了这个问题,所以当我试图获取视图时我会存在

public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

    AppBarLayout mAppBarLayout = getView().findViewById(R.id.app_bar_layout);
            final CollapsingToolbarLayout collapsing_toolbar = getView().findViewById(R.id.collapsing_toolbar);
            final CircularImageView image = getView().findViewById(R.id.profileImage);

            mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
                @Override
                public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                    int min_height = ViewCompat.getMinimumHeight(collapsing_toolbar) * 2;
                    float scale = (float) (min_height + verticalOffset) / min_height;
                    image.setScaleX(scale >= 0 ? scale : 0);
                    image.setScaleY(scale >= 0 ? scale : 0);
                }
            });AppBarLayout mAppBarLayout = getView().findViewById(R.id.app_bar_layout);
            final CollapsingToolbarLayout collapsing_toolbar = getView().findViewById(R.id.collapsing_toolbar);
            final CircularImageView image = getView().findViewById(R.id.profileImage);

            mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
                @Override
                public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                    int min_height = ViewCompat.getMinimumHeight(collapsing_toolbar) * 2;
                    float scale = (float) (min_height + verticalOffset) / min_height;
                    image.setScaleX(scale >= 0 ? scale : 0);
                    image.setScaleY(scale >= 0 ? scale : 0);
                }
            });
        }