版权声明:本文为博主原创文章,未经博主允许不得转载。
效果图如下:
XML代码(activity_main)如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ListView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/lv_main_list"></ListView>
- </LinearLayout>
XML代码(itemtag)如下(用来优化的):
- <?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:layout_height="match_parent"
- android:descendantFocusability="blocksDescendants">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/iv_main_image"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:id="@+id/tv_main_word"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="下载"
- android:id="@+id/bu_main_bu"/>
- </LinearLayout>
JAVA代码如下(ItemTag):
可以把它想象成一个实体类
- public class ItemTag {
- public ImageView imageView;
- public TextView textView;
- public Button button;
- }
Java代码如下(MainActivity):
- public class MainActivity extends AppCompatActivity {
- private ListView lv_main_list;
- private int images[]={R.drawable.bird,R.drawable.cat,R.drawable.chicken,R.drawable.duck,R.drawable.elephant};
- private String titles[]={"小鸟","猫咪","小鸡","鸭子","大象"};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- lv_main_list = (ListView) findViewById(R.id.lv_main_list);
- lv_main_list.setAdapter(new MyAdapter());
- lv_main_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- Toast.makeText(MainActivity.this, "跳转到"+titles[position]+"页面", Toast.LENGTH_SHORT).show();
- }
- });
- }
- class MyAdapter extends BaseAdapter{
- @Override
- public int getCount() {
- return titles.length;
- }
- @Override
- public Object getItem(int position) {
- return titles[position];
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(final int position, View convertView, ViewGroup parent) {
- if(convertView==null){
- convertView=getLayoutInflater().inflate(R.layout.itemtag,null);
- ItemTag itemTag=new ItemTag();
- itemTag.imageView= (ImageView) convertView.findViewById(R.id.iv_main_image);
- itemTag.textView= (TextView) convertView.findViewById(R.id.tv_main_word);
- itemTag.button = (Button) convertView.findViewById(R.id.bu_main_bu);
- convertView.setTag(itemTag);
- }
- ItemTag itemTag= (ItemTag) convertView.getTag();
- itemTag.imageView.setImageResource(images[position]);
- itemTag.textView.setText(titles[position]);
- itemTag.button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(MainActivity.this, "正在下载"+titles[position], Toast.LENGTH_SHORT).show();
- }
- });
- return convertView;
- }
- }
- }