本文主要是介绍模拟新浪微博的随便看看栏目,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
做到如下图的效果:
首先对页面进行布局,在activity_sina_look.xml中的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="horizontal" android:background="#FFFFFF"><ListView android:id="@+id/listView"android:layout_width="fill_parent"android:layout_height="fill_parent"></ListView>
</RelativeLayout>
在list_item.xml这个新建的xml中的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<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="horizontal" ><!-- 左边图片部分 --><ImageViewandroid:id="@+id/photo"android:layout_width="48dp"android:layout_height="48dp" /><!-- 右边布局 --><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical" ><!-- 右上方 --><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><!-- 用户名 --><TextViewandroid:id="@+id/name"android:layout_width="wrap_content"android:layout_height="wrap_content" android:textColor="#000000"/><!-- 发布时间 --><TextViewandroid:id="@+id/time"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="right" android:textColor="#000000"/></LinearLayout><!-- 右下方发布内容 --><TextViewandroid:id="@+id/content"android:layout_width="fill_parent"android:layout_height="wrap_content" android:textColor="#000000"/></LinearLayout></LinearLayout>
最后,再在主要的Activity(SinaLookActivity.java)中编写代码如下:
package com.bzu.sinalook.activity;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.support.v4.app.NavUtils;public class SinaLookActivity extends Activity {private ListView listView;private List<Map<String, ?>> data;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_sina_look);listView = (ListView) this.findViewById(R.id.listView);data = getData();SimpleAdapter adapter = new SimpleAdapter(this, data,R.layout.list_item, new String[] { "photo", "name", "time","content" }, new int[] { R.id.photo, R.id.name,R.id.time, R.id.content });listView.setAdapter(adapter);//点击事件listView.setOnItemClickListener(new ListViewHandle());}/** 处理点击事件的方法*/private class ListViewHandle implements OnItemClickListener{public void onItemClick(AdapterView<?> adapter, View view, int position,long id) {Map<String, ?> item=data.get(position);Toast.makeText(getApplicationContext(),item.get("name").toString(), Toast.LENGTH_LONG).show();}}private List<Map<String, ?>> getData() {List<Map<String, ?>> data = new ArrayList<Map<String, ?>>();Map<String, Object> item = new HashMap<String, Object>();item.put("photo", R.drawable.p1);item.put("name", "仰望星空");item.put("time", "1分钟前");item.put("content", "好看!");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p2);item.put("name", "星星");item.put("time", "5分钟前");item.put("content", "今天真高兴!");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p3);item.put("name", "星空");item.put("time", "6分钟前");item.put("content", "电影好看!");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p4);item.put("name", "仰望");item.put("time", "17分钟前");item.put("content", "今天真高兴!");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p5);item.put("name", "仰望星空001");item.put("time", "18分钟前");item.put("content", "电影好看!");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p6);item.put("name", "仰望星空002");item.put("time", "19分钟前");item.put("content", "好漂亮!");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p7);item.put("name", "仰望大海");item.put("time", "22分钟前");item.put("content", "电视剧好看!");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p8);item.put("name", "够黯然才自然");item.put("time", "24分钟前");item.put("content", "章节好看!");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p9);item.put("name", "仰望星空006");item.put("time", "26分钟前");item.put("content", "你好看!");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p10);item.put("name", "仰望星空009");item.put("time", "29分钟前");item.put("content", "企鹅好看!");data.add(item);return data;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_sina_look, menu);return true;}}
这篇关于模拟新浪微博的随便看看栏目的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!