There is a list where each item represents a three-button CardView. To display on the screen, use the Frament placed in the ViewPager. Buttons are displayed from one to three depending on the data obtained.
The problem is that the number of displayed buttons changes arbitrarily with each scrolling list. (For example: There are ten elements in the list. Three of them should display two buttons and the other three buttons each. And the displayed number of buttons does not match the extracted data, the data is extracted from List).
I hide unnecessary buttons like this:
if (holder.ussdCmdObj.getStatus().equals("no")) { holder.btnStatus.setVisibility(View.GONE); } How to make the number of buttons displayed correctly and not changed when scrolling through the list?
public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder> { private final List<UssdCmdObj> ussdCmdObjList; private final OnListFragmentInteractionListener mListener; public MyItemRecyclerViewAdapter(List<UssdCmdObj> items, OnListFragmentInteractionListener listener) { ussdCmdObjList = items; mListener = listener; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.fragment_item, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(final ViewHolder holder, final int position) { holder.ussdCmdObj = ussdCmdObjList.get(position); holder.description.setText(ussdCmdObjList.get(position).getDescription()); if (holder.ussdCmdObj.getStatus().equals("no")) { holder.btnStatus.setVisibility(View.GONE); } if (holder.ussdCmdObj.getCmd_on().equals("empty")) { holder.btnOn.setVisibility(View.GONE); } if (holder.ussdCmdObj.getCmd_off().equals("empty")) { holder.btnOff.setVisibility(View.GONE); } holder.btnStatus.setOnClickListener(new View.OnClickListener() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onClick(View v) { String encodedHash = Uri.encode("#"); String ussd = holder.ussdCmdObj.getStatus() + encodedHash; Toast.makeText(v.getContext(), "=== " + holder.ussdCmdObj.getStatus(), Toast.LENGTH_SHORT).show(); v.getContext().startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussd))); } }); holder.btnOn.setOnClickListener(new View.OnClickListener() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onClick(View v) { String encodedHash = Uri.encode("#"); String ussd = holder.ussdCmdObj.getCmd_on() + encodedHash; Toast.makeText(v.getContext(), "=== " + holder.ussdCmdObj.getCmd_on(), Toast.LENGTH_SHORT).show(); v.getContext().startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussd))); } }); holder.btnOff.setOnClickListener(new View.OnClickListener() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onClick(View v) { String encodedHash = Uri.encode("#"); String ussd = holder.ussdCmdObj.getCmd_off() + encodedHash; Toast.makeText(v.getContext(), "=== " + holder.ussdCmdObj.getCmd_off(), Toast.LENGTH_SHORT).show(); v.getContext().startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussd))); } }); holder.mView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (null != mListener) { // Notify the active callbacks interface (the activity, if the // fragment is attached to one) that an item has been selected. mListener.onListFragmentInteraction(holder.ussdCmdObj); } } }); } @Override public int getItemCount() { return ussdCmdObjList.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public final View mView; public final TextView description; public final Button btnStatus; public final Button btnOn; public final Button btnOff; public UssdCmdObj ussdCmdObj; public ViewHolder(View view) { super(view); mView = view; description = (TextView) view.findViewById(R.id.tv_Description); btnStatus = (Button) view.findViewById(R.id.button_status); btnOn = (Button) view.findViewById(R.id.button_on); btnOff = (Button) view.findViewById(R.id.button_off); } } }