RecyclerView什么都没显示

时间:2017-04-21 23:16:10

标签: java android android-recyclerview

我正在创建一个应用程序,理论上,它必须从我的内部存储器加载图像并在RecyclerView上显示它。我想我已经正确地遵循了所有步骤,经过许多不成功的尝试,我在这里问。所以我认为将数据传递给适配器的代码是错误的,但我想不出任何要验证的东西。任何帮助,观察,建议都非常感谢。这些是我的文件:

Photos.java(我获取数据的地方):

public class Photos {
static ArrayList<String> filenameListTemp=new ArrayList<>();
static ArrayList<File> photosList=new ArrayList<>();

public static ArrayList<String> getPhotos(File internal){
    File listFolers[]=internal.listFiles();
    if (listFolers!=null && listFolers.length>0){
        for (File file:listFolers) {
            if (file.isDirectory()){
                getPhotos(file);
            }
            else {
                if (file.getName().endsWith(".png")||file.getName().endsWith(".jpg")||file.getName().endsWith(".gif")||file.getName().endsWith(".bmp")){
                    String temp =file.getPath().substring(0,file.getPath().lastIndexOf('/'));

                    if (!filenameListTemp.contains(temp)){
                        filenameListTemp.add(temp); //This add names
                        photosList.add(file);// This add Files
                    }
                }
            }
        }
    }
        return filenameListTemp;
    }
}

RecyclerViewAdapter:

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

Context context;
ArrayList<String> photos=new ArrayList<>();
LayoutInflater layoutinflater;



public RecyclerViewAdapter(Context context, ArrayList<String> photos) {
    this.context=context;
    this.photos=photos;
    layoutinflater=LayoutInflater.from(context);
}



@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view=layoutinflater.inflate(R.layout.recyclerview_item_list,parent,false);
    RecyclerViewAdapter.ViewHolder holder=new RecyclerViewAdapter.ViewHolder(view);
    return holder;
}

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

        holder.image.setImageBitmap(BitmapFactory.decodeFile(photos.get(position)));

}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    ImageView image;

    public ViewHolder(View itemView) {
        super(itemView);
        image= (ImageView) itemView.findViewById(R.id.image);
    }
   }
  }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerViewAdapter adapter;
File expath= Environment.getExternalStorageDirectory();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView= (RecyclerView) findViewById(R.id.recyclerview);
    adapter=new RecyclerViewAdapter(this, Photos.getPhotos(expath));
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
   }
}

2 个答案:

答案 0 :(得分:2)

1。确保您已在AndroidManifest.xml中添加了读取外部存储空间的权限。

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

2。logs方法中添加一些getPhotos(),检查filenameListTempphotosList的大小,了解天气是否有效

答案 1 :(得分:0)

请确保您在适配器中传递的arraylist的大小大于0.如果大小为0,那么它将在适配器中显示任何内容

谢谢!