What I'm trying to do is to have a recycler view with a button in it. When the layout that has the recycler is initialized it will only have the button and once is clicked an item will be added(the first item) and the button(or imageview, whatever it has a + on it) pushed to the right. I have the recyclerView and it's working(by working I mean that I have some photos that are being displayed in the right order), but I can't get the button to display. I found various links sort of similar to this, such as How to add fixed Button in RecyclerView Adapter?, but I can't figure it out.
Here is the Adapter:
public class SelectPhotoAdapter extends
RecyclerView.Adapter<SelectPhotoHolder> {// Recyclerview will extend to
// recyclerview adapter
private ArrayList<Data_Model> arrayList;
private Context context;
private boolean hasLoadButton = true;
private final int IMAGES = 0;
private final int LOAD_MORE = 1;
public SelectPhotoAdapter (Context context,
ArrayList<Data_Model> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
public boolean isHasLoadButton() {
return hasLoadButton;
}
public void setHasLoadButton(boolean hasLoadButton) {
this.hasLoadButton = hasLoadButton;
notifyDataSetChanged();
}
@Override
public int getItemCount() {
if (hasLoadButton) {
return arrayList.size() + 1;
} else {
return arrayList.size();
}
}
@Override
public int getItemViewType(int position) {
if (position < getItemCount()) {
return IMAGES;
} else {
return LOAD_MORE;
}
}
// @Override
// public int getItemCount() {
// return (null != arrayList ? arrayList.size() : 0);
//
// }
@Override
public void onBindViewHolder(SelectPhotoHolder holder, int position) {
SelectPhotoHolder mainHolder = holder;// holder
if(position >= getItemCount()) {
mainHolder.addPhoto.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//dostuff
}
});
} else {
final Data_Model model = arrayList.get(position);
Bitmap image = BitmapFactory.decodeResource(context.getResources(),
model.getImagePath());// This will convert drawbale image into
mainHolder.imageview.setImageBitmap(image);
}
}
@Override
public SelectPhotoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == IMAGES) {
return new SelectPhotoHolder((LayoutInflater.from(parent.getContext()).inflate(R.layout.selectphotolist, parent, false)),IMAGES);
} else if (viewType == LOAD_MORE) {
return new SelectPhotoHolder((LayoutInflater.from(parent.getContext()).inflate(R.layout.addphoto, parent, false)),LOAD_MORE);
} else {
return null;
}
}
}
If I use the getItemCount()
which is commented I get the photos, but no button and if I use the uncommented getItemCount()
which I took from the link provided it will crash. I also haven't figured out where to use isHasLoadButton()
or setHasLoadButton
methods. Could anyone point me in the right direction ?
If you need me to post anymore files let me know. Thank you.
The file which uses the recyclerView:
public class SelectPhotoDialogFragment extends DialogFragment {
private RecyclerView mRecyclerView;
private SelectPhotoAdapter adapter;
// this method create view for your Dialog
public static final Integer[] IMAGES= {R.drawable.first,R.drawable.second,R.drawable.third,R.drawable.picc};
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a window without the title
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//inflate layout with recycler view
View v = inflater.inflate(R.layout.select_photo, container, false);
mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
int recyclerHeight = mRecyclerView.getHeight();
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
ArrayList<Data_Model> arrayList = new ArrayList<>();
for (int i = 0; i < IMAGES.length; i++) {
arrayList.add(new Data_Model(IMAGES[i]));
}
adapter = new SelectPhotoAdapter(getActivity(), arrayList);
mRecyclerView.setAdapter(adapter);// set adapter on recyclerview
adapter.notifyDataSetChanged();// Notify the adapter
return v;
}
}
To explain again: The only thing that I'm seeing are the images(first,second,third,picc) and no button after, but they are displayed correctly. What I would like to happen is to have only the button as an item, and when I click on it to have a picture inserted before and it moving to the right(as you can see I have a horizontal orientation). This is what I've been trying to do with 2 ViewTypes, but I haven't figured it out.
The Holder:
public class SelectPhotoHolder extends RecyclerView.ViewHolder {
// View holder for gridview recycler view as we used in listview
private final int IMAGES = 0;
private final int LOAD_MORE = 1;
public ImageView imageview;
public Button addPhoto;
public SelectPhotoHolder(View view,int ViewType) {
super(view);
// Find all views ids
if(ViewType == IMAGES) {
this.imageview = (ImageView) view
.findViewById(R.id.selectPhoto);
}
else
this.addPhoto = (Button) view.findViewById(R.id.load_more);
}
}
new Holder output:
01-31 14:12:47.131 24072-24072/name.company.newapp E/test-exist: IMAGESfalse
01-31 14:12:47.141 24072-24072/name.company.newapp E/test-exist: IMAGESfalse
01-31 14:12:47.141 24072-24072/name.company.newapp E/test-exist: IMAGESfalse
01-31 14:12:47.151 24072-24072/name.company.newapp E/test-exist: IMAGESfalse
01-31 14:12:47.231 24072-24072/name.company.newapp E/test-exist: IMAGESfalse