从自定义视频库加载图像

时间:2017-08-17 10:18:15

标签: android android-layout android-recyclerview video-gallery

我正在制作一个页面,其中有一个按钮,可以将您带到我创建的视频库。从中选择一些视频(缩略图),然后点击箭头返回到我点击按钮的同一位置,进入包含所选视频的自定义图库。

基本上地图是这样的:

按钮(来自AddFragment) - > VideoGallery->选择视频 - > AddFragment包含在回收站视图中可见的选定视频

现在,当我按下按钮时,我来到我的视频库,但是当我从画廊中选择一些视频并返回到AddFragment时,它什么都没显示,出现了一个错误 E / RecyclerView:没有连接适配器;跳过布局

现在我使用Bundle提取已发送的数据并在我的AddFragment中填充它。

我正在给你代码:

1。 AddFragment.java

public class AddFragment extends Fragment {

private ImageButton nextActivity;
private RecyclerView recyclerView;
private ProgressDialog videoProgressDialog;
ArrayList<File> checkedList = new ArrayList<>();
ImageAdapter imageAdapter;
Button button;

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) {
            startActivity(new Intent(getContext(),VideoGalleryActivity.class));
        }
    });

    return view;
}

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

    private LayoutInflater mInflater;

    private Bitmap bitmap;

    private ArrayList<File> fileName;

    public ImageAdapter(ArrayList<File> checkedList) {

        fileName = checkedList;
    }

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

        Log.e("ADAPTER SETTING","DOING");
        //getting the passed value from videogallery
        Bundle bundle = new Bundle();
        ArrayList<String> getValue = bundle.getStringArrayList("sendData");
        Log.e("RECEIVED_DATA======",getValue.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);
        recyclerView.setAdapter(imageAdapter);
        recyclerView.setVisibility(View.VISIBLE);

        View items = mInflater.from(parent.getContext()).inflate(R.layout.custom_added_video,
                parent,false);
        items.setLayoutParams(new AbsListView.LayoutParams(215,215));
        return new ViewHolder(items);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

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

        if(fileName != null){
            bitmap = ThumbnailUtils.createVideoThumbnail(fileName.get(position).toString(),1);
            holder.imageView.setImageBitmap(bitmap);
        }
    }

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

class ViewHolder extends RecyclerView.ViewHolder {
    public ImageView imageView;
    //public ImageButton imageButton;

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

        //imageButton = (ImageButton) itemView.findViewById(R.id.addVideos);
        imageView = (ImageView) itemView.findViewById(R.id.galleryImageView);
    }
} }

2。 fragment_add.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorWhite"
tools:context="in.pinelane.myhovi.AddFragment">

<!-- TODO: Update blank fragment layout -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/linearLayout"
    android:padding="19dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17dp"
        android:textStyle="bold"
        android:text="Choose Videos"
        android:textColor="@color/colorBackground"/>

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/gotoButton"
        android:layout_width="25sp"
        android:layout_height="25sp"
        android:background="#00ffffff"
        android:src="@mipmap/ic_arrow_forward_black_24dp"/>
</LinearLayout>

<Button
    android:id="@+id/buttonToGallery"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Gallery"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/grid_add_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" />

夸大此布局 的 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">

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

<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"/>

任何想法都会有很大的帮助!感谢

我已经尝试在onCreate中定义下面的代码,但是我得到了一个N​​ullPointerException,所以我在onCreateViewHolder中定义了相同但没有结果只是没有附加适配器;跳过布局

Log.e("ADAPTER SETTING","DOING");
        //getting the passed value from videogallery
        Bundle bundle = new Bundle();
        ArrayList<String> getValue = bundle.getStringArrayList("sendData");
        Log.e("RECEIVED_DATA======",getValue.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);
        recyclerView.setAdapter(imageAdapter);
        recyclerView.setVisibility(View.VISIBLE);

EDITS

我在我的代码和用户 onActivityResult startActivityForResult 中对此片段进行了一些更改,但仍然没有结果。我的AddFragment仍为空白且未显示任何结果

已编辑的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);
        }
    });

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

    return view;
}

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

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

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

            }
    }
}

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

    private LayoutInflater mInflater;

    private Bitmap bitmap;

    private ArrayList<File> fileName;

    public ImageAdapter(ArrayList<File> checkedList) {

        fileName = checkedList;
    }

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

        View items = mInflater.from(parent.getContext()).inflate(R.layout.custom_added_video,
                parent,false);
        items.setLayoutParams(new AbsListView.LayoutParams(215,215));
        return new ViewHolder(items);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

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

        if(fileName != null){
            bitmap = ThumbnailUtils.createVideoThumbnail(fileName.get(position).toString(),1);
            holder.imageView.setImageBitmap(bitmap);
        }
    }

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

class ViewHolder extends RecyclerView.ViewHolder {
    public ImageView imageView;
    //public ImageButton imageButton;

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

        //imageButton = (ImageButton) itemView.findViewById(R.id.addVideos);
        imageView = (ImageView) itemView.findViewById(R.id.galleryImageView);
    }
} }

1 个答案:

答案 0 :(得分:0)

移动此代码:

    //setting the adapter
    imageAdapter = new ImageAdapter(checkedList);
    recyclerView.setAdapter(imageAdapter);
    recyclerView.setVisibility(View.VISIBLE);

到您的createView()方法。

createView你正在给布局膨胀,你发现了RecyclerView,但你没有为它设置适配器。 RecyclerView没有要显示的任何数据,因此会从布局中跳过这些数据以提升效果。您还应该设置LayoutManager。如果您需要一个简单的列表,则应使用LinearLayoutManager

createViewHolder()的每个可见元素调用RecyclerView方法一次。它不是执行RecyclerView初始设置的正确位置。