GridLayoutManager. How show selected item at the first grid?

48 Views Asked by At

My app is called MyGram, a school project. I want this app to function like instagram, everytime you select an image, it should show at the top.

My code is

package com.example.mygram;


import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mygram.utils.SpacingItemDecoder;



import java.util.ArrayList;
import java.util.Collections;

import de.hdodenhof.circleimageview.CircleImageView;

public class MainActivity extends AppCompatActivity {

    private static final int IMAGE_PICK_CODE=1000;
    private static final int PERMISSION_CODE=1001;

    RecyclerView recyclerView;
    TextView textView;
    Button pick, gallery;
    CircleImageView profpic;

    ArrayList <Uri> uri = new ArrayList<>();

    RecyclerAdapter adapter ;

    private static final int Read_Permission = 101;
    private static final int PICK_IMAGE = 1;


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

        profpic = findViewById(R.id.profile_image);
        gallery = findViewById(R.id.select);
        gallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                galleryActivityResultLauncher.launch(intent);
            }
        });

        textView = findViewById(R.id.totalPhotos);
        recyclerView = findViewById(R.id.recyclerView_Gallery_Images);
        pick = findViewById(R.id.pick);

        adapter = new RecyclerAdapter(uri);
        recyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this,3, LinearLayoutManager.VERTICAL, true));
        SpacingItemDecoder itemDecorator = new SpacingItemDecoder(10);
        recyclerView.addItemDecoration(itemDecorator);
        recyclerView.setAdapter(adapter);


        pick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, Read_Permission);

                    return;
                }

                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                if  (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
                    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                }
                //intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
            }
        });
    }

    private ActivityResultLauncher<Intent> galleryActivityResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    if (result.getResultCode() == Activity.RESULT_OK){
                        Intent data = result.getData();
                        Uri imageUri = data.getData();

                        profpic.setImageURI(imageUri);
                    }
                    else{
                        Toast.makeText(MainActivity.this, "Cancelled", Toast.LENGTH_SHORT).show();
                    }
                }
            }
    );



    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && null != data){
            if(data.getClipData()!=null){

                int countofImages = data.getClipData().getItemCount();
                for(int i=0; i<countofImages; i++){
                    Uri imageuri = data.getClipData().getItemAt(i).getUri();
                    uri.add(imageuri);
                }
                adapter.notifyDataSetChanged();
                textView.setText("");
            }else{
                Uri imageuri = data.getData();
                uri.add(imageuri);
            }
            adapter.notifyDataSetChanged();
            textView.setText("");
        }else{
            Toast.makeText(this, "You haven't pick any images", Toast.LENGTH_SHORT).show();
        }

    }
}

I want my selected image to appear in the first box but setstackfromend seems not to work. I have tried reversing the collection but it didnt do anything. Help me with this.

My app is an instagram inspired application, the selected images should show at the first grid at the top.

1

There are 1 best solutions below

0
lawrence On

you should call uri.add(0,imageuri); to make sure the selected image is in the first position.