专业IM即时通讯软件开发,值得信赖!

Android仿微信聊天置顶,删除效果

即时通讯软件开发 云聊IM 63℃

实现类似微信聊天中的消息的置顶删除效果

Java代码:

package com.example.fangweixin;
 
 
import java.util.ArrayList;
 
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
 
 
public class MainActivity extends ListActivity {
	private ArrayList data=null;
	private ArrayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.activity_main);
        data=new ArrayList();
        for(int i=0;i<50;i++){
        	data.add("测试数据"+i);
        }
        adapter=new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1, data);
        setListAdapter(adapter);
        ListView lv= getListView();
        lv.setOnCreateContextMenuListener(this);
    }
 
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
    		ContextMenuInfo menuInfo) {
    	// TODO Auto-generated method stub
    	menu.add(1, 1001, 0, "未读");
    	menu.add(1, 1002, 1, "置顶");
    	menu.add(1, 1003, 2, "删除");
    	super.onCreateContextMenu(menu, v, menuInfo);
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
    	// TODO Auto-generated method stub
    	AdapterContextMenuInfo info=(AdapterContextMenuInfo) item.getMenuInfo();
    	final int pos=info.position;
    	int id=item.getItemId();
    	switch (id) {
		case 1001:
			Toast.makeText(this,"未读消息", Toast.LENGTH_SHORT).show();
			break;
		case 1002:
			AlertDialog dialog=new AlertDialog.Builder(this).create();
			dialog.setTitle("对话框");
			dialog.setMessage("是否置顶");
			dialog.setButton(AlertDialog.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					String s=data.remove(pos);
					data.add(0, s);
					adapter.notifyDataSetChanged();
					Toast.makeText(getApplicationContext(), "置顶成功", Toast.LENGTH_SHORT).show();
				}
			});
			dialog.setButton(AlertDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
					Toast.makeText(getApplicationContext(), "取消置顶", Toast.LENGTH_SHORT).show();
				}
			});
			dialog.show();
			break;
		case 1003:
			AlertDialog dialog1=new AlertDialog.Builder(this).create();
			dialog1.setTitle("对话框");
			dialog1.setMessage("是否删除");
			dialog1.setButton(AlertDialog.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					String s=data.remove(pos);
				
					adapter.notifyDataSetChanged();
					Toast.makeText(getApplicationContext(), "删除成功", Toast.LENGTH_SHORT).show();
				}
			});
			dialog1.setButton(AlertDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
					Toast.makeText(getApplicationContext(), "取消删除", Toast.LENGTH_SHORT).show();
				}
			});
			dialog1.show();
			break;			
 
		default:
			break;
		}
    	return super.onContextItemSelected(item);
    }
   
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
喜欢 (0)
仿微信聊天软件开发
点击这里给我发消息