java—在拖动卷影时未获得所需的输出,在拖动卡时创建新活动

vptzau2j  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(294)

我有一个“回收者”视图,当我长按该卡时显示一个阴影生成器,当拖动时打开一个新活动,其中包含该卡的所有详细信息。但在发送意图时,我没有获得该卡所需的阴影以及新活动中所需的数据。
手势。java:-

package com.example.cako;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.DragEvent;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

class Gesture extends GestureDetector.SimpleOnGestureListener implements View.OnDragListener {
    RecyclerView cardView;
    View v;
    Context context;
    String itemName, pieceInfo, price, id;
    String drawable;

    Gesture(Context co, RecyclerView recyclerView) {
        this.cardView = recyclerView;
        context = co;
//        context = co;
//        this.drawable=drawable;
//        this.itemName=itemName;
//        this.pieceInfo=pieceinfo;
//        this.price=price;
//        this.id=id;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        v = cardView.findChildViewUnder(e.getX(), e.getY());
        assert v != null;
        View.DragShadowBuilder builder = new View.DragShadowBuilder(cardView.getChildAt(cardView.getChildAdapterPosition(v)));
        cardView.startDrag(null, builder, null, 0);
        builder.getView().setOnDragListener(this);
    }

    @Override
    public boolean onDrag(View itemView, DragEvent dragEvent) {
        ImageView image;
        TextView textViewForItemName;
        TextView textViewForPrice;
        TextView textViewForPieceInfo;
        image = itemView.findViewById(R.id.imageView);
        textViewForItemName = itemView.findViewById(R.id.textViewForItem);
        textViewForPieceInfo = itemView.findViewById(R.id.textViewForPieceInfo);
        textViewForPrice = itemView.findViewById(R.id.textViewforPrice);
        if (dragEvent.getAction() == DragEvent.ACTION_DRAG_ENDED) {
            Intent intent = new Intent(context, OnDragActivity.class);
            intent.putExtra("Id", id);
//
//            Bundle bundle=new Bundle("image",image.getDrawingCache());
//            intent.putParcelable("Picture", image.getIma);
            intent.putExtra("ItemName", textViewForItemName.getText().toString());
            intent.putExtra("PieceInfo", textViewForPieceInfo.getText().toString());
            intent.putExtra("Price", textViewForPrice.getText().toString());
            context.startActivity(intent);

        }

        return true;
    }
}

回收站视图。java:-

package com.example.cako;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

class RecyclerVview extends RecyclerView.Adapter<RecyclerVview.Holder> {
    @SuppressLint("StaticFieldLeak")
    static Activity co;
    ArrayList<String> id,name,price,image,detail;
    SQLiteDatabase sqLiteDatabase;
     RecyclerView recyclerView;

    public RecyclerVview(Context applicationContext,RecyclerView recyclerVview,ArrayList<String> id, ArrayList<String> name,ArrayList<String> price,ArrayList<String> image,ArrayList<String> detail,Database database) {
        co = (Activity) applicationContext;
        recyclerView=recyclerVview;
        this.id=id;
        this.name = name;
        this.price=price;
        this.image = image;
        this.detail=detail;
        sqLiteDatabase=database.getWritableDatabase();
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(co).inflate(R.layout.layout_for_horizontal_recyclerview, parent, false);
        return new Holder(v,recyclerView);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onBindViewHolder(@NonNull final Holder holder, final int position) {
        holder.textViewForItemName.setText(name.get(position));
        holder.textViewForPrice.setText(price.get(position));
        holder.textViewForPieceInfo.setText(detail.get(position));
        Glide.with(co).load(image.get(position)).into(holder.image);
        holder.floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Recycle")
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View view) {
                ContentValues values=new ContentValues();
                String s=co.getSharedPreferences("com.example.cako.SharedPreferenceInCako",Context.MODE_PRIVATE).getString("Email","null");
                assert s != null;
                if(!s.equals("null")){
                    Cursor cursor=sqLiteDatabase.rawQuery("Select cakeId from Cart where Email=?",new String[]{s});
                    while(cursor.moveToNext()){
                        if(!cursor.getString(0).equals(id.get(position))){
                            values.put("cakeID",id.get(position));
                            values.put("Email",s);
                            sqLiteDatabase.insert("Cart",null,values);
                        }
                    }
                }
            }
        });
        holder.textViewForPrice.setText(price.get(position));
        holder.textViewForPieceInfo.setText(detail.get(position));
    }

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

    public static class Holder extends RecyclerView.ViewHolder {
        GestureDetector gestureDetector;
        CardView cardView;
        ImageView image;
        TextView textViewForItemName;
        FloatingActionButton floatingActionButton;
        TextView textViewForPrice;
        TextView textViewForPieceInfo;

        @SuppressLint("ClickableViewAccessibility")
        public Holder(@NonNull View itemView,RecyclerView recyclerView) {
            super(itemView);
            cardView = (CardView) itemView.findViewById(R.id.cardViewForHorizontalInFirst);
            image = itemView.findViewById(R.id.imageView);
            textViewForItemName = itemView.findViewById(R.id.textViewForItem);
            floatingActionButton = itemView.findViewById(R.id.floatingActionButton);
            textViewForPieceInfo = itemView.findViewById(R.id.textViewForPieceInfo);
            textViewForPrice = itemView.findViewById(R.id.textViewforPrice);
            gestureDetector = new GestureDetector(co, new Gesture(co,recyclerView));
            cardView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    gestureDetector.onTouchEvent(motionEvent);
                    return true;
                }
            });

        }
    }
}

