基于WIFI direct的即时聊天app开发(一)

最近和几个同事在闲余时间做了一个基于WIFI direct的即时聊天的app,我做的部分比较简单,负责实现的是聊天历史记录,以及删除的功能,因为知道自己的部分比较简单,所以也自告奋勇承担了UI的设计,所以之后的内容大概会是:

1. 聊天历史记录的功能实现

2. 一些UI的七七八八

3. 其他几个重要功能的实现。

这一篇主要讲聊天记录的实现,其他的主要功能会在之后慢慢添加。


要实现聊天记录,主要有几个界面的需求:记录了聊天历史的列表,聊天记录内容(可以直接复用聊天记录功能用于显示)。


一. 聊天记录列表的实现.

1.三个布局文件和一个menu布局文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/history_delete"
        android:icon="@drawable/ic_delete_forever_white_48dp"
        android:showAsAction="always"/>
</menu>

history_title.xml 用于界面上的标题栏显示,包含了一个删除控件 history_delete,用于删除历史记录;


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="@drawable/historylist">
    <ListView 
        android:id="@+id/historylist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        
    </ListView>

</LinearLayout>
history_list.xml 布局了一个ListView容器,用于排布历史记录列表


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="horizontal">
    <TextView
    	android:id="@+id/history_item"
    	android:layout_width="match_parent"
    	android:layout_height="match_parent"
   		android:textColor="@color/black"
   		android:layout_gravity="center_vertical"
   		android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
		android:layout_marginLeft="5dp"
		android:layout_weight="1"
    	android:textSize="18sp"    > 
    </TextView>
    
    <CheckBox
        android:id="@+id/history_cb"
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"   
        android:visibility="visible"
        />
</LinearLayout>
history_array_item.xml 用于填充listview中的内容的设置 包含一个txtview和一个checkbox


2.实现文件

这个实现的功能主要是将数据库的历史记录列表显示,然后展开聊天记录,以及实现聊天记录的多选删除,开始弄多选的时候 checkbox的焦点都被抢占了,导致直接点到checkbox上没有实现勾选功能,现在改了属性就好很多,功能比较简单,数据库接口也还没写,不管了,之后有空再详解。

package com.gof.letschat.android.activity;

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

import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.util.Log;

import com.gof.letschat.R;


public class HistoryListActivity extends Activity {
	
	public static final String TAG = "History";	
	private ListView list;
	private Context  context;
	private ImageButton delete;
	private List<String> array = new ArrayList<String>();
	private List<String> selectid = new ArrayList<String>();
	private boolean isMulChoice = false; //multi choice
	private historyadapter adapter;
	//private RelativeLayout layout;
	
	
	@Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.history_list);
	        //Instance every controls
	        context = this;
	        list = (ListView) findViewById(R.id.historylist);
	        init();
	        adapter = new historyadapter(context);
	        list.setAdapter(adapter);     
	 }
	
	void init()
	{
		for(int i = 0; i < 15; i++){
			array.add("test " + i);
		}
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.history_title, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	public boolean onPrepareOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.history_delete:
			if(isMulChoice)
			{
				isMulChoice =false;
				Log.d(HistoryListActivity.TAG, "click delete to delete to set isMulChoice as" + isMulChoice);
				for(int i=0;i<selectid.size();i++){
					for(int j=0;j<array.size();j++){
						if(selectid.get(i).equals(array.get(j))){
							Log.d(HistoryListActivity.TAG, "remove array "+array.get(j));
							array.remove(j);
							}
						}
					}
				selectid.clear();
				adapter = new historyadapter(context);
				list.setAdapter(adapter);
			}
			else
			{
				isMulChoice = true;
				Log.d(HistoryListActivity.TAG, "click delete to show to set isMulChoice as" + isMulChoice);
				selectid.clear();
				for(int i=0;i<array.size();i++)
				{
					adapter.visiblecheck.put(i, CheckBox.VISIBLE);
				}
				adapter = new historyadapter(context);
				list.setAdapter(adapter);
			}
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	
	/*history adapter*/
	
	class historyadapter extends BaseAdapter{
		private Context context;
		private LayoutInflater inflater=null;
		private HashMap<Integer, View> mView ;
		public  HashMap<Integer, Integer> visiblecheck ;//用来记录是否显示checkBox
		public  HashMap<Integer, Boolean> ischeck;
		
		public historyadapter(Context context)
		{
			this.context = context;
			inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			mView = new HashMap<Integer, View>();
			visiblecheck = new HashMap<Integer, Integer>();
			ischeck      = new HashMap<Integer, Boolean>();
			Log.d(HistoryListActivity.TAG, "isMulChoice :" + isMulChoice);
			if(isMulChoice){
			for(int i=0;i<array.size();i++){
				ischeck.put(i, false);
				visiblecheck.put(i, CheckBox.VISIBLE);
				}
			}else{
				for(int i=0;i<array.size();i++)
				{
					ischeck.put(i, false);
					visiblecheck.put(i, CheckBox.INVISIBLE);
				}
			}
		}
		
		public int getCount() {
		// TODO Auto-generated method stub
			return array.size();
		}
		
		public Object getItem(int position) {
		// TODO Auto-generated method stub
			return array.get(position);
		}

		public long getItemId(int position) {
		// TODO Auto-generated method stub
			return 0;
		}

        
		public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = mView.get(position);
		if(view==null)
		{
			view = inflater.inflate(R.layout.history_array_item, null);
			final CheckBox ceb = (CheckBox)view.findViewById(R.id.history_cb);
			final TextView txt = (TextView)view.findViewById(R.id.history_item);
			
			ceb.setChecked(ischeck.get(position));
			ceb.setVisibility(visiblecheck.get(position));
			txt.setText(array.get(position));
			
			view.setOnLongClickListener(new Onlongclick());

			view.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(isMulChoice){
						if(ceb.isChecked()){
							ceb.setChecked(false);
							selectid.remove(array.get(position));
						}else{
							ceb.setChecked(true);
							selectid.add(array.get(position));
							}
						}
					else{
						Log.d(HistoryListActivity.TAG, "OnClick trun to historytrecord isMulChoice" + isMulChoice);
						startActivity(new Intent(HistoryListActivity.this, HistoryRecordActivity.class));
					}
						
					}
				});
			
			ceb.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(isMulChoice){
						if(ceb.isChecked()){
							ceb.setChecked(false);
							selectid.remove(array.get(position));
						}else{
							ceb.setChecked(true);
							selectid.add(array.get(position));
							}
						}	
					}
				});
				
			mView.put(position, view);
			}
			return view;
		}

		class Onlongclick implements OnLongClickListener{
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				isMulChoice = true;
				selectid.clear();
				Log.d(HistoryListActivity.TAG, "OnlongClick to set isMulChoice as" + isMulChoice);
				for(int i=0;i<array.size();i++)
				{
					adapter.visiblecheck.put(i, CheckBox.VISIBLE);
				}
				adapter = new historyadapter(context);
				list.setAdapter(adapter);
				return true;
				}
		}

	}

}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值