在我的Android应用程序中,我有一个片段打开一个dialogfragment,它本身包含一个recyclerview。我以前有一个listview,但需要水平滚动功能(请注意listview工作)。 getCount()返回0,并且从未调用onBindViewHolder。因此,当我调用适配器时,我在阶段出现错误,但我不能为我的生活弄清楚它是什么。在此先感谢您的帮助!!!
来自dialogfragment的片段:
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater li = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.feed_dialog, null);
LinearLayoutManager layoutManager
= new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
imageId = new ArrayList<Integer>();
imageId.add(R.drawable.sample_bag);
imageId.add(R.drawable.sample_bag);
RecyclerView listview = (RecyclerView) view.findViewById(R.id.feed_dialog_listview);
listview.setLayoutManager(layoutManager);
FeedImageDialogFragmentList adapter = new FeedImageDialogFragmentList(getActivity(), imageId);
listview.setAdapter(adapter);
Dialog dialog = new Dialog(getActivity());
dialog.setContentView(view);
dialog.show();
}
我的适配器类:
public class FeedImageDialogFragmentList extends
RecyclerView.Adapter<FeedImageDialogFragmentList.ViewHolder>{
private final Activity context;
private final ArrayList<Integer> imageId;
public FeedImageDialogFragmentList(Activity context, ArrayList<Integer> imageId) {
this.context = context;
this.imageId = imageId;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.list_feed_dialog, null, false);
return new FeedImageDialogFragmentList.ViewHolder(v);
}
@Override
public void onBindViewHolder(FeedImageDialogFragmentList.ViewHolder holder, final int position) {
Integer item = imageId.get(position);
holder.image.setImageBitmap(null);
holder.image.setImageResource(item);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView image;
public ViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.feed_dialog_image);
}
}
@Override
public int getItemCount() {
return imageId.size();
}
}