android仿超级课程表下课聊卡片listview
来源:互联网 发布:通联数据校招 编辑:程序博客网 时间:2024/05/15 20:34
listview是安卓应用中最常用的组件,比如好友列表,美团商家列表,微信朋友圈,超级课程表下课聊等 都有用到Listview,其中比较有意思的是卡片式的Listview,于是就来尝试着自己做一个。
先来看看下课聊的截图:
下面是自己实现的效果图,稍加修饰应该可以以假乱真
看起来感觉还不错,于是用http代理抓包把课表的json给抓下来了,给自己做测试数据使用:
至于抓包的话网上有很多安卓抓包的教程,有用adb的,也有将软件装进手机的。这些其实都可行,我个人比较喜欢代理抓包,那软件名叫Fiddler,具体使用方法不在本次技术范围内。
百度了下卡片ListView,一个比较有用的小提示网页:http://www.2cto.com/kf/201110/107462.html
新建项目后,在res/drawable文件夹新建一个 list_item_bg.xml,
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="6dp"/> <solid android:color="#fff"/></shape>
corners 是圆角的意思,radius是半径,顾名思义越大越圆。solid是背景颜色,可以自行定义。
看到这里已经成功一半了
现在再去主Activity里定义Listview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="获取内容" android:onClick="click" /> <ListView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/lv" android:padding="20px" android:dividerHeight="25px" android:divider="@android:color/transparent" ></ListView></LinearLayout>
为了让数据更加直观从json加载到类,定义一个bean实体对象:
package so.orion.Card;public class Card { String content ; int readCount ; long issueTime ; public Card( ) { } public Card(String content, int readCount, long issueTime) { this.content = content; this.readCount = readCount; this.issueTime = issueTime; } @Override public String toString() { return "Card [content=" + content + ", readCount=" + readCount + ", issueTime=" + issueTime + "]"; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public int getReadCount() { return readCount; } public void setReadCount(int readCount) { this.readCount = readCount; } public long getIssueTime() { return issueTime; } public void setIssueTime(long issueTime) { this.issueTime = issueTime; }}
有了listview,和数据实体,剩下的就是每一个item显示的方式了
每一个Item可以这么写,最外层用一个Linearlayout包裹,匿名发布和时间用一个相对布局RelativeLayout包裹,下面一个Textview,再最下面也是用一个相对布局,具体的直接看下面的代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/list_item_bg" android:orientation="vertical" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="60px" > <TextView android:layout_margin="10px" android:text="匿名发布" android:textSize="12sp" android:textColor="#92000000" android:layout_height="wrap_content" android:layout_width="wrap_content" /> <TextView android:layout_margin="10px" android:id="@+id/tv_time" android:textSize="12sp" android:textColor="#75000000" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentRight="true" android:text="晚上7:51" /> </RelativeLayout> <TextView android:layout_margin="10px" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="内容内容内容内容内容内容内容" android:id="@+id/tv_content" /> <RelativeLayout android:padding="10px" android:layout_width="fill_parent" android:layout_height="70px" android:background="#05000000" > <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="13sp" android:text="阅读(132)" android:layout_centerVertical="true" android:textColor="#0000ff" android:id="@+id/tv_readCount" /> <Button android:layout_height="60px" android:layout_width="100px" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:text="评论" android:textSize="13sp" android:background="@drawable/button_bg" android:id="@+id/bt_comment" android:layout_marginRight="10px" /> <Button android:layout_height="60px" android:layout_marginRight="10px" android:layout_width="100px" android:layout_centerVertical="true" android:layout_toLeftOf="@id/bt_comment" android:text="喝喝" android:textSize="13sp" android:background="@drawable/button_bg" android:id="@+id/bt_hehe" /> </RelativeLayout></LinearLayout>
至 此,显示界面就已经实现好了,剩下的就是将json数据加载到每一个item里。java中比较常用的json解析开源项目是谷歌出的一个gson,内部 利用反射机制随时可在类和json转换。Listview的适配器得自己重写,继承BaseAdapter就可以了,代码比较多,我就直接上源码了:
package so.orion.jsoncard;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.Random;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import so.orion.Card.Card;import so.orion.NetWork.HttpDownloader;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends Activity { protected static final int MSG_OK = 0; private ListView lv = null ; private MyAdapter myAdapter = null ; private List<Card> list ; private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { if( msg.what == MSG_OK){ lv.setAdapter(new MyAdapter()); } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); lv = (ListView) findViewById(R.id.lv); //lv.setAdapter(new MyAdapter()); } public void click( View view ){ new Thread(){ public void run() { String url = "http://172.16.111.XX/json.json"; String json = HttpDownloader.downloadTxt(url); //System.out.println(json); Gson gson = new Gson(); list = gson.fromJson(json,new TypeToken<List<Card>>(){}.getType()); System.out.println( list ); Message msg = new Message(); msg.what = MSG_OK ; handler.sendMessage(msg); }; }.start(); } public void init( ){ // Random random = new Random();// for( int i = 0 ; i <15 ; i ++){// Card card = new Card("内容内容内容内容内容内容内容内容"+i, "阅读("+random.nextInt(1036)+")" , System.currentTimeMillis());// System.out.println(card);// list.add(card); // } } public String getTime( long now ){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date = sdf.format(new Date(now)); return date; } public class MyAdapter extends BaseAdapter{ @Override public int getCount() { // TODO 自动生成的方法存根 return list.size(); } @Override public Object getItem(int position) { // TODO 自动生成的方法存根 return null; } @Override public long getItemId(int position) { // TODO 自动生成的方法存根 return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO 自动生成的方法存根 Card card = list.get(position); View view = View.inflate(MainActivity.this, R.layout.list_item, null); TextView tv_readCount = (TextView) view.findViewById(R.id.tv_readCount); TextView tv_content = (TextView) view.findViewById(R.id.tv_content); TextView tv_time = (TextView) view.findViewById(R.id.tv_time); tv_content.setText( card.getContent()); tv_readCount.setText( "阅读("+card.getReadCount()+")"); tv_time.setText( getTime( card.getIssueTime() ) ); return view; } }}
效果已经出来了:
转载请注明:乔布尸 » android仿超级课程表下课聊卡片listview
- android仿超级课程表下课聊卡片listview
- 仿超级课程表下拉刷新listview
- Android仿超级课程表的课程界面设计
- Android仿超级课程表的课程界面设计
- 仿超级课程表
- android超级课程表原理
- 实践--课程表(仿超级课程表展示课表)
- 手机端课程表的实现——仿超级课程表
- 仿超级课程表的课程界面设计
- 仿超级课程表的课程界面设计
- 简单方法实现仿超级课程表界面
- 简单方法实现仿超级课程表界面
- Android超级课程表实现原理
- android仿超级课程表页面自定义view的实现(方法一)
- GridLayout + Animation 实现 Android 仿超级课程表“发现”全屏宫格图标弹出动画
- 【android】项目案例(一)之超级课程表
- android学习:《超级课程表》的课表界面设计
- (仿超级课程表)结合MaterialSheetFab实现简单的课程表功能
- 前端神器推荐—我们也可以高效优雅的写代码
- kvo 简单笔记
- Android 进阶 - ActivieyManagerService简介
- MPEG相关标准
- spring的bean的创建
- android仿超级课程表下课聊卡片listview
- PHP连接MySQL
- 邻居好说话:冒泡排序
- 微软编程之美热身赛-传话游戏
- [JQuery]自定义表单序列化
- 字符编码笔记:ASCII,Unicode和UTF-8
- malloc是什么(数据结构)
- php配置memcache缓存方法
- Spark作为一套用Scala写成的分布式内存计算系统