Gridview实现银行选择列表


【MainActivity.java】

package com.example.activitydemo;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.AdapterView.OnItemClickListener;
import android.app.Activity;

public class MainActivity extends Activity {

    private Adapter gAdapter;
    private GridView gBankLogo;
    private int lastPosition = -1;
    private View lastView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gBankLogo = (GridView)findViewById(R.id.gridView1);
        gAdapter = new Adapter(this);
      //TEST
        for (int i = 0; i < 12; i++) {
        	 String bank = "" ;
             gAdapter.addObject("bank"+i, bank);
		}      
        gBankLogo.setAdapter(gAdapter);
        gBankLogo.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (lastPosition == position)
                    return;
                if (lastPosition != position) {
                    if (lastPosition == -1) {
                        RadioButton tmp = (RadioButton) view.findViewById(R.id.rSelect);
                        tmp.setChecked(true);
                        lastView = view;
                        lastPosition = position;
                    } else {
                        RadioButton tmp = (RadioButton) view.findViewById(R.id.rSelect);
                        tmp.setChecked(true);
                        RadioButton tmp1 = (RadioButton) lastView.findViewById(R.id.rSelect);
                        tmp1.setChecked(false);
                        lastView = view;
                        lastPosition = position;
                    }
                }
            }
        });       
    }
}


【Adapter.java】
package com.example.activitydemo;

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

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;

public class Adapter extends BaseAdapter {
	private ViewHolder holder;
	private LayoutInflater mLayoutInflater;
	private static Map<String, Object> M = new HashMap<String, Object>();
	private static List<String> L = new ArrayList<String>();

	public Adapter(Context context) {
		mLayoutInflater = LayoutInflater.from(context);

	}

	public void addObject(String key, String bank) {
		if (!L.contains(key)) {
			L.add(key);
		}
		M.put(key, bank);
		this.notifyDataSetChanged();
	}

	public void removeObject(String key) {
		M.remove(key);
		L.remove(key);
		this.notifyDataSetChanged();
	}

	public void removeAllObject() {
		if (getCount() != 0) {
			M.clear();
			L.clear();
			this.notifyDataSetChanged();
		}
	}

	public void removeAllSelect() {
		if (getCount() != 0) {
			holder.rSelect.setChecked(false);
			this.notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return M.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return M.get(L.get(position));
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mLayoutInflater.inflate(R.layout.item_msgbanklogo,
					null);
			holder.iBankLogo = (ImageView) convertView
					.findViewById(R.id.iBankLogo);
			holder.rSelect = (RadioButton) convertView
					.findViewById(R.id.rSelect);
			holder.iBankLogo.setClickable(false);
			holder.rSelect.setClickable(false);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.iBankLogo.setImageResource(R.drawable.abc);
		return convertView;
	}
}

【ViewHolder.java】

package com.example.activitydemo;

import android.widget.ImageView;
import android.widget.RadioButton;

public class ViewHolder {
	ImageView iBankLogo;
	RadioButton rSelect;
}



 

posted @ 2013-08-15 18:46  pangbangb  阅读(297)  评论(0编辑  收藏  举报