翁德拉gactivity:-

package com.example.cako;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.transition.AutoTransition;
import android.transition.TransitionManager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.bumptech.glide.Glide;

public class OnDragActivity extends AppCompatActivity {
    SQLiteDatabase sqLiteDatabase;
    Database database;
    CardView cardView;
    ConstraintLayout constraintLayout;
    ImageView imageView;
    String id;
    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_for_on_drag_in_first_act);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarForLong);
        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setDisplayHomeAsUpEnabled(true);

        id= getIntent().getStringExtra("Id");
//        String picture = getIntent().getStringExtra("Picture");
        String PieceInfo = getIntent().getStringExtra("PieceInfo");
        String ItemName = getIntent().getStringExtra("ItemName");
        String price = getIntent().getStringExtra("Price");

//        ImageView imageForLongPressedAct = (ImageView) findViewById(R.id.imageForLongPressedAct);
//        Glide.with(OnDragActivity.this).load(picture).into(imageForLongPressedAct);

        TextView textForCakeInLong = (TextView) findViewById(R.id.textForCakeInLong);
        textForCakeInLong.setText(ItemName);

        TextView textForCakePriceInLong = (TextView) findViewById(R.id.textForCakePriceInLong);
        textForCakePriceInLong.setText(price );

        TextView textForCakeDetailInLong = (TextView) findViewById(R.id.textForCakeDetailInLong);
        textForCakeDetailInLong.setText(PieceInfo);

        database = new Database(OnDragActivity.this);
        sqLiteDatabase=database.getWritableDatabase();
        ImageButton buttonForLovedInLong=(ImageButton)findViewById(R.id.buttonForLovedInLong) ;
//        buttonForLovedInLong.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                ContentValues values=new ContentValues();
//                String s=getSharedPreferences("com.example.cako.SharedPreferenceInCako", Context.MODE_PRIVATE).getString("Email","null");
//                assert s != null;
//                if(!s.equals("null")){
//                    @SuppressLint("Recycle") Cursor cursor=sqLiteDatabase.rawQuery("Select cakeId from Loved where Email=?",new String[]{s});
//                    while(cursor.moveToNext()){
//                        if(!cursor.getString(0).equals(id)){
//                            values.put("cakeID",id);
//                            values.put("Email",s);
//                            sqLiteDatabase.insert("Cart",null,values);
//                        }
//                    }
//                }
//            }
//        });

        imageView = (ImageView) findViewById(R.id.arrowButtonInLong);
        imageView.setBackgroundResource(R.drawable.ic_baseline_keyboard_arrow_down_24);
        constraintLayout = (ConstraintLayout) findViewById(R.id.constraintForCakeDetailVisibleOrNotInLong);
        cardView = (CardView) findViewById(R.id.cardViewForLong);
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (constraintLayout.getVisibility() == View.GONE) {
                    TransitionManager.beginDelayedTransition(cardView, new AutoTransition());
                    imageView.setBackgroundResource(R.drawable.ic_baseline_keyboard_arrow_up_24);
                    constraintLayout.setVisibility(View.VISIBLE);
                } else {
                    TransitionManager.beginDelayedTransition(cardView, new AutoTransition());
                    imageView.setBackgroundResource(R.drawable.ic_baseline_keyboard_arrow_down_24);
                    constraintLayout.setVisibility(View.GONE);
                }
            }
        });

    }
}

一小部分的回收视图调用activity:-

