item:   

<?  version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout
     ns:android=\"http://schemas.android.com/apk/res/android\"
    android:layout_width=\"match_parent\"
    android:layout_height=\"match_parent\"
         android:orientation=\"horizontal\"
    >
    <ImageView
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:id=\"@+id/image\"
        android:maxWidth=\"72dp\"
        android:maxHeight=\"72dp\"
        android:paddingRight=\"10dp\"
        android:paddingBottom=\"20dp\"
        android:paddingTop=\"20dp\"
        android:adjustViewBounds=\"true\"
        />

    <TextView
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:id=\"@+id/name\"
        android:padding=\"10dp\"
        android:gravity=\"center\"
        />

</LinearLayout>

----------------------------------------------------------------------------------------------------------------------

main. :

<?  version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout
     ns:android=\"http://schemas.android.com/apk/res/android\"
    android:layout_width=\"match_parent\"
    android:layout_height=\"match_parent\"
    android:orientation=\"vertical\"
    >
<ImageView
    android:layout_width=\"match_parent\"
    android:layout_height=\"wrap_content\"
    android:scaleType=\"fitXY\"
    android:src=\"@mipmap/wei_top\"
    />
  <ListView
      android:layout_width=\"wrap_content\"
      android:layout_height=\"370dp\"
      android:id=\"@+id/weichat_list\"

      >

  </ListView>

  <ImageView
      android:layout_width=\"match_parent\"
      android:layout_height=\"wrap_content\"
      android:layout_gravity=\"bottom\"
      android:scaleType=\"fitXY\"
      android:src=\"@mipmap/wei_down\" />
</LinearLayout>

------------------------------------------------------------------------------------------------------

package com.zengjx.android project;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zengjx on 2018/12/18.
 */

public class WeiChatListView   extends Activity {

   ListView   weichatListView;
    //文本
   String[]   name=  new String[]{\"吴彦祖1\",\"吴彦祖2\",\"吴彦祖3\",
           \"吴彦祖4\",\"吴彦祖5\",\"吴彦祖6\",
           \"吴彦祖7\",\"吴彦祖8\"};
 //图片
   int[]    imageid=  new  int[]{R.mipmap.img1,R.mipmap.img2,R.mipmap.img3,R.mipmap.img4,
           R.mipmap.img5,R.mipmap.img6,R.mipmap.img7,R.mipmap.img8,
   };
    private List<Map<String, >>    listitems= new ArrayList<Map<String, >>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weichat_list);
        initView();
    }
    public  void   initView(){
        weichatListView=findViewById(R.id.weichat_list);
        //通过for循环 添加 资源到list

        for(int i=0;i<imageid.length;i++){
           Map<String, >  map  =new HashMap<String, >();
           map.put(\"image\",imageid[i]);
           map.put(\"name\",name[i]);
           listitems.add(map);
        }
        //适配器
        SimpleAdapter   simpleAdapter  =new SimpleAdapter(this,//窗口
                listitems,//list 
                R.layout.weichat_listitem,//布局
                new String[]{\"image\",\"name\"},//文本
                new int[]{R.id.image,R.id.name});//图片
        weichatListView.setAdapter(simpleAdapter); // 将适配器与ListView关联
        weichatListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                   //获取选择项的值
                Map<String, >  map =null;
                map=listitems.get(position);
                String  name= (String)map.get(\"name\");
                Toast.makeText(WeiChatListView.this,\"点击\"+name,Toast.LENGTH_SHORT).show();
            }
        });
    }
}

\"\"

 

 

收藏 打印