Android社交类APP动态详情代码实现通用模板

简介: Android社交类APP动态详情代码实现通用模板Android平台上一些比较流行的社交类APP比如微信、陌陌等,都有动态详情页,在该页面,用户发表的动态详情,好友可以发起评论、点赞等等。


Android社交类APP动态详情代码实现通用模板


Android平台上一些比较流行的社交类APP比如微信、陌陌等,都有动态详情页,在该页面,用户发表的动态详情,好友可以发起评论、点赞等等。这种设计在微信和陌陌上大同小异。我自己写了一个较为通用的模板,记下作为备忘和参考,更多更丰富的内容可据此深入定制和开发。
思路:整体是一个ListView实现,ListView添加一个header,作为该用户发送的动态详情呈现页面,然后在ListView下面的item里面是该用户的好友们发表的评论等等。
ListView下方是一个单独的布局,该布局放置一个EditText和Button,供发送评论。
先看代码运行的结果:


实现代码:
测试的主Activity:MainActivity.java:

package zhangphil.detail;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LayoutInflater mLayoutInflater = LayoutInflater.from(this);

		View view = View.inflate(this, R.layout.activity_main, null);

		ListView listView = (ListView) view.findViewById(android.R.id.list);

		// 每条评论的适配器
		ArrayAdapter adapter = new MyArrayAdapter(this, -1);
		listView.setAdapter(adapter);
		listView.setHeaderDividersEnabled(false);

		// ListView的头部
		View v = mLayoutInflater.inflate(R.layout.listview_head, null);
		listView.addHeaderView(v);

		TableLayout tableLayout = (TableLayout) v.findViewById(R.id.tableLayout);
		final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;

		// 随机生成若干张测试图片的量值。
		int total = new Random().nextInt(10);
		int ROW = 0;
		int mod = total % 3;
		if (mod == 0)
			ROW = total / 3;
		else
			ROW = total / 3 + 1;

		Context context = this;

		// 转换成ROW行3列的格式。
		int k = 0;
		for (int i = 0; i < ROW; i++) {
			TableRow tableRow = new TableRow(this);

			for (int j = 0; j < 3; j++) {
				if (k < total) {

					// 测试图片
					ImageView iv = new ImageView(context);
					iv.setImageResource(R.drawable.ic_launcher);

					tableRow.addView(iv);

					k++;
				}
			}

			tableLayout.addView(tableRow, new TableLayout.LayoutParams(WC, WC));
		}

		setContentView(view);
	}

	private class MyArrayAdapter extends ArrayAdapter {

		private LayoutInflater mLayoutInflater;

		public MyArrayAdapter(Context context, int resource) {
			super(context, resource);
			mLayoutInflater = LayoutInflater.from(context);
		}

		@Override
		public View getView(int pos, View convertView, ViewGroup parent) {
			if (convertView == null)
				convertView = mLayoutInflater.inflate(R.layout.comment_item, null);
			return convertView;
		}

		// 返回一个测试数据量值
		@Override
		public int getCount() {
			return 5;
		}
	}
}


MainActivity.java需要的布局文件:activity_main.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="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/commentLinearLayout"
        android:layout_gravity="center"
        android:scrollbars="none" />

    <LinearLayout
        android:id="@+id/commentLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#e0e0e0"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/commentEditText"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="8"
            android:hint="发表评论" />

        <Button
            android:id="@+id/buttonSend"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="发送" />
    </LinearLayout>

</RelativeLayout>



ListView头部布局用到的listview_head.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"
    android:background="#f5f5f5"
    android:padding="5dip"
    android:paddingBottom="20dip" >

    <ImageView
        android:id="@+id/imageViewHead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="5dip"
        android:adjustViewBounds="true"
        android:maxHeight="60dip"
        android:maxWidth="60dip"
        android:padding="1dip"
        android:src="@drawable/head" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dip"
        android:layout_toRightOf="@+id/imageViewHead"
        android:orientation="vertical"
        android:padding="1dip" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="1dip" >

            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerVertical="true"
                android:text="姓名某某某"
                android:textColor="@android:color/black"
                android:textSize="13sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_centerVertical="true"
                android:text="2015-09-16"
                android:textSize="9sp" />
        </RelativeLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:background="#EFEFEF"
            android:padding="1dip" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:text="标题" />

        <TextView
            android:id="@+id/detail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:text="一些文字,一些文字,一些文字,一些文字,一些文字,一些文字,一些文字,一些文字,一些文字,一些文字,一些文字,一些文字,一些文字,一些文字,一些文字,一些文字,一些文字"
            android:textSize="11sp" />

        <TableLayout
            android:id="@+id/tableLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:shrinkColumns="0,1,2" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="1dip" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:drawableLeft="@drawable/favorite"
                    android:gravity="bottom|right"
                    android:padding="1dip"
                    android:text="99+"
                    android:textSize="8sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:drawableLeft="@drawable/comment"
                    android:gravity="bottom|right"
                    android:padding="1dip"
                    android:text="99+"
                    android:textSize="8sp" />
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>


