Android仿qq聊天记录长按删除功能效果

最近项目在做IM即时通讯开发,在删除聊天列表的时候跟删除聊天详细信息的时候,产品经理想要跟ios一样,在当前选中行上方弹出一个删除窗口.于是先从网上找demo,找了一个发现是Dialog做的,我感觉没有必要这么麻烦,于是我用Popupwindow实现了一个,有需要的朋友可以参考一下。

1.效果图如下(长按列表弹窗,消息详细信息长按弹窗)

Android仿qq聊天记录长按删除功能效果_第1张图片


2.对源码进行说明。

一条消息实体类,有消息内容跟是否发送这两个属性。

public class Message {
	private String content;// 消息内容
	private boolean sended;// 是否发送
	
	public Message(){
	}
	
	public Message(String content,boolean sended){
		this.content=content;
		this.sended=sended;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public boolean isSended() {
		return sended;
	}

	public void setSended(boolean sended) {
		this.sended = sended;
	}
}

消息详细信息的Activity

1).给每条消息设置长按事件,把点击的下标用tag传进去

2).用popupwindow展示,显示在当前点击的View下方,然后设置xy的偏移度

 * 消息详细界面
 * @author ansen
 * @create time 2015-08-04
 */
public class MessageDetailActivity extends Activity{
	private List<Message> messages=new ArrayList<Message>();
	private ListView listView;
	private MyAdapter mAdapter;
	
	private PopupWindow popupWindow;
	private TextView tvDelete;
	private EditText etInput;
	private int longClickPosition;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_private_message_detail_list);
		
		initData();
		
		listView=(ListView) findViewById(R.id.list_private_message);
		listView.setAdapter(mAdapter=new MyAdapter());
		
		TextView sendMessage=(TextView) findViewById(R.id.tv_send_message);
		sendMessage.setOnClickListener(clickListener);
		
		etInput=(EditText) findViewById(R.id.et_input);
	}
	
	
	private class MyAdapter extends BaseAdapter{
		private LayoutInflater inflater;
		public MyAdapter(){
			inflater=LayoutInflater.from(MessageDetailActivity.this);
		}

		@Override
		public int getCount() {
			return messages.size();
		}

		@Override
		public Object getItem(int position) {
			return messages.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if(null==convertView){
				holder=new ViewHolder();
				convertView= inflater.inflate(R.layout.item_private_message_chat,parent,false);
				holder.tvMessageTo=(TextView) convertView.findViewById(R.id.tv_message_to);
				holder.ivMessageToHeadImage=(ImageView) convertView.findViewById(R.id.iv_message_to_head_image);
				
				holder.tvMessageFrom=(TextView) convertView.findViewById(R.id.tv_message_from);
				holder.ivMessageFromHeadImage=(ImageView) convertView.findViewById(R.id.iv_message_from_head_image);
				convertView.setTag(holder);
			}else{
				holder=(ViewHolder) convertView.getTag();
			}
			
			Message message=messages.get(position);
			if(message.isSended()){//发送消息
				holder.tvMessageTo.setVisibility(View.GONE);
				holder.ivMessageToHeadImage.setVisibility(View.GONE);
				
				holder.tvMessageFrom.setVisibility(View.VISIBLE);
				holder.tvMessageFrom.setText(message.getContent());
				holder.tvMessageFrom.setOnLongClickListener(longClickListener);
				holder.tvMessageFrom.setTag(position);
				
				holder.ivMessageFromHeadImage.setVisibility(View.VISIBLE);
			}else{//接收消息
				holder.tvMessageFrom.setVisibility(View.GONE);
				holder.ivMessageFromHeadImage.setVisibility(View.GONE);
				
				holder.tvMessageTo.setVisibility(View.VISIBLE);
				holder.tvMessageTo.setText(message.getContent());
				holder.tvMessageTo.setOnLongClickListener(longClickListener);
				holder.tvMessageTo.setTag(position);
				
				holder.ivMessageToHeadImage.setVisibility(View.VISIBLE);
			}
			return convertView;
		}
		
		private class ViewHolder{
			private ImageView ivMessageToHeadImage;//接收消息用户头像
			private TextView tvMessageTo;//接收消息内容
			
			private ImageView ivMessageFromHeadImage;//发送消息用户头像
			private TextView tvMessageFrom;//发送消息内容
		}
	}
	
	private OnLongClickListener longClickListener=new OnLongClickListener() {
		@Override
		public boolean onLongClick(View v) {
			longClickPosition=(Integer) v.getTag();
			showDialog(v);
			return true;
		}
	};
	
	private void  showDialog(View view){
		if(null==popupWindow){
			View popView = LayoutInflater.from(this).inflate(R.layout.layout_long_click_dialog, null);
			tvDelete=(TextView) popView.findViewById(R.id.tv_delete);
			tvDelete.setOnClickListener(clickListener);
			popupWindow = new PopupWindow(popView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			popupWindow.setAnimationStyle(R.style.PopAnimStyle);
			popupWindow.setOutsideTouchable(true);
			popupWindow.setBackgroundDrawable(new BitmapDrawable());
		}
		if (popupWindow.isShowing())
			popupWindow.dismiss();
		
		//第一次显示控件的时候宽高会为0 
		int deleteHeight=tvDelete.getHeight()==0?145:tvDelete.getHeight();
		int deleteWidth=tvDelete.getWidth()==0?212:tvDelete.getWidth();
		
		popupWindow.showAsDropDown(view,(view.getWidth()-deleteWidth)/2,-view.getHeight()-deleteHeight);
	}
	
	private OnClickListener clickListener=new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.tv_delete:
				messages.remove(longClickPosition);
				mAdapter.notifyDataSetChanged();
				
				popupWindow.dismiss();
				break;
			case R.id.tv_send_message:
				String content=etInput.getText().toString().trim();
				if(!TextUtils.isEmpty(content)){
					Message message=new Message(content, true);
					messages.add(message);
					mAdapter.notifyDataSetChanged();
					listView.setSelection(mAdapter.getCount()-1);
				}
				break;
			}
		}
	};
	
	private void initData(){
		Message message=new Message("范德萨范德", true);
		Message message7=new Message("范德萨范德fds", true);
		Message message1=new Message("个人提个人鬼地", false);
		Message message4=new Message("接收消息", false);
		Message message2=new Message("吃饭了嘛。。。。吃过了没有啊。。。。。还没有吃啊 范德萨范德萨发水电费的说法都是", true);
		Message message3=new Message("吃饭了嘛。。。。吃过了没有啊。。。。。还没有吃啊 范德萨范德萨发水电费的说法都是", false);
		messages.add(message);
		messages.add(message1);
		messages.add(message2);
		messages.add(message3);
		messages.add(message4);
		messages.add(message7);
	}
}

