将RecyclerView显示为片段

时间:2018-12-25 20:31:48

标签: android android-recyclerview

我有方法:

private void initImageBitmaps() {
        mImageUrls.add("imageUrl");
        mNames.add("abcd");
        initRecyclerView();
    }

    private void initRecyclerView() {
        Log.d(TAG, "initRecyclerView: init recyclerview.");
        RecyclerView recyclerView = findViewById(R.id.recyclerv_view);
        RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, mNames, mImageUrls);
        recyclerView.setAdapter(adapter);
        //recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
    }

将图片添加到我的recyclerview。 如果它处于活动状态,我将在OnCreate中调用initImageBitmaps()。不幸的是,我必须在fragment中使用它,问题是:如何对扩展Fragment的类实现这些方法?当然,所有内容都将以xml中的recycler_view id显示在RecyclerView中。

编辑: 我的适配器:

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

    private static final String TAG = "RecyclerViewAdapter";

    private ArrayList<String> mImageNames = new ArrayList<>();
    private ArrayList<String> mImages = new ArrayList<>();
    private Context mContext;

    public RecyclerViewAdapter(WallpapersFragment context, ArrayList<String> imageNames, ArrayList<String> images ) {
        mImageNames = imageNames;
        mImages = images;
        mContext = context;
        //Rquired is android.content.context and found is WallpapersFragment from my project
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        Log.d(TAG, "onBindViewHolder: called.");

        Glide.with(mContext)
                .asBitmap()
                .load(mImages.get(position))
                .into(holder.image);

        holder.imageName.setText(mImageNames.get(position));

        holder.parentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, GalleryActivity.class);
                intent.putExtra("image_url", mImages.get(position));
                intent.putExtra("image_name", mImageNames.get(position));
                mContext.startActivity(intent);
            }
        });
    }

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


    public class ViewHolder extends RecyclerView.ViewHolder{

        ImageView image;
        TextView imageName;
        RelativeLayout parentLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.image);
            imageName = itemView.findViewById(R.id.image_name);
            parentLayout = itemView.findViewById(R.id.parent_layout);
        }
    }
}

仅当我在适配器中注释mContext = context;时才编译。那么错误是: java.lang.NullPointerException:您无法在尚未附加的View或其中getActivity()返回null的Fragment上开始加载(通常在Fragment附加之前或销毁Fragment之后调用getActivity()时发生)。 / p>

4 个答案:

答案 0 :(得分:0)

在Fragment类中有一个onCreate()方法,但是该方法不同于活动的onCreate()。

public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.example_fragment, container, false);
}

    @Override
public void onViewCreated (View view, Bundle savedInstanceState) {
            initRecyclerView();
    }

}

在此处了解有关片段的更多信息:Fragments

答案 1 :(得分:0)

initRecyclerView(view);

然后:

 recyclerView = view.findViewById(R.id.recyclerv_view);

答案 2 :(得分:0)

尝试一下...

 public class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
  View view=   inflater.inflate(R.layout.example_fragment, container, false);
    initImageBitmaps(view);
  return view;
}


private void initImageBitmaps(View view) {
    mImageUrls.add("imageUrl");
    mNames.add("abcd");
    initRecyclerView(View view);
}

 private void initRecyclerView(View view) {
    Log.d(TAG, "initRecyclerView: init recyclerview.");
    RecyclerView recyclerView = view.findViewById(R.id.recyclerv_view); // init recyclerview
      GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);// declare layoutmanager
    recyclerView.setLayoutManager(layoutManager); // set layoutmanager
    RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), mNames, mImageUrls); // init adapter
    recyclerView.setAdapter(adapter); // set adapter in recyclerveiw
}

编辑  将 WallpapersFragment 替换为适配器中的 Context

 public RecyclerViewAdapter(Context context, ArrayList<String> imageNames, ArrayList<String> images ) {
        mImageNames = imageNames;
        mImages = images;
        mContext = context;
        //Rquired is android.content.context and found is WallpapersFragment from my project
    }

答案 3 :(得分:0)

您可以在以下生命周期回调中初始化回收者视图:

onViewCreated()

,也可以在此处填充数据方法,例如

  

initRecyclerView()

     

initImageBitmaps()

对于数据刷新,您可以在适配器上调用notifyDatasetChanged()。