1. 程式人生 > >android中RecyclerView控件實現長按彈出PopupMenu菜單功能

android中RecyclerView控件實現長按彈出PopupMenu菜單功能

mage 有一個 手工 sim pat 創建 .get mco span

之前寫過一篇文章:android中實現簡單的聊天功能

現在是在之前功能的基礎上,添加一個長按聊天記錄,刪除對應聊天記錄的功能

RecyclerView控件,沒有對應的長按事件,我們需要自己手工添加,修改對應的適配器類,註意紅色內容為添加內容

ChatAdapter.java:

package com.example.chenrui.app1;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import com.example.chenrui.common.Chat; import java.util.List; public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
private List<Chat> chatList; public ChatAdapter(List<Chat> chatList) { this.chatList = chatList; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.chat_item,viewGroup,false
); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) { Chat chat = chatList.get(i); viewHolder.chatText.setText(chat.getContent()); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(viewHolder.chatText.getLayoutParams()); if(chat.getTargetUser().equals("張三")) { params.gravity = Gravity.LEFT; } else { params.gravity = Gravity.RIGHT; } viewHolder.chatText.setLayoutParams(params); if(onItemClickListener!=null) { viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { onItemClickListener.onItemLongClick(viewHolder.itemView,i); return false; } }); } } @Override public int getItemCount() { return chatList.size(); } static class ViewHolder extends RecyclerView.ViewHolder { TextView chatText; public ViewHolder(@NonNull View itemView) { super(itemView); chatText = itemView.findViewById(R.id.chatText); } } private OnItemClickListener onItemClickListener; public interface OnItemClickListener{ void onItemLongClick(View view , int pos); } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } }

修改對應的類,註意紅色內容為添加內容

這裏實現了長按聊天內容,彈出一個PopupMenu菜單,PopupMenu菜單的使用參考我的上一篇文章:Android中創建PopupMenu彈出式菜單

菜單有一個刪除功能,點擊刪除功能,刪除對應的聊天記錄

MainActivity.java

package com.example.chenrui.app1;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.Spinner;

import com.example.chenrui.common.Chat;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final RecyclerView chatList = findViewById(R.id.chatList);

        final List<Chat> chatData = new ArrayList<Chat>();
        final ChatAdapter chatAdapter = new ChatAdapter(chatData);
        chatList.setAdapter(chatAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        chatList.setLayoutManager(layoutManager);
chatAdapter.setOnItemClickListener(
new ChatAdapter.OnItemClickListener() { @Override public void onItemLongClick(View view, final int pos) { PopupMenu popupMenu = new PopupMenu(MainActivity.this,view); popupMenu.getMenuInflater().inflate(R.menu.menu3,popupMenu.getMenu()); //彈出式菜單的菜單項點擊事件 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { if(item.getItemId()==R.id.delete) { chatData.remove(pos); chatAdapter.notifyItemRemoved(pos); } return false; } }); popupMenu.show(); } }); final Spinner targetUser = findViewById(R.id.targetUser); String[] data = {"張三","李四"}; ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,R.layout.support_simple_spinner_dropdown_item,data); targetUser.setAdapter(adapter); final EditText chatContent = findViewById(R.id.chatContent); Button chatSubmit = findViewById(R.id.chatSubmit); chatSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(!chatContent.getText().toString().equals("")) { String user = targetUser.getSelectedItem().toString(); chatData.add(new Chat(user, chatContent.getText().toString())); chatAdapter.notifyItemInserted(chatData.size() - 1); chatList.scrollToPosition(chatData.size() - 1); chatContent.setText(""); } } }); } }

顯示效果:

技術分享圖片

android中RecyclerView控件實現長按彈出PopupMenu菜單功能