Open ashishoc opened 8 years ago
@ashishoc Please post the adapter code you are using, if possible. Generally, adding an entry to the adapters data should always be accompanied by a call to notifyItemInserted(int position)
, but I'm not sure whether that solves your case.
Activity
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.contact_main); ButterKnife.bind(this); this.setMaterialMenu(R.id.toolbar); this.disabledTitle(); pref=new Pref(this); ArrayList<ResPartner> dataSet=getData(); face=Typeface.createFromAsset(getAssets(),"Spinnaker-Regular.ttf"); //working when I'm passing not empty list ContactAdapter adapter=new ContactAdapter(this,recyclerView,dataSet,face); recyclerView.setAdapter(adapter); //no after http resposne need to add more items adapter.mDataset.add(getItemt()); //IndexOutOfBound exception }
Adapter
public class ContactAdapter extends SwipeAdapter implements View.OnClickListener { ArrayList<ResPartner> mDataset; int[] colorsRnd = new int[]{ R.color.color_red, R.color.color_pink, R.color.color_purple, R.color.color_deep_purple, R.color.color_indigo, R.color.color_blue, R.color.color_light_blue, R.color.color_cyan, R.color.color_teal, R.color.color_green, R.color.color_light_green, R.color.color_lime, R.color.color_yellow, R.color.color_amber, R.color.color_orange, R.color.color_deep_orange, R.color.color_brown, R.color.color_grey, R.color.color_blue_grey}; private Activity mContext; private RecyclerView mRecyclerView; private Typeface face; public ContactAdapter(Activity context, RecyclerView recyclerView, ArrayList<ResPartner> mDataset, Typeface face) { mContext = context; mRecyclerView = recyclerView; this.mDataset = mDataset; this.face = face; recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(context)); } public class ContactViewHolder extends RecyclerView.ViewHolder { LinearLayout contentView; ImageView avatarView; TextView textView; TextView description; public ContactViewHolder(View view) { super(view); contentView = (LinearLayout) view.findViewById(R.id.contentView); avatarView = (ImageView) view.findViewById(R.id.avatarView); textView = (TextView) view.findViewById(R.id.textView); description = (TextView) view.findViewById(R.id.textView_other); contentView.setOnClickListener(ContactAdapter.this); } } @Override public RecyclerView.ViewHolder onCreateSwipeViewHolder(ViewGroup parent, int i) { View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.contact_child, parent, true); return new ContactViewHolder(v); } private int getColor() { return colorsRnd[new Random().nextInt(colorsRnd.length)]; } @Override public void onBindSwipeViewHolder(RecyclerView.ViewHolder swipeViewHolder, int i) { ContactViewHolder sampleViewHolder = (ContactViewHolder) swipeViewHolder; ResPartner partner = mDataset.get(i); TextDrawable drawable = TextDrawable.builder().buildRound(String.valueOf(partner.name.charAt(0)), getColor()); sampleViewHolder.avatarView.setImageDrawable(drawable); String fStr = partner.function.equals("false") ? (partner.phone.equals("false") ? (partner.mobile.equals("false") ? (partner.street.equals("false") ? (partner.customer ? "Customer" : "Individual") : partner.street) : partner.mobile) : partner.phone) : partner.function; String descriptionText = "<b> " + fStr + " </b> " + "<br />" + (partner.email.equals("false") ? "email not available" : partner.email); sampleViewHolder.textView.setText(partner.name); sampleViewHolder.textView.setTypeface(face); sampleViewHolder.description.setTypeface(face); sampleViewHolder.description.setText(Html.fromHtml(descriptionText)); } @Override public SwipeConfiguration onCreateSwipeConfiguration(Context context, int position) { return new SwipeConfiguration.Builder(context) .setLeftBackgroundColorResource(R.color.color_delete) .setRightBackgroundColorResource(R.color.color_mark) .setDrawableResource(R.drawable.ic_action_add_contact) .setRightDrawableResource(R.drawable.ic_communication_call) .setLeftUndoable(true) .setLeftUndoDescription(R.string.add_contact) .setDescriptionTextColorResource(android.R.color.white) .setLeftSwipeBehaviour(SwipeConfiguration.SwipeBehaviour.NORMAL_SWIPE) .setRightSwipeBehaviour(SwipeConfiguration.SwipeBehaviour.RESTRICTED_SWIPE) .build(); } @Override public void onSwipe(int position, int direction) { if (direction == SWIPE_LEFT) { mDataset.remove(position); notifyItemRemoved(position); Toast toast = Toast.makeText(mContext, "Deleted item at position " + position, Toast.LENGTH_SHORT); toast.show(); } else { callIntent( position); } } private void callIntent(int position){ String phone = mDataset.get(position).phone; if (phone.equals("false")) { Snackbar.make(mContext.findViewById(R.id.toolbar), "Oops! "+mDataset.get(position).name+" not shared any phone or mobile contact yet.", Snackbar.LENGTH_LONG) .show(); } else { Intent intent = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null)); mContext.startActivity(intent); } } @Override public void onClick(View view) { // We need to get the parent of the parent to actually have the proper view int position = mRecyclerView.getChildAdapterPosition((View) view.getParent().getParent()); Toast toast = Toast.makeText(mContext, "Clicked item at position " + position, Toast.LENGTH_SHORT); toast.show(); } @Override public int getItemCount() { return mDataset.size(); }
}
alternatively is there way to restore normal state while swiping left? Some time need to restore item rather then deletion.
@ashishoc Try adding the following after inserting the item, so the code looks like this:
adapter.mDataset.add(getItem());
adapter.notifyItemInserted(adapter.mDataset.size() - 1);
Currently there is no way of programmatically undoing a swipe, and I probably won't add this in the near future due to limited time. Pull Requests are welcome though 😃!
Thanks, Btw I came to this solution for now..
if (direction == SWIPE_LEFT) {
//restore state
notifyItemChanged(position);
}
I'm trying to add new Item in list using adapter instance after setting adapter.