我正在尝试将用户添加到类似WhatsApp的组中,但是我不知道该怎么做,并且我还没有找到解决方案。
当我们在WhatsApp中添加用户时,添加的用户水平显示在上部,并且联系人列表中的用户用绿色圆圈标记。
就我而言,我使用了两个RecyclerView:一个用于添加的用户,另一个用于联系人列表。
我的第一个问题是:添加的用户每页显示一个。如何将它们并排放置?
我的第二个问题是:我不知道如何将CircleImageView从绿色更改为无,反之亦然。我认为这与两个适配器之间的通信有关。
这是我到目前为止所拥有的:
这是我如何在Activity.java中调用RecyclerViews,其中mUserList
对应于联系人列表,mUserListAdded
对应于添加的用户(水平recyclerview)。
mToolBar = findViewById(R.id.find_user_bar_layout);
setSupportActionBar(mToolBar);
getSupportActionBar().setTitle("Novo Contato");
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
mUpdateGroupBtn = findViewById(R.id.new_user_finalize_button);
mUserList= findViewById(R.id.userList);
mUserList.setNestedScrollingEnabled(false);
mUserList.setHasFixedSize(false);
mUserListLayoutManager = new LinearLayoutManager(getApplicationContext(),LinearLayout.VERTICAL,false);
mUserList.setLayoutManager(mUserListLayoutManager);
mUserListAdapter = new UserListAdapter(userList, this);
mUserList.setAdapter(mUserListAdapter);
mUserListAdded = findViewById(R.id.user_from_contactlist);
mUserListAdded.setNestedScrollingEnabled(false);
mUserListAdded.setHasFixedSize(true);
mUserListAddedLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL,false);
mUserListAdded.setLayoutManager(mUserListAddedLayoutManager);
mUserListAddedAdapter = new UserListAddedAdapter(userListAdded, this);
mUserListAdded.setAdapter(mUserListAddedAdapter);
这是mUserList
的适配器:
public class UserListAdapter extends RecyclerView.Adapter<UserListAdapter.UserListViewHolder> implements Filterable {
private UserListToUserListAddedAdapter userListToUserListAddedAdapter;
ArrayList<UserObject> userList, userListFull;
private HashMap<Integer,Boolean> selectedItem = new HashMap<>();
public UserListAdapter(ArrayList<UserObject> userList, UserListToUserListAddedAdapter listener){
this.userList = userList;
this.userListFull = new ArrayList<>(userList);
this.userListToUserListAddedAdapter = listener;
}
@NonNull
@Override
public UserListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user,null,false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
layoutView.setLayoutParams(lp);
UserListViewHolder rcv = new UserListViewHolder(layoutView);
return rcv;
}
@Override
public void onBindViewHolder(@NonNull final UserListViewHolder holder, final int position) {
holder.mName.setText(userList.get(position).getName());
holder.mPhone.setText(userList.get(position).getPhone());
holder.mLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (selectedItem.get(position) == null || selectedItem.get(position) == false) {
selectedItem.put(position, true);
holder.mIcon.setVisibility(View.VISIBLE);
holder.mIcon.setImageResource(R.drawable.online);
if (userListToUserListAddedAdapter != null) {
Log.i("debinf userListAdp", "userListToUserListAddedAdapter != null");
userListToUserListAddedAdapter.getUserAdded(userList.get(position), true);
}
} else {
selectedItem.put(position, false);
holder.mIcon.setVisibility(View.INVISIBLE);
if (userListToUserListAddedAdapter != null) {
Log.i("debinf userListAdp", "userListToUserListAddedAdapter != null");
userListToUserListAddedAdapter.getUserAdded(userList.get(position), false);
}
}
/*String key = FirebaseDatabase.getInstance().getReference().child("group").push().getKey();
FirebaseDatabase.getInstance().getReference().child("user").child(FirebaseAuth.getInstance().getUid()).child("group").child(key).setValue(true);
FirebaseDatabase.getInstance().getReference().child("user").child(userList.get(position).getUid()).child("group").child(key).setValue(true);*/
if (FirebaseAuth.getInstance().getUid().equals(userList.get(position).getUid())) {
Toast.makeText(v.getContext(), "Voce eh o(a) criador(a) do Grupo e ja foi adicionado(a)", Toast.LENGTH_SHORT).show();
} else {
//FirebaseDatabase.getInstance().getReference().child("ContactAdded").child(FirebaseAuth.getInstance().getUid()).child(userList.get(position).getUid()).setValue("");
Map<String, String> NamePhoneMap = new HashMap<>();
NamePhoneMap.put("name",userList.get(position).getName());
NamePhoneMap.put("phone",userList.get(position).getPhone());
Toast.makeText(v.getContext(), "Voce adiciou : " + NamePhoneMap, Toast.LENGTH_SHORT).show();
}
//DatabaseReference contactAdded = FirebaseDatabase.getInstance().getReference().child("ContactAdded").child(FirebaseAuth.getInstance().getUid()).child(userList.get(position).getUid());
//Task<Void> voidTask = contactAdded.updateChildren(NamePhoneMap);
//ItemClicked.add();
//Log.i("debinf userlistadapter", "");
}
});
}
@Override
public int getItemCount() {
return userList.size();
}
public void addBackingListUser(UserObject user) {
userListFull.add(user);
}
@Override
public Filter getFilter() {
return teamFilter;
}
private Filter teamFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<UserObject> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0) {
filteredList.addAll(userListFull);
} else {
String filterParttern = constraint.toString().toLowerCase().trim();
for (UserObject user : userListFull) {
if (user.getName().toLowerCase().contains(filterParttern) || user.getPhone().toLowerCase().contains(filterParttern)) {
filteredList.add(user);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
userList.clear();
userList.addAll((List) results.values);
notifyDataSetChanged();
}
};
public class UserListViewHolder extends RecyclerView.ViewHolder {
public CircleImageView mIcon;
public TextView mName, mPhone;
public RelativeLayout mLayout;
public UserListViewHolder(View view){
super(view);
mIcon = view.findViewById(R.id.user_profile_icon);
mName = view.findViewById(R.id.name);
mPhone = view.findViewById(R.id.phone);
mLayout = view.findViewById(R.id.layoutItemUser);
}
}
public void setProfileIcon(UserObject userObject) {
}
public interface UserListToUserListAddedAdapter {
void getUserAdded(UserObject user, boolean isToAdd);
}
}
这是mUserListAdded
public class UserListAddedAdapter extends RecyclerView.Adapter<UserListAddedAdapter.UserListAddedViewHolder> {
private UserListAddedToUserListAdapter userListAddedToUserListAdapter;
ArrayList<UserObject> userListAdded;
public UserListAddedAdapter(ArrayList<UserObject> userListAdded, UserListAddedToUserListAdapter listener){
this.userListAdded = userListAdded;
this.userListAddedToUserListAdapter = listener;
}
@NonNull
@Override
public UserListAddedAdapter.UserListAddedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user_added,null,false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
layoutView.setLayoutParams(lp);
UserListAddedAdapter.UserListAddedViewHolder rcv = new UserListAddedAdapter.UserListAddedViewHolder(layoutView);
return rcv;
}
@Override
public void onBindViewHolder(@NonNull UserListAddedViewHolder holder, final int position) {
holder.mName.setText(userListAdded.get(position).getPhone());
//holder.mPhone.setText(userListAdded.get(position).getPhone());
holder.mIcon.setImageResource(R.drawable.cancel_black_18dp);
//holder.mLayout.setOnTouchListener();
holder.mLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (userListAddedToUserListAdapter != null) {
Log.i("debinf userListAdp", "userListToUserListAddedAdapter != null");
userListAddedToUserListAdapter.getUserRemoved(userListAdded.get(position));
}
/*String key = FirebaseDatabase.getInstance().getReference().child("group").push().getKey();
FirebaseDatabase.getInstance().getReference().child("user").child(FirebaseAuth.getInstance().getUid()).child("group").child(key).setValue(true);
FirebaseDatabase.getInstance().getReference().child("user").child(userList.get(position).getUid()).child("group").child(key).setValue(true);*/
if (FirebaseAuth.getInstance().getUid().equals(userListAdded.get(position).getUid())) {
//Log.i("debinf", "Owner Group was selected");
Toast.makeText(v.getContext(), "Voce eh o(a) criador(a) do Grupo e nao pode ser removido(a)", Toast.LENGTH_SHORT).show();
} else {
FirebaseDatabase.getInstance().getReference().child("ContactAdded").child(FirebaseAuth.getInstance().getUid()).child(userListAdded.get(position).getUid()).removeValue();
Toast.makeText(v.getContext(), "Voce removeu : " + userListAdded.get(position).getUid(), Toast.LENGTH_SHORT).show();
}
//ItemClicked.add();
//Log.i("debinf userlistadapter", "");
}
});
}
@Override
public int getItemCount() {
return userListAdded.size();
}
public class UserListAddedViewHolder extends RecyclerView.ViewHolder {
public CircleImageView mIcon;
public TextView mName;
public RelativeLayout mLayout;
public UserListAddedViewHolder(View view){
super(view);
mIcon = view.findViewById(R.id.user_profile_icon_added);
mIcon.setVisibility(View.VISIBLE);
mName = view.findViewById(R.id.name_user_added);
mLayout = view.findViewById(R.id.layoutItemUserAdded);
}
}
public interface UserListAddedToUserListAdapter {
void getUserRemoved(UserObject user);
}
}
这是 xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NewUserToGroupActivity">
<include
android:id="@+id/find_user_bar_layout"
layout="@layout/app_bar_layout">
</include>
<android.support.v7.widget.RecyclerView
android:id="@+id/user_from_contactlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/find_user_bar_layout">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:id="@+id/users_already_added"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/user_from_contactlist"
android:orientation="vertical">
<View
android:id="@+id/list_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="4dp"
android:background="?android:attr/listDivider" />
<android.support.v7.widget.RecyclerView
android:id="@+id/userList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/list_divider">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<Button
android:id="@+id/new_user_finalize_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="0dp"
android:text="Finalizar"
android:textAllCaps="false" />
</RelativeLayout>
我感谢任何能帮助我解决这两个问题的人:水平项目(上部)之间的间距以及将CircleImageView从绿色更改为绿色(在删除时更改为无)。
答案 0 :(得分:0)
更合适的解决方案是使用 Android Material Chips
https://material.io/develop/android/components/chip/
dependencies {
implementation 'com.google.android.material:material:1.0.0'
}
另一方面,没有它,我们也可以做类似的工作。
水平项目之间的间距(上部)
该问题发生在UserListAddedAdapter
,更具体地说是LayoutParams
代替:
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
使用:
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
将CircleImageView从添加时的绿色更改为删除时的无。
为此,我们需要在UserObject
中添加一个布尔变量,以替换selectedItem
中UserListAdapter
的工作。
接下来,我们需要通过两个适配器(interface
和UserListAdapter
)来实现UserListAddedAdapter
public class MainActivity extends AppCompatActivity implements UserListAdapter.UserListToUserListAddedAdapter, UserListAddedAdapter.UserListAddedToUserListAdapter
接下来,我们需要处理interface methods
中的MainActivity
@Override
public void getUserAdded(UserObject user, boolean isToAdd) {
Log.i("debinf finduser", "getUserAdded from Adapter = "+user.getPhone()+"; isToAdd = "+isToAdd);
if (isToAdd) {
userListAdded.add(user);
} else {
userListAdded.remove(user);
}
mUserListAddedAdapter.notifyDataSetChanged();
}
@Override
public void getUserRemoved(UserObject user) {
if (userList.size() > 0) {
Log.i("debinf finduser", "Position of userList = " + userList.indexOf(user) + ", AFTER REMOVED userListAdded.size() is " + userListAdded.size());
userList.get(userList.indexOf(user)).setToAdd(false);
mUserListAdapter.notifyDataSetChanged();
} else {
Log.i("debinf finduser", "userList.size() == 0, AFTER REMOVED userListAdded.size() is " + userListAdded.size());
}
}
最后,我们需要对UserListAdapter
public class UserListAdapter extends RecyclerView.Adapter<UserListAdapter.UserListViewHolder> implements Filterable {
private UserListToUserListAddedAdapter userListToUserListAddedAdapter;
ArrayList<UserObject> userList, userListFull;
private HashMap<Integer,Boolean> selectedItem = new HashMap<>();
private UserListAdapter adapter;
public UserListAdapter(ArrayList<UserObject> userList, UserListToUserListAddedAdapter listener){
this.userList = userList;
this.userListFull = new ArrayList<>(userList);
this.userListToUserListAddedAdapter = listener;
this.adapter = this;
}
@NonNull
@Override
public UserListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user,null,false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
layoutView.setLayoutParams(lp);
UserListViewHolder rcv = new UserListViewHolder(layoutView);
return rcv;
}
@Override
public void onBindViewHolder(@NonNull final UserListViewHolder holder, final int position) {
holder.mName.setText(userList.get(position).getName());
holder.mPhone.setText(userList.get(position).getPhone());
holder.mLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("debinf userListAdp", "UserListAdapter Clicou no Layout");
if (!userList.get(position).isToAdd()) {
userList.get(position).setToAdd(true);
holder.mIcon.setVisibility(View.VISIBLE);
holder.mIcon.setImageResource(R.drawable.online);
if (userListToUserListAddedAdapter != null) {
Log.i("debinf userListAdp", "userListToUserListAddedAdapter != null - Position true is "+position);
userListToUserListAddedAdapter.getUserAdded(userList.get(position), true);
}
} else {
userList.get(position).setToAdd(false);
holder.mIcon.setVisibility(View.INVISIBLE);
if (userListToUserListAddedAdapter != null) {
Log.i("debinf userListAdp", "userListToUserListAddedAdapter != null - Position false is "+position);
userListToUserListAddedAdapter.getUserAdded(userList.get(position), false);
}
}
if (FirebaseAuth.getInstance().getUid().equals(userList.get(position).getUid())) {
Toast.makeText(v.getContext(), "Voce eh o(a) criador(a) do Grupo e ja foi adicionado(a)", Toast.LENGTH_SHORT).show();
} else {
Map<String, String> NamePhoneMap = new HashMap<>();
NamePhoneMap.put("name",userList.get(position).getName());
NamePhoneMap.put("phone",userList.get(position).getPhone());
Toast.makeText(v.getContext(), "Voce adiciou : " + NamePhoneMap, Toast.LENGTH_SHORT).show();
}
}
});
if (userList.get(position).isToAdd()) {
Log.i("debinf userListAdp", "UserListAdapter TRUE Position is "+position+", isToAdd is "+userList.get(position).isToAdd());
holder.mIcon.setVisibility(View.VISIBLE);
holder.mIcon.setImageResource(R.drawable.online);
} else {
Log.i("debinf userListAdp", "UserListAdapter FALSE Position is "+position+", isToAdd is "+userList.get(position).isToAdd());
holder.mIcon.setVisibility(View.INVISIBLE);
}
}
@Override
public int getItemCount() {
return userList.size();
}
public void addBackingListUser(UserObject user) {
userListFull.add(user);
}
@Override
public Filter getFilter() {
return teamFilter;
}
private Filter teamFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<UserObject> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0) {
filteredList.addAll(userListFull);
} else {
String filterParttern = constraint.toString().toLowerCase().trim();
for (UserObject user : userListFull) {
if (user.getName().toLowerCase().contains(filterParttern) || user.getPhone().toLowerCase().contains(filterParttern)) {
filteredList.add(user);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
userList.clear();
userList.addAll((List) results.values);
notifyDataSetChanged();
}
};
public class UserListViewHolder extends RecyclerView.ViewHolder {
public CircleImageView mIcon;
public TextView mName, mPhone;
public LinearLayout mLayout;
public UserListViewHolder(View view){
super(view);
mIcon = view.findViewById(R.id.user_profile_icon);
mName = view.findViewById(R.id.name);
mPhone = view.findViewById(R.id.phone);
mLayout = view.findViewById(R.id.layoutItemUser);
}
}
public void setProfileIcon(int position) {
selectedItem.put(position, false);
userList.get(position).setToAdd(false);
adapter.notifyItemChanged(position);
}
public interface UserListToUserListAddedAdapter {
void getUserAdded(UserObject user, boolean isToAdd);
}
}