ListView适配器加载的每一个item的布局文件:comment_item.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"
    android:orientation="horizontal"
    android:padding="15dip" >

    <ImageView
        android:id="@+id/head"
        android:layout_width="30dip"
        android:layout_height="30dip"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/comment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="2dip"
        android:layout_toRightOf="@+id/head"
        android:gravity="center"
        android:padding="1dip"
        android:text="哎哟,不错哦!" />

    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="20:30" />

</RelativeLayout>


其中,图片素材head.png、comment.png、favorite.png可以自行根据喜好选取。


附我写的另外一篇与此相关的参考文章:
《Android社交类APP常用的动态消息发布通用模板》
文章链接:http://blog.csdn.net/zhangphil/article/details/48467309


相关文章
|
6月前
|
Java
照片一键生成眨眼视频app,手机照片一键生成眨眼动图,通过JAR代码实现效果
这是一个自动生成眨眼GIF动画的Java程序,包含主程序处理、图像变形和GIF生成三个模块。输入照片路径,自动识别人脸眼睛位置,生成闭眼、半闭眼等多帧图像,并合成为眨眼动效GIF文件。
|
7月前
|
Android开发 数据安全/隐私保护 开发者
Android自定义view之模仿登录界面文本输入框(华为云APP)
本文介绍了一款自定义输入框的实现,包含静态效果、hint值浮动动画及功能扩展。通过组合多个控件完成界面布局,使用TranslateAnimation与AlphaAnimation实现hint文字上下浮动效果,支持密码加密解密显示、去除键盘回车空格输入、光标定位等功能。代码基于Android平台,提供完整源码与attrs配置,方便复用与定制。希望对开发者有所帮助。
130 0
|
10月前
|
JavaScript 前端开发 Android开发
【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
347 13
【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
|
5月前
|
存储 Android开发 数据安全/隐私保护
Thanox安卓系统增加工具下载,管理、阻止、限制后台每个APP运行情况
Thanox是一款Android系统管理工具,专注于权限、后台启动及运行管理。支持应用冻结、系统优化、UI自定义和模块管理,基于Xposed框架开发,安全可靠且开源免费,兼容Android 6.0及以上版本。
440 4
|
6月前
|
消息中间件 缓存 小程序
婚恋交友相亲公众号app小程序系统源码「脱单神器」婚恋平台全套代码 - 支持快速二次开发
这是一套基于SpringBoot + Vue3开发的婚恋交友系统,支持微信公众号、Uniapp小程序和APP端。系统包含实名认证、智能匹配、视频相亲、会员体系等功能,适用于婚恋社交平台和相亲交友应用。后端采用SpringBoot 3.x与MyBatis-Plus,前端使用Vue3与Uniapp,支持快速部署和二次开发。适合技术团队或有经验的个人创业者使用。
440 8
|
5月前
|
小程序 Java 关系型数据库
圈子系统公众号app小程序系统源码圈子系统带即时通讯 多级圈子系统源码 兴趣小组系统开源 私密圈子系统代码 会员制社区系统
本圈子系统解决方案提供即时通讯、多级圈子、兴趣小组、私密社区及会员制管理功能。支持开源与商业方案,推荐ThinkSNS+、EasyClub及OpenFire等系统,并提供前后端技术选型建议,助力快速搭建社交平台。
296 0
|
8月前
|
数据采集 JSON 网络安全
移动端数据抓取:Android App的TLS流量解密方案
本文介绍了一种通过TLS流量解密技术抓取知乎App热榜数据的方法。利用Charles Proxy解密HTTPS流量,分析App与服务器通信内容;结合Python Requests库模拟请求,配置特定请求头以绕过反爬机制。同时使用代理IP隐藏真实IP地址,确保抓取稳定。最终成功提取热榜标题、内容简介、链接等信息,为分析热点话题和用户趋势提供数据支持。此方法也可应用于其他Android App的数据采集,但需注意选择可靠的代理服务。
328 11
移动端数据抓取:Android App的TLS流量解密方案
|
9月前
|
小程序 搜索推荐 Android开发
Axure原型模板与元件库APP交互设计素材(附资料)
Axure是一款强大的原型设计工具,广泛应用于APP和小程序的设计与开发。本文详细介绍Axure的常用界面组件元件库、交互设计素材,涵盖电商、社区服务、娱乐休闲、农业农村、教育等领域的多套交互案例。通过手机模型、矢量图标、通用组件等资源,设计师可高效构建原型并模拟用户操作,评估界面效果。Axure支持导出和分享,助力团队协作,推动更多优秀应用的诞生。
1032 6
|
10月前
|
缓存
flutter3-wetrip跨平台自研仿携程app预约酒店系统模板
基于最新跨平台框架flutter3.x+dart3+getx+flutter_datepicker纯手写实战的一款仿去哪儿/携程旅游酒店预约客房app系统。
385 7
|
2月前
|
移动开发 前端开发 Android开发
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
263 12
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡

热门文章

最新文章