Отображение изображения позади RecyclerView при пролистывании (настройка полей) - PullRequest
0 голосов
/ 25 марта 2020

Я хочу показать изображение под просмотром смахивания без перемещения в любой точке во время смахивания. Я могу получить изображение внизу, но в моем приложении я не знаю, как настроить его так, чтобы изображение было в центре.

Я добавил изображение в метод onDraw (как я видел в других сообщениях, когда они добавляли цвет). И я могу добавить изображение здесь, и оно видно за пролистанным видом.

Тем не менее, оно появляется только в верхнем левом углу? Я хочу, чтобы это было в центре страницы. Я не знаю, нужно ли изменить мой XML, или я могу добавить что-нибудь, чтобы настроить поля в методе onDraw?

enter image description here

Вот мой код:

<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
    android:gravity="center"
    android:paddingTop="0dp"
    android:paddingBottom="16dp"
    android:clipToPadding="false"
    android:clipChildren="false">


    <ImageView
        android:id="@+id/imageview_swipe_background_like"
        android:layout_width="50dp"
        android:layout_height="50dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:src="@drawable/like_thumbs_up_circle"
        android:layout_gravity="center"/>

    <ImageView
        android:id="@+id/imageview_swipe_profile_container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_marginBottom="15dp"
        android:layout_marginEnd="14dp"
        android:layout_marginStart="14dp"
        android:layout_marginTop="0dp"
        android:background="@drawable/swipe_candiate_picture_container"
        app:layout_constraintDimensionRatio="h,3:4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:scaleType="centerCrop" />

И мой сокращенный класс адаптера:

public class SwipeAdapter extends RecyclerView.Adapter {

...

SwipeAdapter(Context context, ArrayList<SwipeCandidate> candidateArrayList) {
    mContext = context;
    mCandidatesArrayList = candidateArrayList;
}

@Override
public int getItemCount() {
    return mCandidatesArrayList.size();
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view;

    view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.swipe_one_profile_in_list, parent, false);

    return new SwipeAdapter.CustomViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

    SwipeCandidate swipeCandidate = mCandidatesArrayList.get(position);
    ((CustomViewHolder) holder).bind(swipeCandidate);
}

ItemTouchHelper.Callback createHelperCallback() {

    return new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
        @Override
        public boolean onMove(@NotNull RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                              @NotNull RecyclerView.ViewHolder target) {
            return false;
        }


        @Override
        public void onChildDraw(@NotNull Canvas c, @NotNull RecyclerView recyclerView, @NotNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

            View itemView = viewHolder.itemView;

            ImageView backgroundImageLike = itemView.findViewById(R.id.imageview_swipe_background_like);
            backgroundImageLike.draw(c);

            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);


        }


        @Override
        public void onSwiped(@NotNull final RecyclerView.ViewHolder viewHolder, int swipeDir) {

            int position = viewHolder.getAdapterPosition();

            String likedUserID = mCandidatesArrayList.get(position).getCandidateUserID();

            mCandidatesArrayList.remove(position);
            notifyItemRemoved(position);

            mSwipeCloudCreateMatch.createMatchForCurrentUser(likedUserID);
            mSwipeCloudCreateMatch.createMatchForLikedUser(likedUserID);

            mSwipeCloudSaveLikedProfiles.addLikedProfileToFirestoreDB(likedUserID);

        }


        @Override
        public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            ...

        }
    };
}


private class CustomViewHolder extends RecyclerView.ViewHolder {
    ImageView candidateMainImage;

    CustomViewHolder(View itemView) {
        super(itemView);
        candidateMainImage = itemView.findViewById(R.id.imageview_swipe_profile_container);
        mContext = itemView.getContext();

    }

    void bind(SwipeCandidate candidate) {
        ...
    }
}

}

...