使用自定义行为

时间:2017-09-26 16:53:05

标签: java android android-recyclerview behavior

我有一个代码在第二次滚动视图时遇到奇怪的行为。基本上,应用栏布局应该在向下滑动时调整垂直空间的大小,用户应该能够看到整个图像。但是当第二次向下滑动屏幕开始超快速闪烁时。任何帮助都会非常感激。

ItemDetailFragment.java

public class ItemDetailFragment extends BaseFragment implements
        LoaderManager.LoaderCallbacks<Cursor> {

private static final String TAG = ItemDetailFragment.class.getSimpleName();
private static final int LOAD_ITEM = 1002;

@BindView(R.id.photo)
ImageView mItemImageView;
@BindView(R.id.rv_item)
RecyclerView rvItem;
@BindView(R.id.toolbar)
Toolbar toolbar;

private Unbinder unbinder;
private int currentId;
private Cursor mCursor;
private ItemDetailAdapter mAdapter;
private ShareActionProvider mShareActionProvider;

public static Fragment newInstance(int id) {
    ItemDetailFragment detailFragment = new ItemDetailFragment();
    Bundle args = new Bundle();
    args.putInt(Commons.ITEM_ID_KEY, id);
    detailFragment.setArguments(args);
    return detailFragment;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_detail, container, false);
    unbinder = ButterKnife.bind(this, view);
    return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    currentId = getArguments().getInt(Commons.ITEM_ID_KEY);

    if(isVisible()){
        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
    }

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    rvItem.setLayoutManager(mLayoutManager);

    mAdapter = new ItemDetailAdapter();
    rvItem.setAdapter(mAdapter);

    getLoaderManager().restartLoader(LOAD_ITEM, null, this);
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if(isVisibleToUser){
        setHasOptionsMenu(isVisible());
    }
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_share, menu);
    MenuItem item = menu.findItem(R.id.menu_item_share);
    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.menu_item_share:
            Log.d(TAG, "Share");
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    return ItemLoader.loadItemById(getContext(), currentId);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    if (!isAdded()) {
        if (cursor != null) {
            cursor.close();
        }
        return;
    }

    mCursor = cursor;
    if (mCursor != null && !mCursor.moveToFirst()) {
        Log.e(TAG, "Error reading item detail cursor");
        mCursor.close();
        mCursor = null;
    }

    String title = mCursor.getString(ItemLoader.Query.TITLE);
    float price = 0;
    String excerpt = mCursor.getString(ItemLoader.Query.EXCERPT);
    mAdapter.swap(new Item(title, price, excerpt));

    loadItemView();
}

private void loadItemView() {
    if (mCursor != null){
        BindingUtils.loadImage(mItemImageView, mCursor.getString(ItemLoader.Query.PHOTO_URL));
    }
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    mCursor = null;
    loadItemView();
}

}

fragment_detail.xml

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_item"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:layout_marginEnd="@dimen/activity_horizontal_margin"
    android:clipToPadding="false"
    app:layout_behavior="la.disfruta.disfruta.custom.behaviour.VerticalScrollingBehaviour"/>

<android.support.design.widget.AppBarLayout
    android:id="@+id/barLayout"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_layout_height"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="@color/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/photo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="center"
            android:fitsSystemWindows="true"
            android:background="@color/photo_placeholder"
            android:contentDescription="@string/app_name"
            app:layout_collapseMode="parallax"/>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:layout_gravity="bottom"
            app:layout_collapseMode="pin">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/app_name"
                android:src="@drawable/logo" />

        </android.support.v7.widget.Toolbar>

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

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

VerticalScrollingBehaviour.java

public class VerticalScrollingBehaviour extends AppBarLayout.ScrollingViewBehavior {

    private static final String TAG = "Behaviour";

    private View mTargetView;
    private View mBanner;
    private View mToolbar;

    private int mInitialHeight;
    private int mExtHeight;
    private int mLastHeight;

    public VerticalScrollingBehaviour(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VerticalScrollingBehaviour() {
        super();
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return dependency instanceof AppBarLayout;
    }

    @Override
    public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) {
        boolean layoutHandler = super.onLayoutChild(parent, child, layoutDirection);
        if(mTargetView == null){
            mTargetView = parent.findViewById(R.id.barLayout);
            mBanner = parent.findViewById(R.id.photo);
            mToolbar = parent.findViewById(R.id.toolbar);
            if(mTargetView != null){
                init(child);
            }
        }
        return layoutHandler;
    }

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target,
                                  int dx, int dy, int[] consumed) {
        mExtHeight = mBanner.getHeight() + mToolbar.getHeight();

        if (mTargetView != null &&
                (dy < 0 && ( child.getTop() >= mInitialHeight && child.getTop() < mExtHeight) ||
                        (dy > 0 && ( child.getTop() > mInitialHeight && child.getTop() <= mExtHeight)  ) )){
            Log.d(TAG, "onNestedPreScroll - Custom Behaviour");
            resize(dy);
        } else {
            super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
        }
    }

    @Override
    public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, View child, View target,
                                    float velocityX, float velocityY) {
        return super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY);
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child,
                                       View directTargetChild, View target, int nestedScrollAxes) {
        return nestedScrollAxes == View.SCROLL_AXIS_VERTICAL;
    }

    @Override
    public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target) {
        super.onStopNestedScroll(coordinatorLayout, child, target);
    }

    private void init(View recyclerView){
        recyclerView.setClipToOutline(false);
        mInitialHeight = recyclerView.getTop();
    }

    private void resize(int dy) {
        mLastHeight = mTargetView.getLayoutParams().height + -dy;
        mTargetView.getLayoutParams().height = mLastHeight;
        mTargetView.requestLayout();
    }
}

顺便说一下,包含这些片段的活动有一个viewpager。我不知道这是否相关。但只是为了让你们知道。

0 个答案:

没有答案