I would like to implement "new" data binding in my app, but I'm stuck. I have a database where I save the contacts, so I need to take data from another fragment and show them in the recycler view. SO: fragment with recycler view -> click on AddButton -->open new fragment with editTexts -> fill the editTexts -> save to database -> show in previous fragment (with that recycler view) I've done Adapter but I don't know ho to set it in my fragment. Could you help me please?
Model
public class Contact extends SugarRecord {
public String name;
private String number;
private String email;
private boolean isFavourite;
public Contact() {
}
public Contact(String name, String email, String number, Boolean isFavourite) {
this.name = name;
this.email = email;
this.number = number;
this.isFavourite = isFavourite;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setNumber(String number) {
this.number = number;
}
public void setFavourite(Boolean isFavourite) {
this.isFavourite = isFavourite;
}
@Bindable
public String getName() {
return name;
}
@Bindable
public String getEmail() {
return email;
}
@Bindable
public String getNumber() {
return number;
}
@Bindable
public boolean getFavourite() {
return isFavourite;
}
}
Adapter
public class ContactsRecyclerAdapter extends RecyclerView.Adapter<ContactsRecyclerAdapter.BindingHolder> {
private List<Contact> mContacts;
public static class BindingHolder extends RecyclerView.ViewHolder {
private ViewDataBinding binding;
public BindingHolder(View rowView) {
super(rowView);
binding = DataBindingUtil.bind(rowView);
}
public ViewDataBinding getBinding() {
return binding;
}
}
public ContactsRecyclerAdapter(List<Contact> recyclerUsers) {
this.mContacts = recyclerUsers;
}
@Override
public BindingHolder onCreateViewHolder(ViewGroup parent, int type) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_contact, parent, false);
BindingHolder holder = new BindingHolder(view);
return holder;
}
@Override
public void onBindViewHolder(BindingHolder holder, int position) {
final Contact contact = mContacts.get(position);
holder.getBinding().setVariable(BR.contact, contact);
holder.getBinding().executePendingBindings();
}
@Override
public int getItemCount() {
return mContacts.size();
}
}
Fragment where I want the recycler view
public class ContactListFragment extends Fragment implements View.OnClickListener {
private static final String NEWCONTACT = "newcontact";
FloatingActionButton fabButton;
SearchView searchView;
public static ContactListFragment newInstance() {
Bundle args = new Bundle();
ContactListFragment fragment = new ContactListFragment();
fragment.setArguments(args);
return fragment;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_contact_list, container, false);
return view;
}
----- other stuff in the fragment ------
Try to initialise RecyclerView
in onCreateView
method of ContactListFragment
like below
View view = inflater.inflate(R.layout.fragment_contact_list, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
if(recyclerView != null) {
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(new ContactsRecyclerAdapter(recyclerUsers));
}
this way you can set adapter to recycler view in fragment.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.