Android:仿手机QQ朋友动态ListView
1.介绍:
使用此博客XListView模仿Android版本QQ朋友动态ListView效果。效果如下面的截图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbnVwdDEyMzQ1Njc4OQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
效果图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: zhb931706659@126.com
*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
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Android:仿手机QQ朋友动态ListView的更多相关文章
- Android:仿手机QQ好友动态的ListView
1.介绍: 本博客使用XListView模仿Android版QQ好友动态的ListView效果.效果截图例如以下: 效果图1 watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...
- Android仿苹果版QQ下拉刷新实现(二) ——贝塞尔曲线开发"鼻涕"下拉粘连效果
前言 接着上一期Android仿苹果版QQ下拉刷新实现(一) ——打造简单平滑的通用下拉刷新控件 的博客开始,同样,在开始前我们先来看一下目标效果: 下面上一下本章需要实现的效果图: 大家看到这个效果 ...
- vue-miniQQ——基于Vue2实现的仿手机QQ单页面应用(接入了聊天机器人,能够进行正常对话)
使用Vue2进行的仿手机QQ的webapp的制作,作品由个人独立开发,源码中进行了详细的注释. 由于自己也是初学Vue2,所以注释写的不够精简,请见谅. 项目地址 https://github.com ...
- 腾讯技术分享:Android版手机QQ的缓存监控与优化实践
本文内容整理自公众号腾讯Bugly,感谢原作者的分享. 1.问题背景 对于Android应用来说,内存向来是比较重要的性能指标.内存占用过高,会影响应用的流畅度,甚至引发OOM,非常影响用户体验.因此 ...
- 高仿手机QQ音乐之——Android带进度条的开关
最新版的手机QQ音乐体验确实不错,发现首页播放按钮能够显示歌曲当前进度条.认为挺有新意.效果例如以下: 自己琢磨了下.能够用自己定义组件来实现,试着做了一下.效果例如以下: 整理了下思路.大概设计流程 ...
- Android仿微信QQ等实现锁屏消息提醒
demo代码如下: import android.content.Intent; import android.os.Bundle; import android.support.v7.app.App ...
- [Android]仿新版QQ的tab下面拖拽标记为已读的效果
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4182929.html 可拖拽的红点,(仿新版QQ,tab下面拖 ...
- Android之高仿手机QQ聊天
源代码下载 转载请注明出处,谢谢! 最终版已上传.优化下拉刷新.增加来消息声音提示.主界面改成ViewPager,实现左右滑动.新增群组.最近会话显示条数,开始上班了,不再修改了.谢谢! 国庆这几天, ...
- Android仿支付宝扣款顺序,动态改变ListView各Item次序
前言:今天遇到个需求,需要让用户动态选择语音传输方式的次序,突然想起支付宝选择扣款顺序的功能,恰好能满足需要,就花了点时间写了个demo,在此权当学习记录 先上效果图 支付宝的效果 demo的效果 思 ...
随机推荐
- 汉诺塔问题的java递归实现
import java.util.Scanner; public class Hanoi { int count=0; public void hanoi(int n,char A,char B,ch ...
- Windows8.1和Ubuntu14.04双系统卸载Ubuntu參考教程[图]
我之前编写并公布了<Windows8.1安装Ubuntu14.04双系统參考教程及双硬盘注意事项>这篇教程,而本文提供的卸载Ubuntu方法适用于这篇教程,其它方法安装的Ubuntu可适当 ...
- EJB学习笔记
1 J2ee概述 J2ee是企业级的计算平台,它为分布式和基于组件的软件开发提供了一个“操作系统” Ant是什么工具?? EJB: 什么是EJB? 是一种server端组件结构,简化了开发分布式企 ...
- POJ 1984 Navigation Nightmare (数据结构-并检查集合)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4072 Accepted: 1 ...
- Oracle中merge into的使用 (转)
该命令使用一条语句从一个或者多个数据源中完成对表的更新和插入数据. ORACLE 9i 中,使用此命令必须同时指定UPDATE 和INSERT 关键词,ORACLE 10g 做了如下改动. 1.ins ...
- Android 推断SD卡是否存在及容量查询
首先要在AndroidManifest.xml中添加SD卡訪问权限 <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name= ...
- C#二进制字节数组操作功能 拦截字节数组SubByte
C#二进制字节数组操作功能 拦截字节数组SubByte /// <summary> /// 截取字节数组 /// </summary> /// <param name=& ...
- MySQL存储过程:用户授权量
写这些脚本需求放缓的调查记录到数据库,方便观看. 1. 因为默认mysql.slow_log表使用csv数据引擎,该数据不支持指数,因此,有必要改变MyISAM发动机.和query_time字段索引, ...
- PowerMockito使用详解(转)
一.为什么要使用Mock工具 在做单元测试的时候,我们会发现我们要测试的方法会引用很多外部依赖的对象,比如:(发送邮件,网络通讯,远程服务, 文件系统等等). 而我们没法控制这些外部依赖的对象,为了解 ...
- nodejs 模拟form表单上传文件
使用nodejs来模拟form表单进行文件上传,可以同时上传多个文件. 以前项目里有这个方法,最近在客户那里出问题了,同事说,这个方法从来就没管用过,SO,用了一天时间把这个方法给搞出来了(觉得花费的 ...