在网上找了很多资料都不全,所以自己写了个。
废话就不多说了。
效果图
Main.java部分代码:
package com.jli.main; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button)findViewById(R.id.test); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { User u = new User(); u.setId("111"); u.setName("3213"); u.setPwd("fdsaf"); u.setMoney(500.0); Intent it = new Intent(Main.this,SingleSelectionList.class); Bundle bundle = new Bundle(); bundle.putString("id", u.getId()); bundle.putString("name", u.getName()); bundle.putDouble("money", u.getMoney()); it.putExtras(bundle); it.putExtras(bundle); startActivityForResult(it,0); }}); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(Activity.RESULT_OK == resultCode){ User user = upDateUIForUserInfo(data.getExtras()); Toast.makeText(this, "你选择的是:/n"+"id:"+user.getId()+"/n"+"name:"+user.getName(), Toast.LENGTH_LONG).show(); }else{ Toast.makeText(this, "你取消了选择/n", Toast.LENGTH_LONG).show(); } } private User upDateUIForUserInfo(Bundle bundle){ User user = new User(); user.setId(bundle.getString("id")); user.setName(bundle.getString("name")); return user; } }
转至:http://blog.csdn.net/IT_Farmer2010/archive/2011/04/22/6342009.aspx
SingleSelectionList.java:
package com.jli.main; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; /** * User: 李杰 * Date: 2011-3-29 * Time: 下午02:43:46 */ public class SingleSelectionList extends Activity implements View.OnClickListener, OnItemClickListener { private ImageButton mChancleImgBtn; private ImageButton mOkImgBtn; private ListView mFriendsListView; private List<User> mFriendsList = new ArrayList<User>(); private User mUser; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.single_selection_list); mOkImgBtn = (ImageButton)findViewById(R.id.btnYes); mOkImgBtn.setOnClickListener(this); mChancleImgBtn = (ImageButton)findViewById(R.id.btnNo); mChancleImgBtn.setOnClickListener(this); buildData(); mFriendsListView = (ListView)findViewById(android.R.id.list); mFriendsListView.setAdapter(new EfficientAdapter(this)); mFriendsListView.setOnItemClickListener(this); } @Override protected void onRestart() { if(mFriendsList.size() != 0) buildData(); mFriendsListView.setAdapter(new EfficientAdapter(this)); super.onRestart(); }; @Override protected void onDestroy() { super.onDestroy(); } @Override public void onClick(View v) { Intent it = this.getIntent(); if(v.getId() == R.id.btnYes && mUser != null){ Intent intent = this.getIntent(); Bundle bundle = intent.getExtras(); bundle.putString("id", mUser.getId()); bundle.putString("name", mUser.getName()); it.putExtras(bundle); SingleSelectionList.this.setResult(Activity.RESULT_OK, it); }else{ SingleSelectionList.this.setResult(Activity.RESULT_CANCELED, it); } SingleSelectionList.this.finish(); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { /**update list UI when item on click*/ class UpDateListStaus implements Runnable{ private View mView; private EfficientAdapter mAdapt; public UpDateListStaus(View view, EfficientAdapter adapt){ this.mAdapt = adapt; this.mView = view; } public void run(){ //update previous on selected item status if(mAdapt.getSelectHolder() != null){ mAdapt.getSelectHolder().imgView.setImageBitmap(BitmapFactory.decodeResource(mView.getResources(), R.drawable.rdbtn_2)); } //update current on selected item status ViewHolder tmp = new ViewHolder(); tmp.imgView = (ImageView)mView.findViewById(R.id.onSelectSign); upDateItemUi(tmp.imgView, true); //reset current on selected holder mAdapt.setSelectHolder(tmp); } } //update current on selected user mUser = mFriendsList.get(position); EfficientAdapter adapt = (EfficientAdapter)parent.getAdapter(); //update current on selected item's position adapt.setCurSelectPosition(position); new Handler().post(new UpDateListStaus(view,adapt)); } /**if list no data so ui show "you no friends"*/ protected void showUiForNoData(){ if(mFriendsList.size() == 0) ((TextView)findViewById(android.R.id.empty)).setText(getString(R.string.noFriends)); else ((TextView)findViewById(android.R.id.empty)).setText(""); } /**update item's status*/ private void upDateItemUi(ImageView imgView, boolean b){ if(b){ imgView.setImageBitmap(BitmapFactory.decodeResource(imgView.getResources(), R.drawable.rdbtn_1)); }else{ imgView.setImageBitmap(BitmapFactory.decodeResource(imgView.getResources(), R.drawable.rdbtn_2)); } } private class EfficientAdapter extends BaseAdapter { private LayoutInflater mInflater; private ViewHolder mHolder; /**on selected item*/ private ViewHolder mSelectHolder; /**on selected item posion*/ private int mCurSelectPosition = -1; public EfficientAdapter(Context context) { mInflater = LayoutInflater.from(context); } public void setCurSelectPosition(int mCurSelectPosition) { this.mCurSelectPosition = mCurSelectPosition; } public ViewHolder getSelectHolder() { return mSelectHolder; } public void setSelectHolder(ViewHolder selectHolder) { this.mSelectHolder = selectHolder; } @Override public int getCount() { return mFriendsList.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public void notifyDataSetChanged() { }; @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = mInflater.inflate(R.layout.list_item, null); mHolder = new ViewHolder(); mHolder.id = (TextView) convertView.findViewById(R.id.userId); mHolder.icon = (ImageView) convertView.findViewById(R.id.userShot); mHolder.name = (TextView) convertView.findViewById(R.id.name); mHolder.imgView = (ImageView) convertView.findViewById(R.id.onSelectSign); convertView.setTag(mHolder); } else { mHolder = (ViewHolder) convertView.getTag(); } //update ui by current item on select or no if(position == mCurSelectPosition){ upDateItemUi(mHolder.imgView,true); }else{ upDateItemUi(mHolder.imgView,false); } User u = mFriendsList.get(position); mHolder.id.setText(u.getId()); mHolder.name.setText(u.getName()); mHolder.icon.setImageBitmap(u.getShot()); return convertView; } } private class ViewHolder { /** User id*/ TextView id; /**User shot*/ ImageView icon; /**User name*/ TextView name; /**sgin current user on selected or no*/ ImageView imgView; } private void buildData(){ for(int i=0;i<50;i++){ User u = new User(); u.setId("id"+i); u.setName("name"+i); u.setShot(BitmapFactory.decodeResource(this.getResources(), R.drawable.user_shot)); mFriendsList.add(u); } } }
single_selection_list.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/background" android:layout_height="match_parent" android:layout_width="match_parent"> <!--android:layout_marginTop="120px" android:layout_marginLeft="40px" @drawable/stact_py_background android:listSelector 获取到焦点行背景色 android:drawSelectorOnTop="true" 点击某一条记录,颜色会显示在最上面,记录上的文字被遮住,所以点击文字不放,文字就看不到 android:drawSelectorOnTop="false" 点击某条记录不放,颜色会在记录的后面,成为背景色,但是记录内容的文字是可见的 android:divider 列表分割线颜色 --> <ListView android:id="@id/android:list" android:background="@color/transparent" android:divider="@drawable/item_divider" android:listSelector="#00000000" android:layout_width="700px" android:layout_height="200px" android:drawSelectorOnTop="true" android:cacheColorHint="@color/transparent" android:layout_marginTop="120px" android:layout_marginLeft="40px" /> <TextView android:id="@id/android:empty" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textSize="21sp"/> <LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginTop="320px" android:gravity="center_horizontal"> <ImageButton android:id="@+id/btnYes" android:background="@drawable/btn_ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="200px" /> <ImageButton android:id="@+id/btnNo" android:background="@drawable/btn_chancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="100px" /> </LinearLayout> </RelativeLayout>
转至:http://blog.csdn.net/IT_Farmer2010/archive/2011/04/22/6342009.aspx
list_item.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/transparent"> <ImageView android:id="@+id/onSelectSign" android:src="@drawable/rdbtn_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="40px" android:focusable="false"/> <ImageView android:id="@+id/userShot" android:src="@drawable/user_shot" android:scaleType="centerCrop" android:layout_width="45px" android:layout_height="45px" android:layout_centerVertical="true" android:layout_marginLeft="80px" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/solidback" android:layout_marginLeft="160px" android:layout_centerVertical="true" android:textSize="20sp" /> <TextView android:id="@+id/userId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/solidback" android:textSize="20sp" android:layout_centerVertical="true" android:layout_marginLeft="600px" /> </RelativeLayout>
转至:http://blog.csdn.net/IT_Farmer2010/archive/2011/04/22/6342009.aspx
麻烦看帖的朋友还是扔个砖头...
这里只是部分代码,源码下载地址http://download.csdn.net/source/3216279
下载有点贵,没办法自己心血,如果实在没有分请联系我:lijie214@foxmail.com 可以发你一份。