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


相关文章
|
19天前
|
ARouter IDE 开发工具
Android面试题之App的启动流程和启动速度优化
App启动流程概括: 当用户点击App图标,Launcher通过Binder IPC请求system_server启动Activity。system_server指示Zygote fork新进程,接着App进程向system_server申请启动Activity。经过Binder通信,Activity创建并回调生命周期方法。启动状态分为冷启动、温启动和热启动,其中冷启动耗时最长。优化技巧包括异步初始化、避免主线程I/O、类加载优化和简化布局。
32 3
Android面试题之App的启动流程和启动速度优化
|
4天前
|
Android开发
Android面试题经典之如何全局替换App的字体
在Android应用中替换字体有全局和局部方法。全局替换涉及在`Application`的`onCreate`中设置自定义字体,并创建新主题。局部替换则可在布局中通过`ResourcesCompat.getFont()`加载字体文件并应用于`TextView`。
18 2
|
5天前
|
API Android开发
Android 监听Notification 被清除实例代码
Android 监听Notification 被清除实例代码
|
17天前
|
缓存 JSON 网络协议
Android面试题:App性能优化之电量优化和网络优化
这篇文章讨论了Android应用的电量和网络优化。电量优化涉及Doze和Standby模式,其中应用可能需要通过用户白名单或电池广播来适应限制。Battery Historian和Android Studio的Energy Profile是电量分析工具。建议减少不必要的操作,延迟非关键任务,合并网络请求。网络优化包括HTTPDNS减少DNS解析延迟,Keep-Alive复用连接,HTTP/2实现多路复用,以及使用protobuf和gzip压缩数据。其他策略如使用WebP图像格式,按网络质量提供不同分辨率的图片,以及启用HTTP缓存也是有效手段。
38 9
|
18天前
|
XML 监控 安全
Android App性能优化之卡顿监控和卡顿优化
本文探讨了Android应用的卡顿优化,重点在于布局优化。建议包括将耗时操作移到后台、使用ViewPager2实现懒加载、减少布局嵌套并利用merge标签、使用ViewStub减少资源消耗,以及通过Layout Inspector和GPU过度绘制检测来优化。推荐使用AsyncLayoutInflater异步加载布局,但需注意线程安全和不支持特性。卡顿监控方面,提到了通过Looper、ChoreographerHelper、adb命令及第三方工具如systrace和BlockCanary。总结了Choreographer基于掉帧计算和BlockCanary基于Looper监控的原理。
24 3
|
22天前
|
JavaScript 前端开发 Android开发
kotlin安卓在Jetpack Compose 框架下使用webview , 网页中的JavaScript代码如何与native交互
在Jetpack Compose中使用Kotlin创建Webview组件,设置JavaScript交互:`@Composable`函数`ComposableWebView`加载网页并启用JavaScript。通过`addJavascriptInterface`添加`WebAppInterface`类,允许JavaScript调用Android方法如播放音频。当页面加载完成时,执行`onWebViewReady`回调。
|
25天前
|
安全 Android开发 Kotlin
Android面试题之Kotlin的几种常见的类
这篇文章探讨了Kotlin编程语言中的初始化顺序、延迟初始化、惰性初始化、`lateinit`与`by lazy`的区别、初始化注意事项、继承、嵌套类、数据类、单例类和枚举类的使用,以及密封类的概念。文中通过示例代码详细解释了各种特性,并提醒读者关注初始化顺序和线程安全问题。同时,鼓励读者关注作者的公众号“AntDream”获取更多相关文章。
22 1
|
5天前
|
XML Android开发 数据格式
Android仿智联详情
Android仿智联详情
|
6天前
|
Web App开发 JavaScript 前端开发
Android端使用WebView注入一段js代码实现js调用android
Android端使用WebView注入一段js代码实现js调用android
20 0
|
16天前
|
Java Android开发 Kotlin
Android面试题:App性能优化之Java和Kotlin常见的数据结构
Java数据结构摘要:ArrayList基于数组,适合查找和修改;LinkedList适合插入删除;HashMap1.8后用数组+链表/红黑树,初始化时预估容量可避免扩容。SparseArray优化查找,ArrayMap减少冲突。 Kotlin优化摘要:Kotlin的List用`listOf/mutableListOf`,Map用`mapOf/mutableMapOf`,支持操作符重载和扩展函数。序列提供懒加载,解构用于遍历Map,扩展函数默认参数增强灵活性。
16 0

相关实验场景

更多