码迷,mamicode.com
首页 > 移动开发 > 详细

android中RecyclerView控件实现长按弹出PopupMenu菜单功能

时间:2019-01-17 15:21:55      阅读:367      评论:0      收藏:0      [点我收藏+]

标签:count   inner   分享   oncreate   cte   ref   klist   tar   submit   

之前写过一篇文章: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菜单功能

标签:count   inner   分享   oncreate   cte   ref   klist   tar   submit   

原文地址:https://www.cnblogs.com/modou/p/10281924.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!