-1

I'm using a Fragment which is a gallery which shows you the thumbnails of the videos selected from my custom video gallery. I've used a button in the gallery which helps you to move from the fragment to the activity you select some videos and then come back to the same fragment with the data populated in the grid view.

Problem Statement : I've followed this link Using two viewHolder in the same Adapter and implementing new thing that is, In the Addfragment I've one Imagebutton which is there at position 0 when nothing is shown up as soon as you hit the button you go upto the next activity, when some videos gets selected you come to the AddFragment again and now the that same imageButton changes its position.

For the above I've used two viewHolder but due to less knowledge about using it i'm stuck how to use it. Please guide for the same so that I'd achieve what i'm willing to achieve.

1.AddFragment.java

public class AddFragment extends Fragment {

private ImageButton nextActivity;
private RecyclerView recyclerView;
ArrayList<File> checkedList = new ArrayList<>();
ImageAdapter imageAdapter;
Button button;
private static final int CustomGallerySelectId = 1;//Set Intent Id

public AddFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_add, container, false);

    nextActivity = (ImageButton) view.findViewById(R.id.gotoButton);

    recyclerView = (RecyclerView) view.findViewById(R.id.grid_add_view);

    button = (Button) view.findViewById(R.id.buttonToGallery);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivityForResult(new Intent(getContext(),VideoGalleryActivity.class),CustomGallerySelectId);
        }
    });

    return view;
}

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

    Log.e("IT IS WORKING","YES");

    switch(requestCode){
        case CustomGallerySelectId :
            if(resultCode == RESULT_OK){
                Log.e("ADAPTER SETTING","DOING");
                //getting the passed value from videogallery
                ArrayList<String> getValue = data.getExtras().getStringArrayList("sendData");
                Log.e("RECEIVED_DATA======",data.getExtras().getSerializable("sendData").toString());

                //adding the files to the list
                for(String pathName : getValue) {
                    File filePath = new File(pathName);
                    checkedList.add(filePath);
                }

                //setting the adapter
                imageAdapter = new ImageAdapter(checkedList);
                GridLayoutManager videoGrid = new GridLayoutManager(getContext(),3);
                recyclerView.setLayoutManager(videoGrid);
                recyclerView.setAdapter(imageAdapter);
                recyclerView.setVisibility(View.VISIBLE);

            }
    }
}

//making adapter for RecyclerView which loads the desired files
class ImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Bitmap bitmap;

    private ArrayList<File> fileName;

    public ImageAdapter(ArrayList<File> checkedList) {

        fileName = checkedList;
    }

    class ViewHolderGalleryImage extends RecyclerView.ViewHolder {
        public ImageView imageView;


        public ViewHolderGalleryImage(View itemView) {
            super(itemView);

            imageView = (ImageView) itemView.findViewById(R.id.galleryImageView);
        }
    }

    class ViewHolderImageButton extends RecyclerView.ViewHolder{

        public ImageButton imageButton;

        public ViewHolderImageButton(View itemView) {
            super(itemView);

            imageButton = (ImageButton) itemView.findViewById(R.id.gotoGalleryButton);

        }
    }

    @Override
    public int getItemViewType(int position) {

        if(fileName !=null){

        }
        return super.getItemViewType(position);

    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        switch(viewType){
            case 0 : View galleryView = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_added_video,
                    parent,false);
                galleryView.setLayoutParams(new AbsListView.LayoutParams(215,215));
                return new ViewHolderGalleryImage(galleryView);

            case 1 : View imageButtonView = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_button_layout,
                    parent,false);
                imageButtonView.setLayoutParams(new AbsListView.LayoutParams(215,215));
                return new ViewHolderImageButton(imageButtonView);
        }

        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        switch (holder.getItemViewType()){
            case 0: if(fileName != null){
            bitmap = ThumbnailUtils.createVideoThumbnail(fileName.get(position).toString(),1);

        }
        }

    }

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

Layout 1 which inflates for deploying the images from VideoGalery into AddFragment custom_added_video.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_marginEnd="3dp"
android:layout_marginStart="3dp">

<ImageView
    android:id="@+id/galleryImageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    android:layout_margin="3dp"
    android:layout_centerInParent="true"/>

Layout which consists of Image Button custom_button_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_marginEnd="3dp"
android:layout_marginStart="3dp">

<ImageButton
    android:id="@+id/gotoGalleryButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/edittext_border"
    android:layout_margin="3dp"
    android:src="@mipmap/ic_add_black_24dp"
    android:layout_centerInParent="true"/>

I'm confused whether what to imlpement in getItemViewType() for achieving the result.

Adil Saiyad
  • 1,582
  • 2
  • 17
  • 34
Alok
  • 8,452
  • 13
  • 55
  • 93

1 Answers1

0

In the getItemViewType(), you have to check your data using the position and return different Integer values. One int value for one layout. Looks like you are using 0 and 1 for galleryView and imageButtonView respectively. And you have ArrayList<File> as the RecyclerView items. That List should have necessary data to decide whether to use gallery view or image button view.

Something like this:

public int getItemViewType(int position) {
    if (fileName.size() == posiiton) {
        return 1;
    } else {
        return 0;
    }
}

You will receive that viewType int value in onCreateViewHolder callback, where using that you can inflate different layouts.

Bob
  • 13,447
  • 7
  • 35
  • 45
  • I don't have something like isGalleryView() to correlate to my View nor it is showing me any suggestion for that! – Alok Aug 17 '17 at 13:40
  • You have to implement something like this in your model object. You should have some data to decide which view you have to use for the given position. It doesn't necessarily have to be inside that List. You have to decide which View you are going to inflate for the given position and return one integer for a layout. – Bob Aug 17 '17 at 13:41
  • I've to append the position with the file if the file is shown otherwise the imagebuttonView has to be in 0 position. For this I've used **if(fileName.get(position).isFile()) then return 0+1 else 1** is this correct could you please help me with this because it is quite complex for me. Thanks – Alok Aug 17 '17 at 13:45
  • where do you want to show the button if there are images? – Bob Aug 17 '17 at 13:49
  • at the last position it just appends with the ImageView if there is no images then the button will at the left at position 0 and if suppose two files have been added then the button must be there at pos = 2(0 = image1,1=image2 and 2 = imagebutton) – Alok Aug 17 '17 at 13:51
  • I have updated the method in the answer. Use like that. – Bob Aug 17 '17 at 13:54
  • I've implemented it but all I'm gettting is a blank page. No imagebutton – Alok Aug 17 '17 at 14:00
  • Inside `getItemCount` you have to return `fileName.size() + 1` – Bob Aug 17 '17 at 15:32
  • what should I do in nBondViewHolder() then, the code which I had given is correct as per your knowledge, will it create thumbnails of the videos? – Alok Aug 18 '17 at 06:59
  • Watch: https://www.youtube.com/watch?v=LqBlYJTfLP4 https://academy.realm.io/posts/360andev-yigit-boyar-pro-recyclerview-android-ui-java/ – Bob Aug 18 '17 at 07:07
  • I've done that, thank you so much for support. Your answer needs an upvote for your support and for your answer as well. Thanks – Alok Aug 18 '17 at 08:26