根据月份和年份对多视图类型的RecyclerView进行排序

时间:2018-10-27 15:57:04

标签: android

我正在使用多视图类型RecyclerView,并且标题应显示月份和年份的名称,并且在我使用Glide从图库加载图像的项目中还要显示。首先,我希望在每个标题上显示月份和年份的名称,然后图像应仅属于该特定月份和年份。 我希望我的多视图看起来像这样 Screenshot

这是我的代码

GalleryAdapter.java

package com.jumptotv.adapter;


import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.CursorLoader;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.MimeTypeMap;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.jumptotv.GlideApp;
import com.jumptotv.R;
import com.jumptotv.model.HeaderModel;
import com.jumptotv.model.Movie;
import com.jumptotv.view.activity.FullScreenActivity;
import com.jumptotv.view.activity.MainActivity;
import com.jumptotv.view.activity.VideoActivity;
import com.jumptotv.view.activity.VideoPlayActivity;
import com.jumptotv.view.fragment.GalleryFragment;

import java.io.File;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

public class GalleryAdapter2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context context;
    private List<HeaderModel> moviesList;
    private ArrayList<String> imageData;
    final int HEADER = 0;
    final int ITEMS = 1;
    int n = 0;
    String sort[];
    String month[] = {"DAY", "TODAY", "SEP 1", "SEP 2", "SEP 3", "OCT 2018", "NOV 2018", "DEC 2018", "JAN 2017", "FEB 2017"};


    public GalleryAdapter2(ArrayList<String> imageData, Context context, List<HeaderModel> moviesList) {
        this.imageData = imageData;
        this.context = context;
        this.moviesList = moviesList;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == HEADER) {

            view = LayoutInflater.from(context).inflate(R.layout.header_type_layout, parent, false);
            return new Header2(view);

        } else if (viewType == ITEMS) {
            view = LayoutInflater.from(context).inflate(R.layout.grid_type_layout, parent, false);
            return new MyViewHolde(view);

        }
        throw new RuntimeException("No match found");
    }

    @Override
    public int getItemViewType(int position) {

        int viewType = ITEMS; //Default is 1
        if (position % 16 == 0) {
            viewType = HEADER;


        }

        //return position;
        //if zero, it will be a header view
        return viewType;

    }

    private String getItem(int position)


    {
        return imageData.get(position);
    }

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

        final int itemType = getItemViewType(position);
        switch (holder.getItemViewType()) {
            case 1:

                final String data = imageData.get(position);
                String data2 = GalleryAdapter2.getMimeType(data);
                if (data != null) {
                    if (data2.equals("jpg") || (data2.equals("png")) || (data2.equals("tif")) || (data2.equals("gif"))) {
                        ((MyViewHolde) holder).play.setVisibility(View.GONE);
                        GlideApp.with(context)
                                .load(data)
                                .placeholder(R.drawable.gallery_39)
                                .into(((MyViewHolde) holder).singleImageView);

                        ((MyViewHolde) holder).singleImageView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(context, FullScreenActivity.class);
                                intent.putExtra(FullScreenActivity.IMAGE_POSITION, position);
                                context.startActivity(intent);
                            }
                        });


                    } else if (data2.equals("mkv") || (data2.equals("flv")) || (data2.equals("mov")) || (data2.equals("mp4")) || (data2.equals("3gp")) || (data2.equals("wmv")) || (data2.equals("mpg") || (data2.equals("avc")) || (data2.equals("UNKNOWN")))) {
                        Glide.with(context).load(data).into(((MyViewHolde) holder).singleImageView);
                        ((MyViewHolde) holder).play.setVisibility(View.VISIBLE);
                        ((MyViewHolde) holder).play.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(context, VideoPlayActivity.class);
                                intent.putExtra("video_url", data);
                                intent.putExtra(VideoPlayActivity.VIDEO_POSITION, position);
                                context.startActivity(intent);

                            }
                        });
                    }
                } else {

                }
                break;
                case 0:

                    n++;
                    if(n==10)
                    {
                        n=6;
                        n++;
                    }


                    ((Header2) holder).textView.setText(month[n]);

                break;

            //Header2 header2=new Header2(View itemView);
            //  bindHeaderItem(,position);
            //((Header2) holder).textView.setText(month[n]);

                  /*if(position==0)
                  {
                      n=n+1;
                  }
                  */

        }
    }

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

    public boolean isHeader(int position) {
        return position == 0;
    }

    public static String getMimeType(String url) {

        Uri uri = Uri.parse(url);
        String extension = MimeTypeMap.getFileExtensionFromUrl(uri.getPath());
        return extension;
    }

    public class MyViewHolde extends RecyclerView.ViewHolder {
        ImageView singleImageView, play;

        public MyViewHolde(View itemView) {
            super(itemView);
            singleImageView = (ImageView) itemView.findViewById(R.id.gallery);
            play = (ImageView) itemView.findViewById(R.id.play_video2);
        }
    }

    public class Header2 extends RecyclerView.ViewHolder {
        TextView textView;

        public Header2(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.headerTitle);
           // textView.setText(month[n]);
        }

    }


    private void bindHeaderItem(Header2 holder, int position) {
        TextView title = (TextView) holder.textView.findViewById(R.id.headerTitle);
        title.setText(moviesList.get(position).getTitle());
    }





}

