Android:仿手机QQ好友动态的ListView

1.介绍:

本博客使用XListView模仿Android版QQ好友动态的ListView效果。效果截图例如以下:

效果图1

效果图2

这里面主要涉及的是ListView的布局问题,让我们看一下Item的布局文件吧。

<?xml version="1.0" encoding="utf-8"?

>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/contacts_items"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical" >
    <View
        android:id="@+id/topLine"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#ffcccccc" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >
        <ImageView
            android:id="@+id/imgHead"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:src="@drawable/ic_launcher" />
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:orientation="vertical" >
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp" >
                <TextView
                    android:id="@+id/tvName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center_vertical"
                    android:singleLine="true"
                    android:text="Tom"
                    android:textSize="16.0sp" />
                <TextView
                    android:id="@+id/tvDate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="0.2dip"
                    android:layout_marginTop="0dip"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:text="13:32"
                    android:textColor="#ffcccccc"
                    android:textSize="12sp" />
            </RelativeLayout>
            <TextView
                android:id="@+id/tvContent"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="5dp"
                android:paddingTop="5dp"
                android:text="Hello world"
                android:textSize="14sp" >
            </TextView>
            <ImageView
                android:id="@+id/ivPhoto"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:gravity="center"
                android:visibility="gone" />
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp" >
                <ImageView
                    android:id="@+id/ivAddress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:paddingLeft="2dp"
                    android:src="@drawable/qzone_address_icon"
                    android:visibility="gone" />
                <TextView
                    android:id="@+id/tvAddress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toRightOf="@id/ivAddress"
                    android:paddingLeft="5dp"
                    android:textSize="10sp"
                    android:visibility="gone" />
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp" >
                <ImageView
                    android:id="@+id/ivPhone"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:gravity="center"
                    android:src="@drawable/status_phone" >
                </ImageView>
                <TextView
                    android:id="@+id/tvPhonemodel"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toRightOf="@id/ivPhone"
                    android:paddingLeft="3dp"
                    android:text="Nexus 5"
                    android:textSize="10sp" />
                <TextView
                    android:id="@+id/tvComment"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:paddingLeft="2dp"
                    android:text="评论"
                    android:textSize="10sp" />
                <ImageView
                    android:id="@+id/ivComment"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toLeftOf="@id/tvComment"
                    android:gravity="center"
                    android:paddingLeft="20dp"
                    android:src="@drawable/qzone_picviewer_bottom_comment_icon" >
                </ImageView>
                <TextView
                    android:id="@+id/tvAgree"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toLeftOf="@id/ivComment"
                    android:text="赞"
                    android:textSize="10sp" />
                <ImageView
                    android:id="@+id/ivAgree"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toLeftOf="@id/tvAgree"
                    android:gravity="center"
                    android:src="@drawable/qzone_picviewer_bottom_unpraise_icon" >
                </ImageView>
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp" >
                <ImageView
                    android:id="@+id/ivAgreeShow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:contentDescription="@string/app_name"
                    android:paddingLeft="2dp"
                    android:src="@drawable/qzone_picviewer_bottom_praise_icon"
                    android:visibility="gone" />
                <TextView
                    android:id="@+id/tvAgreeShow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toRightOf="@id/ivAgreeShow"
                    android:paddingLeft="5dp"
                    android:visibility="gone" />
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp" >
                <TextView
                    android:id="@+id/tvComments"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:paddingLeft="5dp"
                    android:visibility="gone" />
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp" >
                <Button
                    android:id="@+id/btnSendComment"
                    android:layout_width="wrap_content"
                    android:layout_height="15dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:background="#00000000"
                    android:focusable="false"
                    android:text="发送"
                    android:textSize="10sp"
                    android:visibility="gone" />
                <Button
                    android:id="@+id/btnComment"
                    android:layout_width="fill_parent"
                    android:layout_height="15dp"
                    android:layout_alignParentLeft="true"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:layout_toLeftOf="@id/btnSendComment"
                    android:background="@drawable/btn_shape"
                    android:hint="Please input your comment..."
                    android:textSize="10sp" />
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>
    <View
        android:id="@+id/lastLine"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#ff474745"
        android:visibility="gone" />
</LinearLayout>

另外一个就是Adapter:

/*
 * $filename: BaseAdapter1.java,v $
 * $Date: 2014-4-27  $
 * Copyright (C) ZhengHaibo, Inc. All rights reserved.
 * This software is Made by Zhenghaibo.
 */
package edu.njupt.zhb.xlistviewtest;

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

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

