java—动态地希望从其他片段获取图像

vc6uscn9  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(205)

因此,基本上在这个用户可以动态添加图像和图像上传到firebase也。此外,在这个有2个片段,所以在mysnapfragment用户可以添加图像,也可以看到图像。但是当点击指定的按钮时,所有上传的图片也应该显示在一个片段中。我怎么能做到这一点?任何帮助都是非常值得赞赏的。所以基本上我想更新的图像显示时,在snapfragment按钮被点击。
这是我的snapfragmen.xml代码

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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=".aMySnapFragment"
        android:background="@drawable/gradient">

        <!-- TODO: Update blank fragment layout -->
        <TextView
            android:layout_width="341dp"
            android:layout_height="38dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="38dp"
            android:layout_marginBottom="544dp"
            android:text="   CALLIGRAPHY DOODLE SNAPS"
            android:textColor="@color/black"
            android:textSize="20sp" />

        <Button
            android:id="@+id/user_btn"
            android:layout_width="338dp"
            android:layout_height="88dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginHorizontal="20dp"
            android:layout_marginEnd="13dp"
            android:layout_marginBottom="184dp"
            android:background="@color/LightBlue"
            android:text="SHOW ALL CALLIGRAPHY SNAPS"
            android:textColor="@color/black"
            android:textSize="25dp" />

    </RelativeLayout>

这是我的mysnapfragment.xml文件

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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=".MySnapFragment"
        android:background="@drawable/gradient">

        <!-- TODO: Update blank fragment layout -->
        <TextView
            android:layout_width="341dp"
            android:layout_height="38dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="38dp"
            android:layout_marginBottom="544dp"
            android:text="   CALLIGRAPHY DOODLE SNAPS"
            android:textColor="@color/black"
            android:textSize="20sp" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="245dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="2dp"
            android:layout_marginBottom="267dp"
            android:src="@drawable/ic_baseline_add_photo_alternate_24" />

        <Button
            android:id="@+id/upload_btn"
            android:layout_width="304dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginHorizontal="20dp"
            android:layout_marginEnd="23dp"
            android:layout_marginBottom="142dp"
            android:background="@color/LightBlue"
            android:text="UPLOAD"
            android:textColor="@color/black"
            android:textSize="20dp" />

        <Button
            android:id="@+id/showall_btn"
            android:layout_width="304dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginHorizontal="20dp"
            android:layout_marginEnd="23dp"
            android:layout_marginBottom="53dp"
            android:background="@color/purple_200"
            android:text="SHOW ALL"
            android:textColor="@color/black"
            android:textSize="20dp" />

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:visibility="invisible"
            android:layout_marginEnd="153dp"
            android:layout_marginBottom="211dp" />

    </RelativeLayout>

这是我的myadapter.java文件

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

        private ArrayList<Model> mList;
        private Context context;

        public MyAdapter(Context context , ArrayList<Model> mList){

            this.context = context;
            this.mList = mList;
        }

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(context).inflate(R.layout.item , parent ,false);
            return new MyViewHolder(v);
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            Glide.with(context).load(mList.get(position).getImageUrl()).into(holder.imageView);
        }

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

        public static class MyViewHolder extends RecyclerView.ViewHolder{

            ImageView imageView;
            public MyViewHolder(@NonNull View itemView) {
                super(itemView);

                imageView = itemView.findViewById(R.id.m_image);
            }
        }
    }

这是我的showactivity.java文件

public class ShowActivity extends AppCompatActivity {

        private RecyclerView recyclerView;
        private ArrayList<Model> list;

        private MyAdapter adapter;

        private DatabaseReference root = FirebaseDatabase.getInstance().getReference("Image");

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_show);

            recyclerView = findViewById(R.id.recyclerview);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            list = new ArrayList<>();
            adapter = new MyAdapter(this , list);
            recyclerView.setAdapter(adapter);

            root.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    for (DataSnapshot dataSnapshot : snapshot.getChildren()){
                        Model model = dataSnapshot.getValue(Model.class);
                        list.add(model);
                    }
                    adapter.notifyDataSetChanged();
                }

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

                }
            });
        }
    }

this is my item.xml code    

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.cardview.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_margin="20dp"
        android:elevation="10dp">

        <ImageView
            android:id="@+id/m_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            />

    </androidx.cardview.widget.CardView>

暂无答案!

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

相关问题