码迷,mamicode.com
首页 > 其他好文 > 详细

模拟新浪微博随便看看栏目

时间:2015-05-04 23:40:51      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

使用ListView来模仿微博随便看看栏

 

【分析】

 

要完成这个效果,我们需要:

 

1.ListView及ListView_Item

 

2.实体类的编写

 

3.自定义适配器的书写

 

4.ListView的绑定数据源与控件

 

【编码】

package cn.edu.bzu.adapter;
 
import java.util.List;
 
import cn.edu.bzu.entity.Information;
import cn.edu.bzu.weibo.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
/**
 * 自定义Adapter实现listView的显示
 * @author monster
 * @date : 2015-04-28
 */
public class MyAdapter extends BaseAdapter {
    private List<Information> list;
    private Context mContext;
    private LayoutInflater mInflater;
    //定义构造方法
    public MyAdapter(List<Information> list, Context mContext) {
        super();
        this.list = list;
        this.mContext = mContext;
    }
 
    @Override
    public int getCount() {
        return list.size();
    }
 
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            convertView=mInflater.from(mContext).inflate(R.layout.listview_item,null);  //布局转换成视图
            holder=new ViewHolder();
            //实例化控件
            holder.Image=(ImageView)convertView.findViewById(R.id.Iv);
            holder.Name=(TextView)convertView.findViewById(R.id.Name);
            holder.Time=(TextView)convertView.findViewById(R.id.Time);
            holder.Contents=(TextView)convertView.findViewById(R.id.Contents);
            convertView.setTag(holder); //设置setTag使得程序得以优化
        }else{
            holder=(ViewHolder) convertView.getTag();
        }
        //给控件赋值
        holder.Image.setBackgroundResource(list.get(position).getImgId());
        holder.Name.setText(list.get(position).getName());
        holder.Time.setText(list.get(position).getDate());
        holder.Contents.setText(list.get(position).getContent());
        return convertView;
    }
     
    class ViewHolder{
        ImageView Image;
        TextView  Name;
        TextView  Time;
        TextView  Contents;
    }
}

MainActivity

 

package cn.edu.bzu.weibo;
 
import java.util.ArrayList;
import java.util.List;
 
import cn.edu.bzu.adapter.MyAdapter;
import cn.edu.bzu.entity.Information;
import android.os.Bundle;
import android.widget.ListView;
import android.app.Activity;
 
public class MainActivity extends Activity {
    private ListView listView;
    private MyAdapter adapter;
    private List<Information> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setData();//数据源
         initView();
    }
    private void initView() {
        // 初始化控件
        listView=(ListView) findViewById(R.id.Lv);
        listView.setAdapter(adapter);  //绑定适配器
    }
    private void setData() {
        // 设置数据源
         list=new ArrayList<Information>();
        list.add(new Information(R.drawable.img1,"孙浩","2015-05-02","好好学习"));
        list.add(new Information(R.drawable.img2, "华华", "2015-05-02", "天天向上"));
        list.add(new Information(R.drawable.img3,"天天","2015-05-02","五一来了呀"));
        list.add(new Information(R.drawable.img4,"小云","2015-05-02","五一放假啦"));
        list.add(new Information(R.drawable.img5,"小雨","2015-05-02","开学开学了"));
        list.add(new Information(R.drawable.img6, "小张", "2015-05-02", "啦啦啦"));
        list.add(new Information(R.drawable.img7, "小新", "2015-05-02", "今天和闺蜜去超市,买了好多好多好吃的##"));
        adapter=new MyAdapter(list, MainActivity.this);
    }          
}
----->>这次实验让我掌握了ListView 控件的使用理解了 Adapter 的作用并掌握自定义 FruitAdapter 的使用方式

 

模拟新浪微博随便看看栏目

标签:

原文地址:http://www.cnblogs.com/sh791165672/p/4477864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!