/*
 *@author: ZhengHaibo
 *web:     http://blog.csdn.net/nuptboyzhb
 *mail:    [email protected]
 *2014-4-27  Nanjing,njupt,China
 */
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class XBaseAdapter extends BaseAdapter {

	private Context context;

	private Activity activity;

	private List<Model> listViewData;

	private int layoutResId;// ListView每一个Item的布局文件

	public XBaseAdapter(Context context, int layoutResId, Activity activity) {
		this.context = context;
		this.layoutResId = layoutResId;
		listViewData = new ArrayList<Model>();
		this.activity = activity;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		Model model = listViewData.get(position);
		ViewItemHolder viewItemHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(layoutResId,
					null);
			viewItemHolder = new ViewItemHolder();
			viewItemHolder.imgHead = (ImageView) convertView
					.findViewById(R.id.imgHead);
			viewItemHolder.tvName = (TextView) convertView
					.findViewById(R.id.tvName);
			viewItemHolder.tvDate = (TextView) convertView
					.findViewById(R.id.tvDate);
			viewItemHolder.tvContent = (TextView) convertView
					.findViewById(R.id.tvContent);
			viewItemHolder.ivPhoto = (ImageView) convertView
					.findViewById(R.id.ivPhoto);
			viewItemHolder.ivAddress = (ImageView) convertView
					.findViewById(R.id.ivAddress);
			viewItemHolder.tvAddress = (TextView) convertView
					.findViewById(R.id.tvAddress);
			viewItemHolder.tvPhonemodel = (TextView) convertView
					.findViewById(R.id.tvPhonemodel);
			viewItemHolder.ivAgree = (ImageView) convertView
					.findViewById(R.id.ivAgree);
			viewItemHolder.ivComment = (ImageView) convertView
					.findViewById(R.id.ivComment);
			viewItemHolder.tvComment = (TextView) convertView
					.findViewById(R.id.tvComment);
			viewItemHolder.ivAgreeShow = (ImageView) convertView
					.findViewById(R.id.ivAgreeShow);
			viewItemHolder.tvAgreeShow = (TextView) convertView
					.findViewById(R.id.tvAgreeShow);
			viewItemHolder.btnComment = (Button) convertView
					.findViewById(R.id.btnComment);
			viewItemHolder.tvComments = (TextView) convertView
					.findViewById(R.id.tvComments);
			convertView.setTag(viewItemHolder);
		} else {
			viewItemHolder = (ViewItemHolder) convertView.getTag();
		}
		viewItemHolder.imgHead.setImageBitmap(BitmapFactory.decodeResource(
				context.getResources(), model.getImgHead()));
		viewItemHolder.tvName.setText(model.getName());
		viewItemHolder.tvDate.setText(model.getDate());
		viewItemHolder.tvContent.setText(model.getContent());
		if (model.getType() == FinalVar.MSG_IMAGE) {// 图片资源
			viewItemHolder.ivPhoto.setImageResource(R.drawable.pic_screen);
			viewItemHolder.ivPhoto.setVisibility(View.VISIBLE);
		} else {
			viewItemHolder.ivPhoto.setVisibility(View.GONE);
		}
		if (!model.getAddress().isEmpty()) {
			viewItemHolder.ivAddress.setVisibility(View.VISIBLE);
			viewItemHolder.tvAddress.setVisibility(View.VISIBLE);
			viewItemHolder.tvAddress.setText(model.getAddress());
		} else {
			viewItemHolder.ivAddress.setVisibility(View.GONE);
			viewItemHolder.tvAddress.setVisibility(View.GONE);
		}
		viewItemHolder.tvPhonemodel.setText(model.getPhonemodel());
		viewItemHolder.ivAgree
				.setOnClickListener(new ListViewButtonOnClickListener(position));
		if (model.isAgree()) {
			viewItemHolder.ivAgree
					.setImageResource(R.drawable.qzone_picviewer_bottom_praise_icon);
		} else {
			viewItemHolder.ivAgree
					.setImageResource(R.drawable.qzone_picviewer_bottom_unpraise_icon);
		}
		viewItemHolder.ivAgree.setFocusable(false);
		if (null != model.getAgreeShow() && model.getAgreeShow().size() > 0) {
			viewItemHolder.ivAgreeShow.setVisibility(View.VISIBLE);
			viewItemHolder.tvAgreeShow.setVisibility(View.VISIBLE);
			viewItemHolder.tvAgreeShow.setText(model.getAgreeShow().toString()
					+ "认为非常赞!

");
		} else {
			viewItemHolder.ivAgreeShow.setVisibility(View.GONE);
			viewItemHolder.tvAgreeShow.setVisibility(View.GONE);
		}
		viewItemHolder.ivComment
				.setOnClickListener(new ListViewButtonOnClickListener(position));
		viewItemHolder.ivComment.setFocusable(false);
		viewItemHolder.tvComment
				.setOnClickListener(new ListViewButtonOnClickListener(position));
		viewItemHolder.btnComment
				.setOnClickListener(new ListViewButtonOnClickListener(position));
		viewItemHolder.btnComment.setFocusable(false);
		if (null != model.getComments() && model.getComments().size() > 0) {
			viewItemHolder.tvComments.setVisibility(View.VISIBLE);
			String string = "";
			for (String comment : model.getComments()) {
				string += comment + "\n";
			}
			viewItemHolder.tvComments.setText(string);
		} else {
			viewItemHolder.tvComments.setVisibility(View.GONE);
		}
		return convertView;
	}

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

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listViewData.get(position);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (null == listViewData) {
			return 0;
		}
		return listViewData.size();
	}

	/**
	 * 加入一条记录
	 *
	 * @param model
	 */
	public void addModel(Model model) {
		listViewData.add(model);
	}

	/**
	 * 加入一条记录
	 *
	 * @param model
	 * @param insertHead
	 *            true:插入在头部
	 */
	public void addModel(Model model, boolean insertHead) {
		if (insertHead) {
			listViewData.add(0, model);
		} else {
			listViewData.add(model);
		}
	}

	/**
	 * 获取一条记录
	 *
	 * @param i
	 * @return
	 */
	public Model getModel(int i) {
		if (i < 0 || i > listViewData.size() - 1) {
			return null;
		}
		return listViewData.get(i);
	}

	/**
	 * 清除全部数据
	 */
	public void clear() {
		listViewData.clear();
	}

	class ViewItemHolder {
		ImageView imgHead;
		TextView tvName;
		TextView tvDate;
		TextView tvContent;
		ImageView ivPhoto;
		ImageView ivAddress;
		TextView tvAddress;
		ImageView ivAgree;
		TextView tvPhonemodel;
		ImageView ivComment;
		TextView tvComment;
		ImageView ivAgreeShow;
		TextView tvAgreeShow;
		Button btnComment;
		TextView tvComments;
	}

	class ListViewButtonOnClickListener implements OnClickListener {
		private int position;// 记录ListView中Button所在的Item的位置

		public ListViewButtonOnClickListener(int position) {
			this.position = position;
		}

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.ivAgree:
				ImageView ivAgree = (ImageView) v;
				Model model = listViewData.get(position);
				List<String> agreeShow = model.getAgreeShow();
				if (null == agreeShow || agreeShow.size() <= 0) {
					agreeShow = new ArrayList<String>();
				}
				if (model.isAgree()) {
					agreeShow.remove("我");
					ivAgree.setImageResource(R.drawable.qzone_picviewer_bottom_unpraise_icon);
				} else {
					agreeShow.add("我");
					ivAgree.setImageResource(R.drawable.qzone_picviewer_bottom_praise_icon);
				}
				model.setAgree(!model.isAgree());
				model.setAgreeShow(agreeShow);
				notifyDataSetChanged();
				// Toast.makeText(context, "你点了赞", Toast.LENGTH_SHORT).show();
				break;
			case R.id.ivComment:
			case R.id.tvComment:
			case R.id.btnComment:
				InputMethodManager imm = (InputMethodManager) v.getContext()
						.getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
				Model model1 = listViewData.get(position);
				String nikename = model1.getName();
				activity.findViewById(R.id.etComment).setVisibility(
						View.VISIBLE);
				activity.findViewById(R.id.btnSendComment).setVisibility(
						View.VISIBLE);
				((EditText) activity.findViewById(R.id.etComment)).setHint("@"
						+ nikename);
				activity.findViewById(R.id.etComment).setFocusable(true);
				activity.findViewById(R.id.btnSendComment).setOnClickListener(
						new ListViewButtonOnClickListener(position));
				break;
			case R.id.btnSendComment:
				Model mdl = listViewData.get(position);
				List<String> commentsList = mdl.getComments();
				String commentString = ((EditText) activity
						.findViewById(R.id.etComment)).getEditableText()
						.toString();
				if (null == commentsList || commentsList.size() <= 0) {
					commentsList = new ArrayList<String>();
				}
				commentsList.add(commentString);
				mdl.setComments(commentsList);
				notifyDataSetChanged();
				((EditText) activity.findViewById(R.id.etComment)).setText("");
				activity.findViewById(R.id.etComment).setVisibility(View.GONE);
				activity.findViewById(R.id.btnSendComment).setVisibility(
						View.GONE);
				InputMethodManager imm2 = (InputMethodManager) v.getContext()
						.getSystemService(Context.INPUT_METHOD_SERVICE);
				imm2.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
				break;
			default:
				break;
			}
		}
	}
}