//offers recy view and get  
Cursor cursor = database.getDataInExclusiveOffersRecyclerView();
    idForExclusiveOffers = new ArrayList<>();
    nameForExclusiveOffers = new ArrayList<>();
    priceForExclusiveOffers = new ArrayList<>();
    imageForExclusiveOffers = new ArrayList<>();
    detailForExclusiveOffers = new ArrayList<>();
    if (cursor.getCount() != 0) {
        while (cursor.moveToNext()) {
            idForExclusiveOffers.add(cursor.getString(0));
            nameForExclusiveOffers.add(cursor.getString(1));
            priceForExclusiveOffers.add(cursor.getString(2));
            imageForExclusiveOffers.add(cursor.getString(3));
            detailForExclusiveOffers.add(cursor.getString(4));
        }
    } else {
        Toast.makeText(FirstActivity.this, "Cursor null", Toast.LENGTH_LONG).show();
    }
    RecyclerView horizontalrecyclerView = (RecyclerView) findViewById(R.id.recyclerViewHorizontal);
    horizontalrecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
    horizontalrecyclerView.setAdapter(new RecyclerVview(this,horizontalrecyclerView, idForExclusiveOffers, nameForExclusiveOffers, priceForExclusiveOffers, imageForExclusiveOffers, detailForExclusiveOffers, database));
    //Wanted to add space between REcycler view elements
    DividerItemDecoration dividerItemDecorationHorizontal = new DividerItemDecoration(this, RecyclerView.HORIZONTAL);
    dividerItemDecorationHorizontal.setDrawable((Objects.requireNonNull(getDrawable(R.drawable.divider_recycler_vertical_direction))));
    horizontalrecyclerView.addItemDecoration(dividerItemDecorationHorizontal);

    //Best Selling recy view
    Cursor cursor1 = database.getDataInBestSellingRecyclerView();
    idForBestSelling = new ArrayList<>();
    nameForBestSelling = new ArrayList<>();
    priceForBestSelling = new ArrayList<>();
    imageForBestSelling = new ArrayList<>();
    detailForBestSelling = new ArrayList<>();
    while (cursor1.moveToNext()) {
        idForBestSelling.add(cursor1.getString(0));
        nameForBestSelling.add(cursor1.getString(1));
        priceForBestSelling.add(cursor1.getString(2));
        imageForBestSelling.add(cursor1.getString(3));
        detailForBestSelling.add(cursor1.getString(4));
    }
    RecyclerView verticalrecyclerView = (RecyclerView) findViewById(R.id.recyclerViewVertical);
    verticalrecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
    verticalrecyclerView.setAdapter(new RecyclerVview(this,verticalrecyclerView, idForBestSelling, nameForBestSelling, priceForBestSelling, imageForBestSelling, detailForBestSelling, database));
    //Wanted to add space between REcycler view elements
    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, RecyclerView.VERTICAL);
    dividerItemDecoration.setDrawable(Objects.requireNonNull(getDrawable(R.drawable.divider_recycler_horizontal_direction)));
    verticalrecyclerView.addItemDecoration(dividerItemDecoration);

回收卡view:-

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="178dp"
    android:background="@android:color/holo_orange_light"
    android:layout_height="240dp">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardViewForHorizontalInFirst"
        android:layout_width="168dp"
        android:layout_height="230dp"
        android:elevation="10dp"
        android:padding="10dp"
        app:cardBackgroundColor="@android:color/white"
        app:cardCornerRadius="25sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RelativeLayout
            android:id="@+id/relative"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:padding="5dp">

            <TextView
                android:layout_marginStart="10dp"
                android:id="@+id/textViewForPieceInfo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textViewForItem"
                android:layout_alignParentStart="true"
                android:layout_marginTop="6dp"
                android:layout_marginBottom="6dp"
                android:fontFamily="@font/playfair_display_bold_italic"
                android:textSize="16dp" />

            <TextView
                android:layout_marginStart="10dp"
                android:id="@+id/textViewforPrice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textViewForPieceInfo"
                android:layout_alignParentStart="true"
                android:fontFamily="@font/playfair_display_bold_italic"
                android:textSize="16dp" />

            <ImageView
                android:layout_marginStart="3dp"
                android:layout_marginEnd="3dp"
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="10dp"
                android:background="@color/cakoAppIcon_background"
                android:foregroundGravity="center_horizontal"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/textViewForItem"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageView"
                android:layout_alignParentStart="true"
                android:layout_alignParentEnd="true"
                android:layout_marginStart="5dp"
                android:layout_marginTop="4dp"
                android:layout_marginEnd="5dp"
                android:ellipsize="end"
                android:fontFamily="@font/playfair_display_bold_italic"
                android:gravity="center"
                android:maxLines="1"
                android:text="Great"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

        </RelativeLayout>

    </androidx.cardview.widget.CardView>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="15dp"
        android:layout_marginBottom="17dp"
        app:fabSize="mini"
        app:layout_anchor="@id/relative"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@drawable/ic_baseline_add_24" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题