`
wayfarer
  • 浏览: 298385 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Customize ListView

阅读更多

1. ListView with Icon

(1)效果图

listview_icon

(2)实现代码

1)listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<ListView android:id="@+id/listview"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
	/>	
</LinearLayout>

2)simple_list_item_icon.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal" android:layout_width="wrap_content"
	android:layout_height="wrap_content">
	<ImageView android:id="@+id/simple_list_item_icon_iv" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:baselineAlignBottom="true">
	</ImageView>
	<TextView android:id="@+id/simple_list_item_icon_tv" 
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"
		android:textSize="24dip">
	</TextView>	
</LinearLayout>

3)IconSimpleAdapter.java

public class IconSimpleAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<Map<String, Object>> mList;
	private String[] mIndex;

	public IconSimpleAdapter(Context context, String[] index, List<Map<String, Object>> list) {
		mInflater = LayoutInflater.from(context);
		mList = list;
		mIndex = index;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			Bitmap mIcon = (Bitmap) mList.get(position).get(mIndex[0]);
			String mItem = " " + (String) mList.get(position).get(mIndex[1]);
			
			convertView = mInflater.inflate(R.layout.simple_list_item_icon, null);
			holder = new ViewHolder();
			holder.text = (TextView) convertView.findViewById(R.id.simple_list_item_icon_tv);
			holder.icon = (ImageView) convertView.findViewById(R.id.simple_list_item_icon_iv);
			holder.text.setText(mItem);
			holder.icon.setImageBitmap(mIcon);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		return convertView;
	}
	
	private class ViewHolder {
		TextView text;
		ImageView icon;
	}
}

4)IconListActivity.java

public class IconListActivity extends Activity {
	private final String ITEM_ICON = "icon";
	private final String ITEM_TEXT = "text";
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview);
		
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		Map<String, Object> item = new HashMap<String, Object>();
		Bitmap bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.a);
		item.put(ITEM_ICON, bm1);
		item.put(ITEM_TEXT, "中秋节");
		data.add(item);
		item = new HashMap<String, Object>();
		Bitmap bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.b);
		item.put(ITEM_ICON, bm2);
		item.put(ITEM_TEXT, "端午节");
		data.add(item);
		item = new HashMap<String, Object>();
		Bitmap bm3 = BitmapFactory.decodeResource(getResources(), R.drawable.c);
		item.put(ITEM_ICON, bm3);
		item.put(ITEM_TEXT, "重阳节");
		data.add(item);
		item = new HashMap<String, Object>();
		Bitmap bm4 = BitmapFactory.decodeResource(getResources(), R.drawable.d);
		item.put(ITEM_ICON, bm4);
		item.put(ITEM_TEXT, "清明节");
		data.add(item);
		
		ListView lv = (ListView) findViewById(R.id.listview);
		IconSimpleAdapter adapter = new IconSimpleAdapter(this, new String[] {ITEM_ICON, ITEM_TEXT}, data);
		lv.setAdapter(adapter);
	}
}

 

2. ListView with 3Rows

(1)效果图

listview_3rows

(2)实现代码

1)hs_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:id="@+id/hs_linearlayout">
	<ListView android:id="@+id/hs_activity_lv"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent"
	/>
</LinearLayout>

2)simple_list_item_3.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	
	<TextView android:id="@+id/simple_list_item_3_tv1" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" 
		android:textSize="10dip"/>

	<TextView android:id="@+id/simple_list_item_3_tv2"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" 
		android:textSize="18dip"/>
	
	<TextView android:id="@+id/simple_list_item_3_tv3"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" 
		android:textSize="12dip"/>
</LinearLayout>

3)ThreeRowsListActivity.java

public class ThreeRowsListActivity extends Activity {
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hs_activity);
		
		List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
		Map<String, Object> item = new HashMap<String, Object>();
		item.put("category", "便民,超市");
		item.put("name", "中百超市");
		item.put("address", "武汉街道口2547号");
		data.add(item);
		
		item = new HashMap<String, Object>();
		item.put("category", "五星级酒店");
		item.put("name", "希尔顿大酒店");
		item.put("address", "上海宝山路3651号");
		data.add(item);
		
		item = new HashMap<String, Object>();
		item.put("category", "写字楼,办公");
		item.put("name", "中环假日酒店");
		item.put("address", "北京菜户营1号");
		data.add(item);
		
		
		ListView listView = (ListView) findViewById(R.id.hs_activity_lv);
		ListAdapter adapter = new SimpleAdapter(this, data, R.layout.simple_list_item_3, 
				new String[] {"category", "name", "address"}, 
				new int[] {R.id.simple_list_item_3_tv1, R.id.simple_list_item_3_tv2, R.id.simple_list_item_3_tv3});
		listView.setAdapter(adapter);
		
	}
}

 

3. ListView with Icon

(1)How to get android default icons

1)http://www.darshancomputing.com/android/1.5-drawables.html

2)android.jar

(2)Classic Code

1)main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">	
    <ListView android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
 		android:drawSelectorOnTop="false"
 	/>
</LinearLayout>

 2)row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vw1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">    

    <ImageView android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView android:id="@+id/text1"
            android:textSize="12sp"
            android:textStyle="bold"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        <TextView android:id="@+id/text2"
            android:textSize="12sp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
    />
    </LinearLayout>

</LinearLayout>

 3)ResourceExplorer.java

public class ResourceExplorer extends ListActivity {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// define the list which holds the information of the list
		List<Map<String, Object>> resourceNames = new ArrayList<Map<String, Object>>();

		// define the map which will hold the information for each row
		Map<String, Object> data;

		// hard coded numbers retrieved from
		// http://code.google.com/android/reference/android/R.drawable.html
		for (int idx = 17301504; idx <= 17301655; idx++) {
			data = new HashMap<String, Object>();
			try {
				String stg = Resources.getSystem().getResourceName(idx);
				data.put("line1", stg);
				data.put("line2", idx);
				data.put("img", idx);
				resourceNames.add(data);
			} catch (Resources.NotFoundException nfe) {
			}
		}

		SimpleAdapter notes = new SimpleAdapter(this, resourceNames,
				R.layout.row, new String[] { "line1", "line2", "img" },
				new int[] { R.id.text1, R.id.text2, R.id.img });
		setListAdapter(notes);
	}
}

 

4.

  • 大小: 44.8 KB
  • 大小: 51.6 KB
分享到:
评论

相关推荐

    Android代码-帮你做 ListView RecyclerView 等组件的状态维护

    Customize connectivity view (colors / text / layout) Customize empty view Customize progress view Customize error view PR TODOS Why this layout A very common flow of an android view is : show a ...

    Toolbar基本使用及Toolbar+ListView实现滑动变色

    -- Customize your theme here. --&gt; &lt;item name="colorPrimary"&gt;@color/colorPrimary &lt;item name="colorPrimaryDark"&gt;@color/colorPrimaryDark &lt;item name="colorAccent"&gt;@color/colorAccent ...

    Qt-QML-Example:Qt,QML示例集合

    4,QML-Customize-Flipable QML实现页面反转封装,旋转过程中修改页面不透明度,比例,角度 5. QML-ListView-header-Suspension QML实现ListView控件悬浮标题栏 6,QML-Reflection QML实现控件或图片倒影效果 ...

    Android代码-ATableView

    &gt; Please refer to Android docs to customize lists depending on your needs. ATableView Summary ATableView intends to imitate same object model proposed on UIKit for building tables, so it's not only ...

    MlSkin 4.3 Delphi 10.3

    Memo, Edit, ListView and other components support transparent background, the border effect can be customized, add the commonly used search box and password input box. Redefined Toolbar, showing very...

    Android 组件应用框架示例工程及源码

    在别人源码基础上提取写的一个组件化android应用的框架,实现了组件化的HeaderBar,TabBar,SlideGallery,LIstView,TabView,WebView和TabHost,还包括版本检查和内嵌广告的功能。

    EasyNSE 0.2

    Column Handler - Add Columns to Explorer‘s Listview in Win2k/WinME and greater.ExtractIcon Handler - Change Shell object Icons to use your own.IconOverlay Handler - Add Custom Overlay images to ...

    Android 实现夜间模式的快速简单方法实例详解

    在Android应用开发中,夜间模式是一项重要的功能,它允许用户在低光照环境下或者个人喜好时...这种方法不仅支持ListView和RecyclerView,而且能够确保界面元素在主题切换时正确更新颜色,为用户提供良好的视觉体验。

    EHLib 6.3.171〖D7~XE5〗

    SelectionDrawParams property is added in DBGridEh and DBVertGridEh component, which allows you to customize the style of the currently selected cell. The property has the following sub-properties: ...

    Android4.4+ 实现半透明状态栏(Translucent Bars)

    对于包含可滚动内容的视图(如ScrollView或ListView),你需要在布局文件中添加`android:fitsSystemWindows="true"`属性,以确保内容正确填充到状态栏下方,同时设置`android:clipToPadding="false"`防止内容被裁剪...

Global site tag (gtag.js) - Google Analytics