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

来源:互联网 发布:教孩子学编程python 编辑:程序博客网 时间:2024/06/07 00:40


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 {@Overrideprotected 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;}@Overridepublic 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);} elseholder = (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;elseROW = 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;}@Overridepublic Integer getItem(int pos) {return (Integer) mItems.get(pos);}@Overridepublic 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图标图片素材可以根据自己的情况选取。


运行结果如图所示:

0 0
原创粉丝点击