赞
踩
implementation 'com.github.bumptech.glide:glide:4.12.0' //裁剪图片等等 implementation 'androidx.recyclerview:recyclerview:1.1.0' //recycleview依赖
- <androidx.recyclerview.widget.RecyclerView
- android:id="@+id/recyclerView"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- <?xml version="1.0" encoding="utf-8"?>
- <Button xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/btnAdd"
- android:layout_width="100dp"
- android:layout_height="120dp"
- android:layout_margin="4dp"
- android:layout_weight="1"
- android:text="Add"/>
- //点击添加按钮
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:padding="8dp">
-
- <ImageView
- android:id="@+id/imgView"
- android:layout_width="100dp"
- android:layout_height="100dp"
- />
-
- </LinearLayout>
- package com.example.myapplication;
-
- import android.content.Context;
- import android.content.Intent;
- import android.net.Uri;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.ImageView;
-
- import androidx.annotation.NonNull;
- import androidx.recyclerview.widget.RecyclerView;
-
- import com.bumptech.glide.Glide;
-
- import java.util.ArrayList;
-
- public class ImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
-
- private final int VIEW_TYPE_IMAGE = 0;
- private final int VIEW_TYPE_ADD_BUTTON = 1;
- private ArrayList<Uri> imageUris;
- private Context context;
- private LayoutInflater inflater;
- private OnAddButtonClickListener addButtonClickListener;
-
- public interface OnAddButtonClickListener {
- void onAddButtonClick();
- }
-
- public ImageAdapter(Context context, OnAddButtonClickListener listener) {
- this.context = context;
- this.imageUris = new ArrayList<>();
- this.inflater = LayoutInflater.from(context);
- this.addButtonClickListener = listener;
- }
-
-
- public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
- if (viewType == VIEW_TYPE_IMAGE) {
- View itemView = inflater.inflate(R.layout.iteam, parent, false);
- return new ImageViewHolder(itemView);
- } else {
- View itemView = inflater.inflate(R.layout.iteambutton, parent, false);
- Button addButton = itemView.findViewById(R.id.btnAdd);
- addButton.setOnClickListener(v -> addButtonClickListener.onAddButtonClick());
- return new AddButtonViewHolder(itemView);
- }
- }
-
- @Override
- public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
- if (getItemViewType(position) == VIEW_TYPE_IMAGE) {
- Uri uri = imageUris.get(position);
- ImageViewHolder imageHolder = (ImageViewHolder) holder;
- Glide.with(context)
- .load(uri)
- .centerCrop()
- .into( imageHolder.imageView);
-
- //点击图片 放大图片,跳转页面
- imageHolder.imageView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent=new Intent(context,emptyActivity.class);
- intent.putExtra("url",uri.toString());
- context.startActivity(intent);
- }
- });
- }
- }
-
- @Override
- public int getItemCount() {
- // Plus one for the add button
- return imageUris.size() + 1
- }
-
- @Override
- public int getItemViewType(int position) {
- return position == imageUris.size() ? VIEW_TYPE_ADD_BUTTON : VIEW_TYPE_IMAGE;
- }
-
- public void addImage(Uri uri) {
- imageUris.add(uri);
- notifyItemInserted(imageUris.size() - 1);
- }
-
- static class ImageViewHolder extends RecyclerView.ViewHolder {
- ImageView imageView;
-
- ImageViewHolder(View itemView) {
- super(itemView);
- imageView = itemView.findViewById(R.id.imgView);
- }
- }
-
- static class AddButtonViewHolder extends RecyclerView.ViewHolder {
- AddButtonViewHolder(View itemView) {
- super(itemView);
- }
- }
- }
-
- package com.example.myapplication;
-
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.MediaStore;
-
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.recyclerview.widget.GridLayoutManager;
- import androidx.recyclerview.widget.LinearLayoutManager;
- import androidx.recyclerview.widget.RecyclerView;
-
- import java.util.ArrayList;
-
- public class RecyCleActivity extends AppCompatActivity implements ImageAdapter.OnAddButtonClickListener{
- private static final int REQUEST_CODE_PICK_IMAGE = 1;
- private ImageAdapter imageAdapter;
- private RecyclerView recyclerView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_recy_cle);
-
- recyclerView = findViewById(R.id.recyclerView);
- recyclerView.setLayoutManager(new GridLayoutManager(this,3));
- imageAdapter = new ImageAdapter(this, this);
- recyclerView.setAdapter(imageAdapter);
- }
-
- @Override
- public void onAddButtonClick() {
- Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
- startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
- }
-
- protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == RESULT_OK) {
- if (data != null) {
- Uri selectedImageUri = data.getData();
- if (selectedImageUri != null) {
- imageAdapter.addImage(selectedImageUri);
- }
- }
- }
- }
- }
- <?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"
- android:orientation="vertical">
-
-
- <ImageView
- android:id="@+id/img"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"/>
-
- </RelativeLayout>
- package com.example.myapplication;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.net.Uri;
- import android.os.Bundle;
- import android.widget.ImageView;
-
- import com.bumptech.glide.Glide;
-
- import javax.microedition.khronos.opengles.GL;
-
- public class emptyActivity extends AppCompatActivity {
-
- private ImageView img;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_empty);
-
- img=findViewById(R.id.img);
- String imageUriString = getIntent().getStringExtra("url");
-
- Glide.with(emptyActivity.this)
- .load(imageUriString)
- .into(img);
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。