GalleryFragment

package com.jumptotv.view.fragment;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Looper;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.CursorLoader;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jumptotv.R;
import com.jumptotv.Utility;
import com.jumptotv.adapter.GalleryAdapter;
import com.jumptotv.adapter.GalleryAdapter2;
import com.jumptotv.model.HeaderModel;
import com.jumptotv.model.Model_Video;
import com.jumptotv.model.Movie;
import com.jumptotv.view.activity.FullScreenActivity;
import com.jumptotv.view.activity.VideoActivity;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;


public class GalleryFragment extends Fragment {
    public GalleryFragment() {
    }
    private Context context;
    private List<HeaderModel> movieList = new ArrayList<>();
    private RecyclerView recyclerView;
    ArrayList<String> imageData;
    GalleryAdapter2 adapter;
    ImageView gallery,play,back_button;
    String title;
    Button play_video;
    Uri uri;
    TextView header;
    Cursor cursor;
    int column_index_data=0, column_index_folder_name;
    ArrayList<String> listOfAllImages = new ArrayList<String>();
    String absolutePathOfImage = null;
    String month[] = {"TODAY", "SEP 1", "SEP 2","SEP 3","OCT 2018,NOV 2018,DEC 2018,JAN 2017,FEB 2017"};





    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.fragment_gallery, container, false);
        context = getActivity();
        HeaderModel headerModel =new HeaderModel("TODAY");
         movieList.add(headerModel);
         headerModel =new HeaderModel("Tomorrow");
         movieList.add(headerModel);

        headerModel =new HeaderModel("Tomorrow1");
        movieList.add(headerModel);
        headerModel =new HeaderModel("Tomorrow2");
        movieList.add(headerModel);
        headerModel =new HeaderModel("Tomorrow3");
        movieList.add(headerModel);
        headerModel =new HeaderModel("Tomorro4");
        movieList.add(headerModel);
        headerModel =new HeaderModel("Tomorrow5");
        movieList.add(headerModel);





        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerViewList);
        recyclerView.setHasFixedSize(true);
        back_button=(ImageButton)view.findViewById(R.id.back_arrow);
        back_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Fragment someFragment = new FileFragment();
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.main_frame, someFragment ); // give your fragment container id in first parameter
                transaction.addToBackStack(null);  // if written, this transaction will be added to backstack
                transaction.commit();




            }
        });
        header=(TextView)view.findViewById(R.id.see_all);
        final View view2 = inflater.inflate(R.layout.grid_type_layout, container, false);
        Bundle bundle = this.getArguments();
        if(bundle != null) {
             title = bundle.get("key").toString();
        }

    gallery=(ImageView)view2.findViewById(R.id.gallery);
        gallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(context, "Hello", Toast.LENGTH_SHORT).show();

            }
        });



        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return (position %16 == 0) ? 3 : 1;
            }
        });

        recyclerView.setLayoutManager(layoutManager);

        MyTask myTask = new MyTask();
        myTask.execute();

        return view;
    }

    private class MyTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {

            boolean flag = Utility.checkReadStoragePermission(getActivity(),GalleryFragment.this);
            if (flag) {
                imageData= getAllShownImagesPath(getActivity());
                Log.e("imageData: ", String.valueOf(imageData.size()));

            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            adapter = new GalleryAdapter2(imageData,getActivity(),movieList);
            recyclerView.setAdapter(adapter);

        }

    }

    private ArrayList<String> getAllShownImagesPath(Activity activity) {

        if(title.equals("All Photos"))
        {

        //  header.setText("All Photos");


            uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            String[] projection = {MediaStore.MediaColumns.DATA,
                    MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
            final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
            cursor = getActivity().getContentResolver().query(uri, projection, null, null, orderBy + " DESC");
            column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            column_index_folder_name = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
            while (cursor.moveToNext()) {
                absolutePathOfImage = cursor.getString(column_index_data);
                listOfAllImages.add(absolutePathOfImage);
            }

        }
        else if(title.equals("All Videos"))
        {


            getActivity().runOnUiThread(new Runnable() {

                @Override
                public void run() {

                    header.setText(title);


                    // Stuff that updates the UI

                }
            });




            //header.setText("All Videos");
            int column_id,thum;
            uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

            String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Video.Media.BUCKET_DISPLAY_NAME,MediaStore.Video.Media._ID,MediaStore.Video.Thumbnails.DATA};

            final String orderBy = MediaStore.Images.Media.DATE_ADDED;
            cursor = getActivity().getContentResolver().query(uri, projection, null, null, orderBy + " DESC");

            column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.BUCKET_DISPLAY_NAME);
            column_id = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
            thum = cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA);


            while (cursor.moveToNext()) {
                absolutePathOfImage = cursor.getString(column_index_data);
                Log.e("Column", absolutePathOfImage);
                Log.e("Folder", cursor.getString(column_index_folder_name));
                Log.e("column_id", cursor.getString(column_id));
                Log.e("thum", cursor.getString(thum));
                listOfAllImages.add(absolutePathOfImage);

            }

        }

        else if(title.equals("All Files")){





             getActivity().runOnUiThread(new Runnable() {
                 @Override
                 public void run() {
                   header.setText(title);
                     String[] projection = {
                             MediaStore.Files.FileColumns._ID,
                             MediaStore.Files.FileColumns.DATA,
                             MediaStore.Files.FileColumns.DATE_ADDED,
                             MediaStore.Files.FileColumns.MEDIA_TYPE,
                             MediaStore.Files.FileColumns.MIME_TYPE,
                             MediaStore.Files.FileColumns.TITLE
                     }; // used for query ContentResolver for mediafiles

                     String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                             + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
                             + " OR "
                             + MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                             + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;  // used to select images and videos from contentResolver

                     Uri queryUri = MediaStore.Files.getContentUri("external");

                     CursorLoader cursorLoader = new CursorLoader(getActivity(),queryUri, projection, selection, null, MediaStore.Files.FileColumns.DATE_ADDED + " DESC");
                      cursor = cursorLoader.loadInBackground();
                     column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                     while (cursor.moveToNext()) {
                         absolutePathOfImage = cursor.getString(column_index_data);
                         listOfAllImages.add(absolutePathOfImage);
                     }
                 }
             });

        }

        return listOfAllImages;

    }

}

0 个答案:

没有答案