主要代码:
构建虚拟数据 类:
VirtualData.java
package com.data;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import com.example.demo.R;
public class VirtualData extends Activity{
private List
activity_virtual_vlist.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"
tools:context="com.example.demo.VirtualVListActivity" >
<ListView android:id="@+id/vlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</RelativeLayout>
vlist.xml
<LinearLayout 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:orientation="horizontal" >
<ImageView
android:id="@+id/img"
android:layout_width="60dp"
android:layout_height="60dp"
android:scaleType="fitCenter"
android:background="@drawable/image"
android:layout_margin="3px"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">"
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22px"
android:text="" />
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12px"
android:text="" />
<TextView
android:id="@+id/imgid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12px"
android:text="" />
</LinearLayout>
</LinearLayout>
VirtualVListActivity.java
package com.example.demo;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.data.VirtualData;
public class VirtualVListActivity extends ActionBarActivity {
private ListView listView;
private List
> mData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_virtual_vlist);
listView = (ListView)findViewById(R.id.vlist);
listView.setOnItemClickListener(itemListener);
VirtualData vData = new VirtualData();
mData = vData.getData();
SimpleAdapter adapter = buildSimpleAdapter(this, mData);
setListAdapter(adapter);
Map
map = mData.get(4);
}
OnItemClickListener itemListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView
parent, View view, int position,
long id) {
TextView title = (TextView)view.findViewById(R.id.title);
TextView info = (TextView)view.findViewById(R.id.info);
ImageView img = (ImageView)view.findViewById(R.id.img);
Resources res = img.getResources();
String idString = res.getResourceEntryName(img.getId());
String detailMsg = "公司:" + title.getText().toString() + "\n\r软件:" + info.getText().toString() + position;
showToast(detailMsg,img);
}
};
private void showToast(String detailMsg,ImageView img)
{
Toast toast = Toast.makeText(this, detailMsg, 1000);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout)toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageDrawable(img.getDrawable());
toastView.addView(imageCodeProject,0);
toast.show();
}
private SimpleAdapter buildSimpleAdapter(Context context,List
> mData) { SimpleAdapter adapter = new SimpleAdapter(context, mData, R.layout.vlist, new String[]{"title","info","img","imgid"}, new int[]{R.id.title,R.id.info,R.id.img,R.id.imgid}); return adapter; } private void setListAdapter(SimpleAdapter adapter) { listView.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.virtual_vlist, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
源码下载地址:
http://download.csdn.net/download/qq_16959405/8409785