消息详细列表布局文件 activity_private_message_detail_list.xml     

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/list_private_message"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/ll_bottom"
        android:divider="@null" >
    </ListView>

    <include
        android:id="@+id/ll_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        layout="@layout/layout_input_comment" />

</RelativeLayout>


底部输入框布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="7dip"
        android:paddingTop="7dip">
        
        <ImageView
            android:id="@+id/iv_message_to_head_image"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/slide_left_avatar_default"/>

        <TextView
            android:id="@+id/tv_message_to"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/icon_message_to"
            android:gravity="center"
            android:paddingLeft="20dip"
            android:textColor="@color/register_text_color"
            android:layout_toRightOf="@+id/iv_message_to_head_image"
            android:layout_marginRight="55dp"
            android:textSize="16dip"/>

        <TextView
            android:id="@+id/tv_message_from"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           	android:layout_toLeftOf="@+id/iv_message_from_head_image"
            android:background="@drawable/icon_message_from"
            android:gravity="center"
            android:paddingRight="20dip"
            android:textColor="@color/white_normal"
            android:layout_marginLeft="55dp"
            android:text="我已经吃过了"
            android:textSize="16dip"/>
        
        <!--          -->
        <ImageView
            android:id="@+id/iv_message_from_head_image"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/slide_left_avatar_default"/>
    </RelativeLayout>

</LinearLayout>

每一条消息的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="7dip"
        android:paddingTop="7dip">
        
        <ImageView
            android:id="@+id/iv_message_to_head_image"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/slide_left_avatar_default"/>

        <TextView
            android:id="@+id/tv_message_to"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/icon_message_to"
            android:gravity="center"
            android:paddingLeft="20dip"
            android:textColor="@color/register_text_color"
            android:layout_toRightOf="@+id/iv_message_to_head_image"
            android:layout_marginRight="55dp"
            android:textSize="16dip"/>

        <TextView
            android:id="@+id/tv_message_from"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           	android:layout_toLeftOf="@+id/iv_message_from_head_image"
            android:background="@drawable/icon_message_from"
            android:gravity="center"
            android:paddingRight="20dip"
            android:textColor="@color/white_normal"
            android:layout_marginLeft="55dp"
            android:text="我已经吃过了"
            android:textSize="16dip"/>
        
        <!--          -->
        <ImageView
            android:id="@+id/iv_message_from_head_image"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/slide_left_avatar_default"/>
    </RelativeLayout>

</LinearLayout>


弹出删除按钮的布局文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tv_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/icon_private_message_delete"
        android:gravity="center_horizontal"
        android:padding="5dp"
        android:text="删除"
        android:textColor="#ffffffff" />

</FrameLayout>

点击链接下载源码

你可能感兴趣的:(PopupWindow,弹窗,仿QQ长按删除)