【问题标题】:I want to to transfer image from ne activity to another by clicking on it in RecyclerView [closed]我想通过在 RecyclerView 中单击将图像从一个活动转移到另一个活动 [关闭]
【发布时间】:2020-09-05 05:46:08
【问题描述】:

我想在 RecyclerView 中将图像和数据从一个活动复制到另一个活动,请帮助我

【问题讨论】:

  • 在单击 RecyclerView 项目时,您希望它显示在另一个活动布局中吗?如果不是,请详细说明你想要什么
  • 如果您正在寻找活动之间的共享元素,您可以查看此链接:guides.codepath.com/android/shared-element-activity-transition
  • 是的,我想在 imageview 的另一个布局中显示图像
  • 请说明您在 recyclerView 中从哪里获取图像?

标签: java android android-studio


【解决方案1】:

假设您正在制作电子商务应用程序

foodDetails.java

package com.taha.freshtohome;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.Settings;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.ValueEventListener;
import com.taha.freshtohome.Model.Food;

import java.util.UUID;
public class FoodDetails extends AppCompatActivity {
    TextView food_name, food_price, food_description;
    ImageView food_image;
    ElegantNumberButton numberButton;
    CollapsingToolbarLayout collapsingToolbarLayout;
    DatabaseReference rootRef, demoRef;



    DatabaseReference foods;

    String foodId = "";
    FirebaseDatabase database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_details);
        //Firebase
        database = FirebaseDatabase.getInstance();

        rootRef = FirebaseDatabase.getInstance().getReference();

        numberButton = findViewById(R.id.number_button);

        food_image = (ImageView)findViewById(R.id.img_food);
        food_name = (TextView)findViewById(R.id.food_name);
        food_price = (TextView)findViewById(R.id.food_price);
        food_description = (TextView)findViewById(R.id.food_description1);
        collapsingToolbarLayout = findViewById(R.id.collapsing);
        collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppBar);
        collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollaspsedAppBar);

        //Get Food Id from Intent
        if(getIntent() != null)
            foodId = getIntent().getStringExtra("FoodId");
        if (foodId != null && !foodId.isEmpty()) {

                foods = database.getReference(getIntent().getStringExtra("name"));


            getDetailsFood(foodId);
        }

    }
    private void getDetailsFood(final String foodId) {

        foods.child(foodId).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                final Food food = dataSnapshot.getValue(Food.class);
                SharedPreferences sh = getSharedPreferences("MySharedPref", MODE_PRIVATE);
                try {
                    Glide.with(FoodDetails.this).load(food.getImage()).into(food_image);
                    collapsingToolbarLayout.setTitle(food.getName());
                    food_price.setText(food.getPrice());
                    food_name.setText(food.getName());
                    food_description.setText(food.getDescription());
                } catch (Exception e) {
                    e.printStackTrace();
                }

                String android_id = Settings.Secure.getString(FoodDetails.this.getContentResolver(),
                        Settings.Secure.ANDROID_ID);
                final String s3 = sh.getString("userphone", android_id);
                numberButton.setOnValueChangeListener(new ElegantNumberButton.OnValueChangeListener() {
                    @Override
                    public void onValueChange(ElegantNumberButton view, int oldValue, int newValue) {
                        addtocart(foodId);

                    }
                });


            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }

            private void addtocart(final String foodId2) {
                foods.child(foodId2).addValueEventListener(new ValueEventListener() {
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        SharedPreferences sh = getSharedPreferences("MySharedPref", MODE_PRIVATE);

                        String android_id = Settings.Secure.getString(FoodDetails.this.getContentResolver(),
                                Settings.Secure.ANDROID_ID);

                        final String s3 = sh.getString("userphone", android_id);

                        Food food = (Food) dataSnapshot.getValue(Food.class);
                        demoRef = rootRef.child("Cart").child(s3).child(food.getName());

                        demoRef.child("Name").setValue(food.getName());
                        demoRef.child("Price").setValue(food.getPrice());
                        demoRef.child("Quantity").setValue(numberButton.getNumber());
                        demoRef.child("Image").setValue(food.getImage());
                        demoRef.child("Description").setValue(food.getDescription());
                        demoRef.child("Discount").setValue(food.getDiscount());
                    }

                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
            }
        });
    }
    @Override
    protected void onStart() {
        super.onStart();
        if(getIntent() != null)
            foodId = getIntent().getStringExtra("FoodId");
        if (foodId != null && !foodId.isEmpty()) {

            foods = database.getReference(getIntent().getStringExtra("name"));


            getDetailsFood(foodId);
        }
    }
}

fooddetails.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FoodDetails">

    <com.google.android.material.appbar.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">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsing"
            android:layout_width="match_parent"
            android:layout_height="350dp"
            android:fitsSystemWindows="true"
            app:contentScrim="#0e0d0e"
            app:expandedTitleTextAppearance="@android:color/transparent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/img_food"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:contentDescription="@null"
                android:scaleType="fitCenter"
                app:layout_collapseMode="parallax" />

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="parallax"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:title="Food Name" />


        </com.google.android.material.appbar.CollapsingToolbarLayout>

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardBackgroundColor="#FFFFFF"
            app:cardElevation="5dp"
            app:cardUseCompatPadding="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <androidx.core.widget.NestedScrollView
                    android:id="@+id/nestedScrollView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:clipToPadding="false">


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                    </LinearLayout>
                </androidx.core.widget.NestedScrollView>

                <TextView
                    android:id="@+id/food_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:padding="12dp"
                    android:text="Food Name"
                    android:textColor="@color/colorPrimary"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/food_price"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:text="1000"
                    android:textColor="@color/colorPrimary"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
                    android:id="@+id/number_button"
                    android:layout_width="100dp"
                    android:layout_height="35dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginBottom="18dp"
                    app:backGroundColor="@color/colorAccent"
                    app:finalNumber="15"
                    app:initialNumber="0"
                    app:textSize="6sp">


                </com.cepheuen.elegantnumberbutton.view.ElegantNumberButton>


            </LinearLayout>

        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardBackgroundColor="#FFFFFF"
            app:cardElevation="5dp"
            app:cardUseCompatPadding="true">

            <TextView
                android:id="@+id/food_description1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="12dp"
                android:lineSpacingMultiplier="1.5"
                android:padding="12dp"
                android:text="Description"
                android:textColor="@android:color/black"
                android:textSize="14sp" />

        </androidx.cardview.widget.CardView>


    </com.google.android.material.appbar.AppBarLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

点击时查看回收站

Intent i = new Intent(getActivity(), FoodDetails.class);
                            i.putExtra("FoodId", adapter1.getRef(position).getKey());
                            i.putExtra("name", "Food");
                            i.putExtra("food_name", model.getName());
                            startActivity(i);

注意食物是这个

【讨论】:

    猜你喜欢
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 2017-12-13
    • 2013-07-05
    相关资源
    最近更新 更多