Skip to content

Instantly share code, notes, and snippets.

@Klesz
Created February 21, 2017 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Klesz/bd20df398c555de75a4ebeff397e65e7 to your computer and use it in GitHub Desktop.
Save Klesz/bd20df398c555de75a4ebeff397e65e7 to your computer and use it in GitHub Desktop.
Custom layout
public class FindPeopleLayout extends LinearLayout {
@BindView(R.id.invite_findFriendsLayout)
CardView invite_findFriendsLayout;
@BindView(R.id.friendsCount)
TextView friendsCount;
@BindView(R.id.labelFriends)
TextView labelFriends;
@BindView(R.id.invite_findFriendsRV)
RecyclerView invite_findFriendsRV;
@BindView(R.id.invite_findOthersLayout)
CardView invite_findOthersLayout;
@BindView(R.id.othersCount)
TextView othersCount;
@BindView(R.id.labelOthers)
TextView labelOthers;
@BindView(R.id.invite_findOthersRV)
RecyclerView invite_findOthersRV;
private Unbinder bind;
private FragmentManager fragmentManager;
private FindFriendsAdapter findOthersAdapter, findFriendsAdapter;
@OnClick(R.id.invite_findFriendsLayout)
public void selectFriends() {
BaseFindPeopleFragment newFragment = FindFriendsFragment.newInstance();
newFragment.setTargetFragment(fragmentManager.findFragmentById(R.id.fragment), REQUEST_CODE_FIND_FRIENDS_PICKER);
newFragment.show(fragmentManager, "findFriendsPicker");
}
@OnClick(R.id.invite_findOthersLayout)
public void selectOthers() {
BaseFindPeopleFragment newFragment = FindOthersFragment.newInstance();
newFragment.setTargetFragment(fragmentManager.findFragmentById(R.id.fragment), REQUEST_CODE_FIND_OTHERS_PICKER);
newFragment.show(fragmentManager, "findOthersPicker");
}
public FindPeopleLayout_working(final Context context) {
super(context);
init();
}
public FindPeopleLayout_working(final Context context, final AttributeSet attrs) {
super(context, attrs);
init();
}
public FindPeopleLayout_working(final Context context, final AttributeSet attrs, final int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
bind.unbind();
}
private void init(){
fragmentManager = ((CreateEventActivity)getContext()).getSupportFragmentManager();
final View view = inflate(getContext(), R.layout.find_people, this);
bind = ButterKnife.bind(this, view);
setupRecyclerViewForFriends();
setupRecyclerViewForOthers();
}
public void updateFriends(@NonNull ArrayList<PersonInvited> newFriends){
findFriendsAdapter.getList().clear();
findFriendsAdapter.getList().addAll(newFriends);
findFriendsAdapter.notifyItemRangeChanged(0, newFriends.size());
invite_findFriendsRV.post(() -> friendsCount.setText(String.valueOf(newFriends.size())));
}
public void updateOthers(@NonNull ArrayList<PersonInvited> newOthers){
findOthersAdapter.getList().clear();
findOthersAdapter.getList().addAll(newOthers);
findOthersAdapter.notifyItemRangeChanged(0, newOthers.size());
invite_findOthersRV.post(() -> othersCount.setText(String.valueOf(newOthers.size())));
}
private void setupRecyclerViewForOthers() {
final ArrayList<PersonInvited> list = new ArrayList<>();
invite_findOthersRV.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
findOthersAdapter = new FindFriendsAdapter(getContext(), list);
invite_findOthersRV.setAdapter(findOthersAdapter);
}
private void setupRecyclerViewForFriends() {
final ArrayList<PersonInvited> list = new ArrayList<>();
invite_findFriendsRV.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
findFriendsAdapter = new FindFriendsAdapter(getContext(), list);
invite_findFriendsRV.setAdapter(findFriendsAdapter);
}
public class FindFriendsAdapter extends RecyclerView.Adapter<FindFriendsAdapter.ViewHolder> {
private final String TAG = FindFriendsAdapter.class.getSimpleName();
private final Context context;
private ArrayList<PersonInvited> list = new ArrayList<>();
public ArrayList<PersonInvited> getList() {
return list;
}
public FindFriendsAdapter(@NonNull final Context context, @NonNull final ArrayList<PersonInvited> list) {
this.context = context;
this.list = list;
}
/**
* Provide a reference to the type of views that you are using (custom ViewHolder)
*/
public class ViewHolder extends RecyclerView.ViewHolder {
public final CircleImageView imageView;
public ViewHolder(View v) {
super(v);
imageView = (CircleImageView) v.findViewById(R.id.profileImageView);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.card_create_event_find_friens, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
final PersonInvited element = list.get(position);
Log.d(TAG, "Element " + position + " set.");
Picasso.with(context).load(element.photoUrl).resize(40, 40).noFade().into(viewHolder.imageView);
}
@Override
public int getItemCount() {
return list.size();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment