In my project, I have a PagerAdapter to inflate CardViews that hold text objects created by the user.
On each CardView I have edit and delete buttons to access methods to do with the ArrayList holding the text objects.
I have coded an interface in my adapter to handle clicks made within the text object creation activity. However, my instatiateItem method in my adapter is now showing on the @Override annotation "Method does not override method from its superclass". When I remove the @Override annotation I get an error in my Logcat:
java.lang.UnsupportedOperationException: Required method instantiateItem was not overridden
How do I implement a click listener for buttons on a CardView in a PagerAdapter?
Adapter code:
public class CreateAdapter extends PagerAdapter {
private List<PdfPage> pagesList;
private LayoutInflater layoutInflater;
private Context context;
private onItemClickListener mListener;
public interface onItemClickListener{
//click method for edit button
void onEditClick(int position);
//click method for delete button
void onDeleteClick(int position);
}
public void setOnItemClickListener(onItemClickListener listener){
mListener = listener;
}
public CreateAdapter(List<PdfPage> pagesList, Context context) {
this.pagesList = pagesList;
this.context = context;
}
@Override
public int getCount() {
return pagesList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view.equals(object);
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, final int position, final onItemClickListener listener) {
layoutInflater = LayoutInflater.from(context);
View view = null;
if(pagesList.get(position).getPageType() == "Text") {
view = layoutInflater.inflate(R.layout.create_text_cardview, container, false);
TextView inputText = view.findViewById(R.id.inputTextView);
ImageButton editButton = view.findViewById(R.id.imageButtonEdit);
ImageButton deleteButton = view.findViewById(R.id.imageButtonDelete);
//if this works, see 12:30 of video for image getter
inputText.setText(pagesList.get(position).getPageText());
//edit click listener
editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null){
int position2 = pagesList.indexOf(pagesList.get(position));
listener.onEditClick(position2);
}
}
});
//delete click listener
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null){
int position3 = pagesList.indexOf(pagesList.get(position));
listener.onDeleteClick(position3);
}
}
});
container.addView(view, 0);
}
//put else statement here for image cardView
assert view != null;
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View)object);
}
Activity code:
public class CreateActivity extends AppCompatActivity {
ViewPager viewPager;
CreateAdapter createAdapter;
List<PdfPage> pdfPageList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
pdfPageList = new ArrayList<>();
createAdapter = new CreateAdapter(pdfPageList, this);
viewPager = findViewById(R.id.viewPager);
viewPager.setAdapter(createAdapter);
viewPager.setPadding(130, 0, 130, 0);
}
@Override
protected void onStart() {
super.onStart();
createAdapter = new CreateAdapter(pdfPageList, this);
viewPager = findViewById(R.id.viewPager);
viewPager.setAdapter(createAdapter);
viewPager.setPadding(130, 0, 130, 0);
createAdapter.setOnItemClickListener(new CreateAdapter.onItemClickListener() {
@Override
public void onEditClick(int position) {
}
@Override
public void onDeleteClick(int position) {
pdfPageList.remove(position);
}
});
}
Your instantiateItem
method does not override from it's superclass because you added an extra argument final onItemClickListener listener
.
As you are using the same clickListener for all entries and passing the item position as an argument, I suggest that you pass the onItemClickListener
in the adapter constructor, or just set it via
public void setOnItemClickListener(onItemClickListener listener){
mListener = listener;
}
Adapter code:
public class CreateAdapter extends PagerAdapter {
private List<PdfPage> pagesList;
private LayoutInflater layoutInflater;
private Context context;
private onItemClickListener mListener;
public interface onItemClickListener{
//click method for edit button
void onEditClick(int position);
//click method for delete button
void onDeleteClick(int position);
}
public void setOnItemClickListener(onItemClickListener listener){
mListener = listener;
}
public CreateAdapter(List<PdfPage> pagesList, Context context) {
this.pagesList = pagesList;
this.context = context;
}
@Override
public int getCount() {
return pagesList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view.equals(object);
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, final int position) {
layoutInflater = LayoutInflater.from(context);
View view = null;
if(pagesList.get(position).getPageType() == "Text") {
view = layoutInflater.inflate(R.layout.create_text_cardview, container, false);
TextView inputText = view.findViewById(R.id.inputTextView);
ImageButton editButton = view.findViewById(R.id.imageButtonEdit);
ImageButton deleteButton = view.findViewById(R.id.imageButtonDelete);
//if this works, see 12:30 of video for image getter
inputText.setText(pagesList.get(position).getPageText());
//edit click listener
editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mlistener != null){
int position2 = pagesList.indexOf(pagesList.get(position));
mlistener.onEditClick(position2);
}
}
});
//delete click listener
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mlistener != null){
int position3 = pagesList.indexOf(pagesList.get(position));
mlistener.onDeleteClick(position3);
}
}
});
container.addView(view, 0);
}
//put else statement here for image cardView
assert view != null;
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View)object);
}
And your Activity code could stay as it is.