手记

继承BaseAdapter实现布局文件内容在手机屏幕上的显示

1.activity_main.xml   设置listView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >


    <ListView

        android:id="@+id/lv_01"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

         />


</RelativeLayout>

2.MainActivity.java

package com.example.day07_baseadapter;


import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.ListView;

import android.widget.Toast;


public class MainActivity extends Activity {


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ListView lv=(ListView) findViewById(R.id.lv_01);

        List<Map<String,Object>>data=new ArrayList<Map<String,Object>>();

        for(int i=0;i<20;i++){

         Map<String,Object>map=new HashMap<String, Object>();

         if(i%2==0){

         map.put("mv","美女"+i);

         map.put("mv_img",R.drawable.mv);

         }else{

         map.put("mv","帅哥"+i);

         map.put("mv_img",R.drawable.sg);

         }

         data.add(map);

        }

        MyAdapter adapter=new MyAdapter(MainActivity.this,data);

        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new OnItemClickListener() {


@Override

public void onItemClick(AdapterView<?> arg0, View arg1, int 


arg2,

long arg3) {

// TODO Auto-generated method stub

Toast.makeText(MainActivity.this,"第"+arg2+"图


片!",0).show();

}

});

    }



   

    

}

3.item.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="horizontal" >

    <TextView 

        android:id="@+id/tv_01"

        android:layout_height="wrap_content"

        android:layout_width="0dp"

        android:layout_weight="1"

        android:gravity="center"

        android:textColor="#00ff00"

        />

    <ImageView 

        android:id="@+id/iv_01"

        android:layout_height="wrap_content"

        android:layout_width="0dp"

        android:layout_weight="1"

        android:maxWidth="80px"

        android:adjustViewBounds="true"

        />


</LinearLayout>

原文链接:http://www.apkbus.com/blog-813041-61100.html

0人推荐
随时随地看视频
慕课网APP