模拟新浪微博随便看看(Android)

来源:互联网 发布:淘宝消费才送优惠券 编辑:程序博客网 时间:2024/06/06 02:14

一   创建一个模板显示内容,并在主Activity中创建一个ListView。该模板自定义内容显示的格式。想要显示的内容都将按该模板的格式显示。

1.布局文件(模板)

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dip" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/ItemIv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginTop="5dip"
                android:src="@drawable/p1" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="7dip"
                android:layout_toRightOf="@+id/ItemIv"
                android:orientation="vertical" >

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/ItemTvName"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:text="花花"
                        android:textColor="#FF3333"
                        android:textSize="16sp"
                        android:textStyle="bold"
                        android:typeface="sans" />
                </RelativeLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dip" >

                    <TextView
                        android:id="@+id/ItemTvInfo"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:minHeight="50px"
                        android:text="不要拿小人的错误来惩罚自己,不要在这些微不足道的事情上折磨浪费自己的宝贵时间"
                  
                        android:textSize="12sp" />
                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>

2.MainActivity布局文件

  <include layout="@layout/activity_title"/>
    <ListView
        android:id="@+id/Lv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

二   创建实体类,自定义编辑的属性。创建一个类,使得显示的属性与各组件链接。

1.实体类

public class ListItem {
 private int imageUrl;
 private String name;
 private String info;
 public ListItem(int imageUrl,String namestr,String infostr){
  this.imageUrl=imageUrl;
  this.name=namestr;
  this.info=infostr;
 }
 public int getImageUrl(){
  return imageUrl;
 }
 public String getinfo(){
  return info;
 }
 public String getName(){
  return name;
 }
}

2.链接类

public class ViewCache {
 private View baseView;
 private TextView textViewname;
 private TextView textViewinfo;
 private ImageView imageView;
 public ViewCache(View baseView){
  this.baseView=baseView;
 }
 public TextView getnameTextView(){
  if(textViewname==null){
   
   textViewname=(TextView)baseView.findViewById(R.id.ItemTvName);
   
  }
  return textViewname;
  
 }
 public TextView getinfoTextView(){
  if(textViewinfo==null){
   textViewinfo=(TextView)baseView.findViewById(R.id.ItemTvInfo);
   
  }
  return textViewinfo;
 }
 public ImageView getImageView(){
  if(imageView==null){
   imageView=(ImageView)baseView.findViewById(R.id.ItemIv);
   
  }
  return imageView;
 }

三   自定义Adapter(适配器),并在MainActicity中调用。

1.自定义Adapter

public class ListViewAdapter extends ArrayAdapter<ListItem> {
 private ListView listview;
 public ListViewAdapter(Activity activity,List<ListItem> imageAndTexts,ListView listView) {
  super(activity,0,imageAndTexts);
  this.listview=listView;
 }
 public View getView(int position, View convertView, ViewGroup parent){
  Activity activity=(Activity)getContext();
  View rowView=convertView;
  ViewCache viewCache;
  if(rowView==null){
   LayoutInflater inflater=activity.getLayoutInflater();
   rowView=inflater.inflate(R.layout.activity_lostviewitem, null);
   viewCache=new ViewCache(rowView);
   rowView.setTag(viewCache);
  }
  else{
   viewCache=(ViewCache) rowView.getTag();
  }
  
  ListItem imageAndText=getItem(position);
  ImageView imageView=viewCache.getImageView();
  imageView.setImageResource(imageAndText.getImageUrl());
  TextView textViewname=viewCache.getnameTextView();
  textViewname.setText(imageAndText.getName());
     TextView textViewinfo=viewCache.getinfoTextView();
     textViewinfo.setText(imageAndText.getinfo());
    return rowView; 
 }

2.MainActivity类

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Integer[] imagesIds={R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,R.drawable.p6,R.drawable.p7,R.drawable.p8,R.drawable.p9,R.drawable.p10};
        String[] name={"牵我手","带我走","嘿嘿","哈哈","呵呵","嘻嘻","啦啦","呼呼","忽忽","呼呼"};
        String[] infostr={"生活是一面镜子。你对它笑,它就对你笑;你对它哭,它也对你哭。","活着一天,就是有福气,就该珍惜。当我哭泣我没有鞋子穿的时候,我发现有人却没有脚","人生是个圆,有的人走了一辈子也没有走出命运画出的圆圈,其实,圆上的每一个点都有一条腾飞的切线。","千万别迷恋网络游戏,要玩就玩好人生这场大游戏。","命运负责洗牌,但是玩牌的是我们自己!",
          "我们心中的恐惧,永远比真正的危险巨大的多",
          "命运掌握在自己手中。要么你驾驭生命,要么生命驾驭你,你的心态决定你是坐骑还是骑手。",
          "宁愿做过了后悔,也不要错过了后悔。",
          "不要拿小人的错误来惩罚自己,不要在这些微不足道的事情上折磨浪费自己的宝贵时间",
          "如果我们都去做自己能力做得到的事,我们会让自己大吃一惊。"};
        ListView listview=(ListView)findViewById(R.id.Lv);
        List<ListItem> list= new ArrayList<ListItem>();
        for(int i=0;i<name.length;i++){
         list.add(new ListItem(imagesIds[i],name[i],infostr[i])); 
        }
        listview.setAdapter(new ListViewAdapter(this,list,listview));
    }

四.  最后程序截图


通过这个程序可以增加对于Android中LisView的掌握与使用。同时加深了对于Adapter的理解与掌握。

0 0
原创粉丝点击