仿微博看看程序

最近学习了listview,有一些心得,希望能帮到你们。






1.代码如下:

MainActivity.java

<span style="font-size:18px;">package com.example.weibokankan;

import java.util.ArrayList;
import java.util.List;

import com.example.weibokankan.weiboAdapter;
import com.example.weibokankan.MainActivity;
import com.example.weibokankan.R;
import com.example.weibokankan.weibo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MainActivity extends Activity {
	private ListView lvweibo;
    private List<weibo> weibolist=new ArrayList<weibo>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
//		建立数据源
		initweibos();
//		创建Adapter
		lvweibo = (ListView) findViewById(R.id.lv);
		weiboAdapter adapter=new weiboAdapter(this, R.layout.kankan_item,weibolist);
		lvweibo.setAdapter(adapter);
		lvweibo.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position,
					long id) {
				// TODO Auto-generated method stub
				 weibo wb=	weibolist.get(position);
				    Toast.makeText(MainActivity.this, wb.getName(), Toast.LENGTH_SHORT).show();
			}
		});
	}
    
 private void initweibos(){
	weibo webo1=new weibo("娃娃脸",R.drawable.p1,"啦啦啦啦啦啦啦啦啦,德玛西亚!!!!!");
	weibolist.add(webo1);
	weibo webo2=new weibo("你是猪吗",R.drawable.p2,"人生途中,有些是无法逃避的,比如命运;有些是无法更改的,比如情缘;有些是难以磨灭的,比如记忆;有些是难以搁置的,比如爱恋,与其被动地承受,不如勇敢地面对!");
	weibolist.add(webo2);
	weibo webo3=new weibo("你好,2015",R.drawable.p3,"人总是害怕改变,因为改变会带来一份陌生。活在过去的人没有勇气面对陌生——-但人生只有一个方向,就是向前走,就是踏入未来。在陌生里才有新的机会,大步朝前吧。");
	weibolist.add(webo3);
	weibo webo4=new weibo("扯",R.drawable.p4,"啦啦啦拉拉啊,路阿拉拉阿拉啦啦啦啦啦啦啦啦了");
	weibolist.add(webo4);
	weibo webo5=new weibo("星湖像花儿一样",R.drawable.p5,"一朵花的凋零,荒芜不了整个春天,一次挫折也荒废不了整个人生。");
	weibolist.add(webo5);
	weibo webo6=new weibo("等",R.drawable.p6,"挫折是一块石头,对于弱者来说它是拌脚石,让你停步不前。而对于强者来说它是垫脚石,使你站得更高。");
	weibolist.add(webo6);
	weibo webo7=new weibo("夜雨潇湘",R.drawable.p7,"所有杀不死你的,都会使你强大。");
	weibolist.add(webo7);
	weibo webo8=new weibo("潇湘夜雨",R.drawable.p8,"人生途中,有些是无法逃避的,比如命运;有些是无法更改的,比如情缘;有些是难以磨灭的,比如记忆;有些是难以搁置的,比如爱恋,与其被动地承受,不如勇敢地面对!");
	weibolist.add(webo8);
	weibo webo9=new weibo("浅笑如昔",R.drawable.p9,"啦啦啦啦啦啦啦啦啦,德玛西亚!!!!!!");
	weibolist.add(webo9);
	weibo webo10=new weibo("潇湘夜雨",R.drawable.p10,"啦啦啦啦啦啦啦啦啦,德玛西亚!!!!!!");
	weibolist.add(webo10);
 }

}</span>


2.weibo.java

<span style="font-size:18px;">package com.example.weibokankan;

public class weibo {
	private String name;
	private int imageId;
	private String say;
	public weibo(String name, int imageId, String say) {
		super();
		this.name = name;
		this.imageId = imageId;
		this.say=say;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getImageId() {
		return imageId;
	}
	public void setImageId(int imageId) {
		this.imageId = imageId;
	}
	public String getSay() {
		return say;
	}
	public void setSay(String say) {
		this.say = say;
	}

}
</span>


3.weiboAdapter.java


<span style="font-size:18px;">package com.example.weibokankan;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.weibokankan.R;
import com.example.weibokankan.weibo;

public class weiboAdapter extends ArrayAdapter<weibo> {
	private int resourceId;
	public weiboAdapter(Context context, int textViewResourceId,
			List<weibo> data) {
		super(context, textViewResourceId, data);
		resourceId=textViewResourceId;
	}

	// 这个方法在每个子项被滚动到屏幕内的时候会被调用
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		weibo wb = getItem(position);
		View view=LayoutInflater.from(getContext()).inflate(resourceId, null);
		ImageView ivFruit=(ImageView) view.findViewById(R.id.ivimage);
		TextView tvweibo=(TextView) view.findViewById(R.id.tv);
		TextView tvweibosay=(TextView) view.findViewById(R.id.tvsay);
		ivFruit.setImageResource(wb.getImageId());
		tvweibo.setText(wb.getName());
		tvweibosay.setText(wb.getSay());
		return view;
	}





	

}
</span>


4.activity_main


<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout></span>


5.kankan_item


<span style="font-size:18px;"><?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="wrap_content"
    android:orientation="horizontal" >
   <ImageView
        android:id="@+id/ivimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:src="@drawable/p1" />
   <RelativeLayout
    android:id="@+id/rl" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    
    >
    <TextView
        android:id="@+id/tv"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:text="Apple" /> 

     <TextView
         android:id="@+id/tvsay"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_below="@id/tv"
         android:layout_alignLeft="@id/tv"
         android:text="1fdggwhvgtrryvbxcvffrigoiggkgfijijkmkvmgiovmgiomkom iogm" />

    </RelativeLayout>
</LinearLayout></span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值