如何在Fragler的RecyclerView适配器中使用RecyclerView?

时间:2018-01-21 16:49:39

标签: android android-recyclerview android-cardview recycler-adapter layout-inflater

我的SavedActivity.java

import android.os.Environment;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.io.File;
import java.util.ArrayList;

public class SavedActivity extends Fragment {
private static final String Saved  = "/MyApp";
public SavedActivity() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View inflatedView = LayoutInflater.from(container.getContext()).inflate(R.layout.recycler_view_adapter_saved,container, false);
    RecyclerView RecyclerViewMediaList =   (RecyclerView)inflatedView.findViewById(R.id.recycler_View);
    RecyclerViewMediaList.setLayoutManager(new   LinearLayoutManager(getActivity()));
    //Error On the line below
    RecyclerViewAdapterSaved recyclerViewMediaAdapter = new RecyclerViewAdapterSaved(this.getListFiles(new File(Environment.getExternalStorageDirectory().toString()+ Saved)), SavedActivity.this));
    RecyclerViewMediaList.setAdapter(recyclerViewMediaAdapter);
    return inflatedView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

private ArrayList<File> getListFiles(File parentDir) {
    ArrayList<File> inFiles = new ArrayList<>();
    File[] files;
    files = parentDir.listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.getName().endsWith(".jpg"))
            {
                if (!inFiles.contains(file))
                    inFiles.add(file);
            }
        }
    }
    return inFiles;
}
}

这会导致错误:

RecyclerViewAdapterSaved recyclerViewMediaAdapter = new RecyclerViewAdapterSaved(this.getListFiles(new File(Environment.getExternalStorageDirectory().toString()+ Saved)),SavedActivity.this));

我的RecyclerViewAdapterSaved.java

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.io.File;
import java.util.ArrayList;

public class RecyclerViewAdapterSaved extends RecyclerView.Adapter<RecyclerViewAdapterSaved.FileHolder> {
private ArrayList<File> filesList;
private Activity activity;

RecyclerViewAdapterSaved(ArrayList<File> filesList, Activity activity){
    this.filesList = filesList;
    this.activity = activity;
}

@Override
public RecyclerViewAdapterSaved.FileHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View inflatedView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_adapter_saved, parent, false);
    return new FileHolder(inflatedView);
}

@Override
public void onBindViewHolder(final RecyclerViewAdapterSaved.FileHolder holder, final int position) {
    final File currentFile = filesList.get(position);
    showImages(currentFile,holder,position);


}

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

public static class FileHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    ImageView imageViewSaved;
     CardView cardviewImageViewSaved;

    public FileHolder(View itemView) {
        super(itemView);
        imageViewSaved = (ImageView) itemView.findViewById(R.id.imageView);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

    }
public void showImages(final File currentFile, final RecyclerViewAdapterSaved.FileHolder holder, final int position){
    holder.cardviewImageViewSaved.setVisibility(View.VISIBLE);
    Bitmap statusBitmap = BitmapFactory.decodeFile(currentFile.getAbsolutePath());
    holder.imageViewSaved.setImageBitmap(statusBitmap);


}
}

ppCompactActivity中使用它时,相同的代码正常工作,但在Fragment中使用时显示错误。

错误(鼠标悬停在线上):

  

RecyclerViewAdapterSaved(ArrayList),android:app.Activity in   RecyclerViewAdapterSaved无法应用

1 个答案:

答案 0 :(得分:1)

你在这一行中有错误

//Error On the line below
RecyclerViewAdapterSaved recyclerViewMediaAdapter = new RecyclerViewAdapterSaved(this.getListFiles(new File(Environment.getExternalStorageDirectory().toString()+ Saved)), SavedActivity.this));

注意:SavedActivity.this 此情况下的活动

所以正确的代码是

RecyclerViewAdapterSaved recyclerViewMediaAdapter = new RecyclerViewAdapterSaved(this.getListFiles(new File(Environment.getExternalStorageDirectory().toString()+ Saved)), getActivity()));

SavedActivity.this替换为getActivity()