实现思路:
1、获取id
2、获取数据
3、给id设置适配器(Adapter)
4、给id设置点击事件,循环1-1000
5、自定义适配器(内存溢出)
(1)把布局文件转成view
(2)设值
(3)给按钮设置点击事件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zking.y2_android0609_widgetplus2.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv_main_list"
></ListView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/iv_item_icon"
android:src="@drawable/default_ptr_flip"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/tv_item_title"
android:text="XX"
android:textSize="30sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载"
android:id="@+id/btn_item_download"
/>
</LinearLayout>
public class ItemTag {
public ImageView imageView;
public TextView textView;
public Button button;
}
package com.zking.y2_android0609_widgetplus2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private int[] images=new int[]{R.drawable.bird,R.drawable.cat,R.drawable.elephant,R.drawable.chicken};
private String[] titles= new String[]{"李思佳","刘再融","唐嫣","刘恺威"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.lv_main_list);
//获取数据
//实例化SimpleAdapter
listView.setAdapter(new MyAdapter());
//给ListView设置点击事件 //i%titles.length为了要循环数据0-9下标1-10000
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, "跳转到"+titles[i%titles.length]+"页面",Toast.LENGTH_SHORT).show();
}
});
}
//OOM OutOfMemory 内存溢出
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return 10000; //多少条数据
}
@Override
public Object getItem(int i) {
return titles[i%titles.length]; //titles[i]代表获得titles里面每个值
}
@Override
public long getItemId(int i) { //下标
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
Log.i("test","i="+i+" "+view);
//把布局文件转成View
if(view==null){ //同一时间最多能看到14条数据,有14个null
view= getLayoutInflater().inflate(R.layout.item_listview,null);
ItemTag itemTag=new ItemTag();
itemTag.imageView= (ImageView) view.findViewById
(R.id.iv_item_icon);
itemTag.textView= (TextView) view.findViewById
(R.id.tv_item_title);
itemTag.button= (Button) view.findViewById
(R.id.btn_item_download);
view.setTag(itemTag);
}
ItemTag itemtag= (ItemTag) view.getTag();
//设值
itemtag.imageView.setImageResource(images[i%titles.length]);
itemtag.textView.setText(titles[i%titles.length]+i);
//给按钮设置点击事件
itemtag.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "正在下载"+titles[i%titles.length],
Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
}