Android社交类APP常用的动态消息发布通用模板



Android社交类APP常用的动态消息发布通用模板

我写的一个Android社交类APP常用的动态消息发布模板,作为备忘和参考,在此记下。
社交类APP用户通常会发布一些信息(一般考虑装载到Android的ListView),每一条信息一般有图(ImageView)和文(TextView)组成。需要注意的是,每一条消息,有些有图,有些没有图只有文字,因此,在装载图片时候我使用TableLayout动态加载。
现在给出实现的全部源代码。


测试的主Activity MainActivity.java:

package zhangphil.weixin;

import java.util.ArrayList;
import java.util.Random;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils.TruncateAt;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends ListActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 随机生成一些测试数据量。
		Random rand = new Random();
		int cnt = 0;
		ArrayList<Integer> mArrayList = new ArrayList<Integer>();
		for (int i = 0; i < 50; i++) {
			cnt = rand.nextInt(10);
			mArrayList.add(cnt);
		}

		ArrayAdapter adapter = new MyArrayAdapter(this, -1, mArrayList);
		setListAdapter(adapter);

		// 隐藏Android ListView自带的分割线.
		getListView().setDivider(null);
	}

	private class MyArrayAdapter extends ArrayAdapter {

		private ArrayList<Integer> mItems;
		private LayoutInflater mLayoutInflater;
		private ViewHolder holder;
		private Context context;

		private class ViewHolder {
			public TableLayout tableLayout;
			public TextView title;
			public TextView detail;
		}

		public MyArrayAdapter(Context context, int resource, ArrayList<Integer> mArrayList) {
			super(context, resource);
			this.mLayoutInflater = LayoutInflater.from(context);
			this.mItems = mArrayList;
			this.context = context;
		}

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

				TextView title = (TextView) convertView.findViewById(R.id.title);
				TextView detail = (TextView) convertView.findViewById(R.id.detail);
				TableLayout tableLayout = (TableLayout) convertView.findViewById(R.id.tableLayout);

				holder = new ViewHolder();

				holder.title = title;
				holder.detail = detail;
				holder.tableLayout = tableLayout;

				holder.detail.setEllipsize(TruncateAt.END);
				holder.detail.setMaxLines(3);

				convertView.setTag(holder);
			} else
				holder = (ViewHolder) convertView.getTag();

			holder.title.setText("标题要长 (" + pos + ")," + mItems.get(pos) + "图");

			int total = getItem(pos);

			if (total == 0) {
				holder.tableLayout.setVisibility(View.GONE);
			} else {
				holder.tableLayout.removeAllViewsInLayout();
				holder.tableLayout.setVisibility(View.VISIBLE);

				int ROW = 0;
				int mod = total % 3;
				if (mod == 0)
					ROW = total / 3;
				else
					ROW = total / 3 + 1;

				int k = 0;
				for (int i = 0; i < ROW; i++) {
					TableRow tableRow = new TableRow(getContext());

					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++;
						}
					}

					holder.tableLayout.addView(tableRow, new TableLayout.LayoutParams(
							ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
				}
			}

			return convertView;
		}

		@Override
		public Integer getItem(int pos) {
			return (Integer) mItems.get(pos);
		}

		@Override
		public int getCount() {
			return mItems.size();
		}
	}
}


MainActivity.java中ListView需要的item布局文件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: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="zhangphil"
                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-15"
                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="999+"
                    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="999+"
                    android:textSize="8sp" />
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

其中item布局文件中的头像head、favorite、comment图标图片素材可以根据自己的情况选取。


运行结果如图所示:

  • 1
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
用例名称:发布动态 用例描述:乡村宣传APP的用户可以发布各种类型的动态,如文字、图片、视频等,来展示自己所在的乡村生活、文化、风景等方面的内容,吸引更多人关注和了解该乡村。 参与者:乡村宣传APP的用户 前置条件: 1. 用户已经成功登录乡村宣传APP。 2. 用户已经进入了发布动态的页面,该页面包含了文本框、图片上传、视频上传等元素。 3. 用户已经选择了要发布动态类型,即文字、图片、视频等。 主流程: 1. 用户填写动态的文本内容,包括标题和正文,并选择发布的类型。 2. 如果用户选择了图片或视频类型,则点击上传按钮,选择要上传的图片或视频文件,并进行上传。 3. 用户可以选择是否公开该动态,若选择不公开,则只有好友可以查看。 4. 用户点击“发布”按钮,将动态发布到乡村宣传APP上。 5. 系统提示用户发布成功,并将动态展示在用户的个人主页上。 6. 用户可以选择分享该动态社交媒体平台,如微信、微博等,以便让更多人了解该乡村。 扩展流程: 1. 若用户输入的文本内容为空,则系统提示用户文本内容不能为空。 2. 若用户选择的图片或视频文件大小超出了系统限制,则系统提示用户上传的文件过大,需要缩小文件大小后再上传。 3. 若用户选择的图片或视频格式不支持,则系统提示用户选择其他格式的文件。 4. 若用户选择的动态类型不符合规定,则系统提示用户选择其他类型的动态。 5. 若用户选择了不公开该动态,则系统提示用户该动态只能被好友查看。 6. 若用户发布动态内容违反了相关法律法规或社交媒体平台的规定,则系统会对该动态进行审核,并决定是否删除该动态

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值