整个项目的源码:https://github.com/nuptboyzhb/XListViewQzone

时间: 2024-07-28 18:40:38

Android:仿手机QQ好友动态的ListView的相关文章

vue-miniQQ——基于Vue2实现的仿手机QQ单页面应用(接入了聊天机器人,能够进行正常对话)

使用Vue2进行的仿手机QQ的webapp的制作,作品由个人独立开发,源码中进行了详细的注释. 由于自己也是初学Vue2,所以注释写的不够精简,请见谅. 项目地址 https://github.com/jiangqizheng/vue-MiniQQ 项目已实现功能 对话功能--想着既然是QQ总要能进行对话交流,所以在项目中接入了图灵聊天机器人,可以与列表中的每个人物进行对话. 左滑删除--左滑删除相关消息. 搜索页面--点击右上角搜索按钮,能够进入搜索页面,输入对应的单词或者数字,动态查找好友.

[Android]仿新版QQ的tab下面拖拽标记为已读的效果

以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4182929.html 可拖拽的红点,(仿新版QQ,tab下面拖拽标记为已读的效果),拖拽一定的距离可以消失回调.       GitHub:DraggableFlagView(https://github.com/wangjiegulu/DraggableFlagView) 实现原理: 当根据touch事件的移动,不断调用onDraw()方法进行刷新绘制. *

腾讯大牛动态教学:Android 仿微信 QQ 图片裁剪,赶紧收藏起来!

前言 在平时开发中,经常需要实现这样的功能,拍照 - 裁剪,相册 - 裁剪.当然,系统也有裁剪的功能,但是由于机型,系统兼容性等问题,在实际开发当中,我们通常会自己进行实现.今天,就让我们一起来看看怎样实现. 这篇博客实现的功能主要有仿微信,QQ 上传图像裁剪功能,包括拍照,从相册选取.裁剪框的样式有圆形,正方形,九宫格. 主要讲解的功能点 使用说明 整体的实现思路 裁剪框的实现 图片缩放的实现,包括放大,缩小,移动,裁剪等 我们先来看看我们实现的效果图 使用说明 有两种调用方式 第一种 第一种

android自定义View实现图片上传进度显示(仿手机QQ上传效果)

首先看下我们想要实现的效果如下图(qq聊天中发送图片时的效果): 再看一下我实现的效果: 1.效果已经看见了,下面我们来实现它.首先我创建一个android工程ProgressImageView.然后我们重写ImageView控件,创建ProcessImageView类代码如下: package com.example.processimageview; import android.annotation.SuppressLint; import android.content.Context;

android 根据滑动隐藏或显示导航 类似手机QQ好友个人信息

//重写ScrollView public class NotifyingScrollView extends ScrollView { /** * @author Cyril Mottier */ public interface OnScrollChangedListener { void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt); } private OnScrollChangedListener m

Android仿苹果版QQ下拉刷新实现(一) ——打造简单平滑的通用下拉刷新控件

前言: 因为公司人员变动原因,导致了博主四个月没有动安卓,一直在做IOS开发,如今接近年前,终于可以花一定的时间放在安卓上了.好了,废话不多说,今天我们要带来的效果是苹果版本的QQ下拉刷新.首先看一下目标效果以及demo效果:      因为此效果实现的步骤较多,所以今天博主要实现以上效果的第一步——打造一个通用的下拉刷新控件,具体效果如下: GIF图片比较大,还希望读者能耐心等待一下下从效果图中可以看出,我们的下拉刷新的滑动还是很流畅的,可能大多数开发者用的是XListview或者PullTo

wp8用LongListSelector做列表(可用QQ好友动态,微博列表各种地方),加入上拉加载

最近在做一个动态列表(想QQ空间那样),都没发现有现成的可以让我去捡,只能自己去搞了,发现用listbox实现有点困难,就换了LongListSelector,但是LongListSelector看到写的上拉加载不满意,就参考了几篇文章自己写了一个,来先看看做的效果 下面说说如何实现吧,xaml代码(LongListSelector样式的就不传了,只是调整他的UI): <local:ViewModel x:Key="viewmodel"/> <DataTemplate

Android 仿微信QQ聊天界面

一些IM聊天软件的展现形式是左右分开的形式.比如说,别人给你发的信息全部靠左显示,你自己发给别人的信息全部靠右显示. 而我们的ListView很多时候是显示同一个布局,其实BaseAdapter中有2个重要的方法在大多数情况下我们并未使用到,一个是public int getViewTypeCount(),显示ListView中有多少种布局(默认是显示是1),像微信那样聊天界面,是有2种布局方式:另外一个getItemViewType(),可以让不同item条目加载不同的布局,下面就简单的模拟下

仿手机QQ列表支持下拉,上滑,滑动删除

一般安卓程序员都知道下拉刷新主键用 com.handmark.pulltorefresh 网站:https://github.com/chrisbanes/Android-PullToRefresh/ 滑动删除用 fortysevendeg 的 swipelistview 但是要实现QQ列表功能,需要把两者叠加起来,我尝试了一下,发现有Bug, fortysevendeg的代码有点复杂,放弃了,后来自己搞了一个,为了方便广大 安卓程序员,我把整个demo贡献出来. 下载地址:http://pan