1.新建一个布局里面写
<ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/list_main_a"> </ListView>
2.再新建一个布局模型
<ImageView android:layout_width="30dp" android:layout_height="30dp" android:id="@+id/Iv_main_a" android:src="@drawable/icon_user"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_main_a" android:textSize="30sp" android:layout_weight="1" android:text="XX"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt_main_a" android:text="下载" />在布局里加上一句代码可以点击任何一个地方,提供给点击事件用android:descendantFocusability="blocksDescendants"
布局模型效果如下:第一一个实体类参数如下:public ImageView imageView; public TextView textView; public Button button;3.首先接收idlv_main_a= (ListView) findViewById(R.id.list_main_a);
4.给 lv_main_a 设置点击事件//给ListView设置点击事件
lv_main_a.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int i, long id) { Toast.makeText(MainActivity.this, "跳转到"+tityem[i%tityem.length], Toast.LENGTH_SHORT).show(); } });5.定义两个全局变量的集合private int[] images={R.drawable.icon_user,R.drawable.icon_user,R.drawable.icon_user,R.drawable.icon_user,R.drawable.icon_user}; private String[] tityem={"西游记","三国演义","水浒传","少林寺传奇","雄霸天下"};6.设置内部类//OOM OutOfMemory 内存溢出
class My extends BaseAdapter{ @Override public int getCount() { return 100; } @Override public Object getItem(int i) { return tityem[i%tityem.length]; } @Override public long getItemId(int i) { return i; } @Override public View getView(final int i, View view, ViewGroup parent) { if(view==null){ //view如果为空 view=getLayoutInflater().inflate(R.layout.layout_mx,null); 把布局文件转成View layoutActivity layout=new layoutActivity(); layout.button= (Button) view.findViewById(R.id.bt_main_a); layout.imageView= (ImageView) view.findViewById(R.id.Iv_main_a); layout.textView= (TextView) view.findViewById(R.id.tv_main_a); view.setTag(layout); } layoutActivity layout= (layoutActivity) view.getTag(); //获取view的值//设值 layout.textView.setText(tityem[i%tityem.length]+i); layout.imageView.setImageResource(images[i%tityem.length]); layout.button.setOnClickListener(new View.OnClickListener() { //给按钮设置点击事件 @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "正在下载"+tityem[i%tityem.length], Toast.LENGTH_SHORT).show(); } }); return view; } }全部代码如下:package com.example.g160628_android08_widgetplus2; import android.content.pm.LabeledIntent; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; 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; public class MainActivity extends AppCompatActivity { private ListView lv_main_a; private int[] images={R.drawable.icon_user,R.drawable.icon_user,R.drawable.icon_user,R.drawable.icon_user,R.drawable.icon_user}; private String[] tityem={"西游记","三国演义","水浒传","少林寺传奇","雄霸天下"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout); lv_main_a= (ListView) findViewById(R.id.list_main_a); lv_main_a.setAdapter(new My()); lv_main_a.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int i, long id) { Toast.makeText(MainActivity.this, "跳转到"+tityem[i%tityem.length], Toast.LENGTH_SHORT).show(); } }); } class My extends BaseAdapter{ @Override public int getCount() { return 100; } @Override public Object getItem(int i) { return tityem[i%tityem.length]; } @Override public long getItemId(int i) { return i; } @Override public View getView(final int i, View view, ViewGroup parent) { if(view==null){ view=getLayoutInflater().inflate(R.layout.layout_mx,null); layoutActivity layout=new layoutActivity(); layout.button= (Button) view.findViewById(R.id.bt_main_a); layout.imageView= (ImageView) view.findViewById(R.id.Iv_main_a); layout.textView= (TextView) view.findViewById(R.id.tv_main_a); view.setTag(layout); } layoutActivity layout= (layoutActivity) view.getTag(); layout.textView.setText(tityem[i%tityem.length]+i); layout.imageView.setImageResource(images[i%tityem.length]); layout.button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "正在下载"+tityem[i%tityem.length], Toast.LENGTH_SHORT).show(); } }); return view; } } }效果如下:点击书名
点击下载:![]()