标签:
1.逗比式
//逗比式............................................ //加载布局文件 //将xml文件转化为view,获取到view
//由于我们只需要将XML转化为View,并不涉及到具体的布局,所以第二个参数通常设置为null // View view = inflater.inflate(R.layout.item_buju,null); // // 实例化控件 // ImageView imageView = (ImageView) view.findViewById(R.id.img); // TextView text_title = (TextView) view.findViewById(R.id.title1); // TextView text_context = (TextView) view.findViewById(R.id.context1); // //给view中对应的每一个控件赋值 // imageView.setImageResource(data.get(position).itemImageID); // text_title.setText(data.get(position).itemTitle_ID); // text_context.setText(data.get(position).itemContext_ID); // return view; //逗比式...........................................
2.普通式
//普通式.......................................... //注册view中对应的每一个控件 // if(convertView == null){ // convertView = inflater.inflate(R.layout.item_buju,null); // } // ImageView imageView = (ImageView) convertView.findViewById(R.id.img); // TextView text_title = (TextView) convertView.findViewById(R.id.title1); // TextView text_context = (TextView) convertView.findViewById(R.id.context1); // //给view中对应的每一个控件赋值 // imageView.setImageResource(data.get(position).itemImageID); // text_title.setText(data.get(position).itemTitle_ID); // text_context.setText(data.get(position).itemContext_ID); // return convertView; //普通式..........................................
3.文艺式
//文艺式.......................................... //避免重复创建 如果存在缓存池中就不用创建 ViewHolder viewHolder; if(convertView == null){ viewHolder = new ViewHolder(); convertView = inflater.inflate(R.layout.item_buju,null); viewHolder.imageView = (ImageView) convertView.findViewById(R.id.img); viewHolder.title = (TextView) convertView.findViewById(R.id.title1); viewHolder.context = (TextView) convertView.findViewById(R.id.context1); convertView.setTag(viewHolder); } else{ viewHolder = (ViewHolder) convertView.getTag(); } //给view中对应的每一个控件赋值 viewHolder.imageView.setImageResource(data.get(position).itemImageID); viewHolder.title.setText(data.get(position).itemTitle_ID); viewHolder.context.setText(data.get(position).itemContext_ID); return convertView; //文艺式..........................................
MainActivity.java
public class MainActivity extends AppCompatActivity { private ListView listView; private List<MyItem> data; private Myadapter myadapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化 init(); } private void init() { listView = (ListView) findViewById(R.id.listview); //实例化 数据源 data = new ArrayList<>(); //加载到data List<MyItem> for(int i = 0;i < 200 ; i++){ data.add(new MyItem(android.R.drawable.sym_def_app_icon,"我是标题!"+i,"我是内容" + i)); } //实例化适配器 Myadapter myadapter =new Myadapter(data,MainActivity.this); //加载适配器 listView.setAdapter(myadapter); } }
Myadapter.java (自己创建的适配器 继承BaseAdapter)
public class Myadapter extends BaseAdapter{ private List<MyItem> data; private LayoutInflater inflater; public Myadapter(List<MyItem> data, Context context) { this.data = data; //获取上下文 this.inflater = LayoutInflater.from(context); } @Override public int getCount() { return data.size(); } @Override public Object getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { //逗比式............................................ //加载布局文件 //将xml文件转化为view,获取到view // View view = inflater.inflate(R.layout.item_buju,null); // //注册view中对应的每一个控件 // ImageView imageView = (ImageView) view.findViewById(R.id.img); // TextView text_title = (TextView) view.findViewById(R.id.title1); // TextView text_context = (TextView) view.findViewById(R.id.context1); // //给view中对应的每一个控件赋值 // imageView.setImageResource(data.get(position).itemImageID); // text_title.setText(data.get(position).itemTitle_ID); // text_context.setText(data.get(position).itemContext_ID); // return view; //逗比式........................................... //普通式.......................................... //注册view中对应的每一个控件 // if(convertView == null){ // convertView = inflater.inflate(R.layout.item_buju,null); // } // ImageView imageView = (ImageView) convertView.findViewById(R.id.img); // TextView text_title = (TextView) convertView.findViewById(R.id.title1); // TextView text_context = (TextView) convertView.findViewById(R.id.context1); // //给view中对应的每一个控件赋值 // imageView.setImageResource(data.get(position).itemImageID); // text_title.setText(data.get(position).itemTitle_ID); // text_context.setText(data.get(position).itemContext_ID); // return convertView; //普通式.......................................... //文艺式.......................................... //避免重复创建 如果存在缓存池中就不用创建 ViewHolder viewHolder; if(convertView == null){ viewHolder = new ViewHolder(); convertView = inflater.inflate(R.layout.item_buju,null); viewHolder.imageView = (ImageView) convertView.findViewById(R.id.img); viewHolder.title = (TextView) convertView.findViewById(R.id.title1); viewHolder.context = (TextView) convertView.findViewById(R.id.context1); convertView.setTag(viewHolder); } else{ viewHolder = (ViewHolder) convertView.getTag(); } //给view中对应的每一个控件赋值 viewHolder.imageView.setImageResource(data.get(position).itemImageID); viewHolder.title.setText(data.get(position).itemTitle_ID); viewHolder.context.setText(data.get(position).itemContext_ID); return convertView; //文艺式.......................................... } //View的容器 用class来封装 class ViewHolder{ public ImageView imageView; public TextView title; public TextView context; } }
ListView<Myitem>中每一个item布局,Myitem_buju.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/img" android:background="@android:drawable/sym_def_app_icon" android:layout_width="60dp" android:layout_height="60dp" /> <TextView android:id="@+id/title1" android:text="Title" android:textSize="25sp" android:gravity="center" android:layout_toEndOf="@+id/img" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/context1" android:text="Context" android:textSize="20sp" android:gravity="center_vertical" android:layout_toEndOf="@+id/img" android:layout_below="@+id/title1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout>
ListView<Myitem>中每一个item类,Myitem.class:
package com.example.administrator.ten_88; /** * Created by Administrator on 2015/10/8 0008. */ public class MyItem { public int itemImageID; public String itemTitle_ID; public String itemContext_ID; public MyItem(int itemImageID, String itemTitle_ID, String itemContext_ID) { this.itemImageID = itemImageID; this.itemTitle_ID = itemTitle_ID; this.itemContext_ID = itemContext_ID; } }
标签:
原文地址:http://www.cnblogs.com/zmaibbs7/p/4861856.html