- 水波纹效果
参考 https://blog.csdn.net/REIGE/article/details/68957994
recycler_item_selector.xml文件
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/design_default_color_primary">
<item android:drawable="@color/white" />
</ripple>
chat_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="@drawable/recycler_item_selector">
<ImageView
android:id="@+id/chat_image"
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/chat_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:gravity="top"
android:text="Name"
android:textSize="18sp"
app:layout_constraintLeft_toRightOf="@id/chat_image"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tx_LastText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="hint"
app:layout_constraintLeft_toRightOf="@id/chat_image"
app:layout_constraintTop_toBottomOf="@id/chat_name" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/black"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
- 长按
菜单使用popup菜单,在Android开发者手册中看到弹出菜单popup。于是百度查询 Recycleview popup 便找到以下结果 https://blog.csdn.net/qq_27494201/article/details/99709433
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(context, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.context_menu, popup.getMenu());
popup.show();
}
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.PopupMenu;
import androidx.recyclerview.widget.RecyclerView;
import com.junhaodev.mywechat.BaseApplication;
import com.junhaodev.mywechat.R;
import java.util.List;
public class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.ViewHolder> {
private List<ChatItem> list;
private Context context;
ChatListAdapter() {
list = null;
context = BaseApplication.getContext();
}
ChatListAdapter(List<ChatItem> list) {
this(BaseApplication.getContext(), list);
}
ChatListAdapter(Context context, List<ChatItem> list) {
this.context = context;
this.list = list;
}
public void setList(List<ChatItem> list) {
this.list = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_item,
parent,
false);
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
showPopup(v);
return true;
}
});
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
ChatItem item = list.get(position);
holder.tx_name.setText(item.getName());
holder.tx_name.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
showPopup(holder.tx_name);
return true;
}
});
}
@Override
public int getItemCount() {
return list.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
private final TextView tx_name;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tx_name = itemView.findViewById(R.id.chat_name);
}
}
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(context, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.context_menu, popup.getMenu());
popup.show();
}
public interface OnRvItemClick {
void onItemClick(View v, int position);
}
}
|