关闭

ListView自定义样式

281人阅读 评论(0) 收藏 举报
分类:

效果图 :




MainActivity.class

package com.example.demo;

import java.util.ArrayList;
import java.util.HashMap;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;


public class MainActivity extends ActionBarActivity {
	
	ListView list;
	ArrayList<HashMap<String, Object>> myArrayList;
	SimpleAdapter mySimpleAdapter;
	String[] ary = { "模拟数据", "模拟数据", "模拟数据", "模拟数据", "模拟数据", "模拟数据", "模拟数据", "模拟数据", "模拟数据", "模拟数据" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
	void initList(int jsonArraylength){
		list = (ListView)findViewById(R.id.list);
		myArrayList = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < jsonArraylength; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("name", ary[i]);
			map.put("money", ary[i]);
			map.put("type", ary[i]);
			map.put("time", ary[i]);
			myArrayList.add(map);
		}
		mySimpleAdapter = new SimpleAdapter(this, myArrayList,
				R.layout.item, new String[] { "name","money","type","time" },
				new int[] { R.id.positionName_position_manage,R.id.positionMoney_position_manage,R.id.positionType_position_manage,R.id.positionTime_position_manage });
		mySimpleAdapter.notifyDataSetChanged();
		
		// 长按item,得到点中的index,即参数arg2
		list.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				return false;
			}
		});
		//点击item
		list.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
			}
		});
		list.setAdapter(mySimpleAdapter);
	}
}

activity_main.xml


<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" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:divider="@null" />

</RelativeLayout>

item.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="#FFF"
    android:paddingLeft="10dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="50dp"
        android:gravity="center">

        <TextView
            android:id="@+id/positionName_position_manage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="平面设计"
            android:textColor="#3F3A38"
            android:textSize="17sp" />

        <TextView
            android:id="@+id/positionMoney_position_manage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/positionName_position_manage"
            android:text="8000-10000"
            android:textColor="#9FA09F"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/positionType_position_manage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/positionName_position_manage"
            android:layout_alignParentRight="true"
            android:text="平面设计"
            android:textColor="#9FA09F"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/positionTime_position_manage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/positionType_position_manage"
            android:text="8000-10000"
            android:textColor="#9FA09F"
            android:textSize="14sp" />
    </RelativeLayout>

    <ImageView
        android:id="@+id/imv_position_manage"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_launcher" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="0.1dp"
        android:layout_alignParentBottom="true"
        android:background="#029108" />

</RelativeLayout>


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:85876次
    • 积分:1832
    • 等级:
    • 排名:千里之外
    • 原创:108篇
    • 转载:1篇
    • 译文:0篇
    • 评论:32条
    最新评论