首页 > 代码库 > ListView CheckBox 仿百度小说界面UI

ListView CheckBox 仿百度小说界面UI

不废话,直接上代码

效果图:

             


主要代码:

CheckBean:

package baidu.example.ui;

public class CheckBean {
	private int id;
	private boolean isCheck;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public boolean isCheck() {
		return isCheck;
	}
	public void setCheck(boolean isCheck) {
		this.isCheck = isCheck;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public CheckBean(int id, boolean isCheck, String name) {
		super();
		this.id = id;
		this.isCheck = isCheck;
		this.name = name;
	}
	public CheckBean(boolean isCheck, String name) {
		super();
		this.isCheck = isCheck;
		this.name = name;
	}
	public CheckBean() {
		super();
	}
	@Override
	public String toString() {
		return "CheckBean [id=" + id + ", isCheck=" + isCheck + ", name="
				+ name + "]";
	}

}

MyAdapter:

package baidu.example.ui;

import java.util.ArrayList;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter{
private ArrayList<CheckBean> list;
private LayoutInflater inflater;
private CallbackPosition callback;
	public static int type=0;
	public MyAdapter(LayoutInflater layoutInflater, ArrayList<CheckBean> data,CallbackPosition callback) {
		// TODO Auto-generated constructor stub
		this.setList(data);
		this.inflater=layoutInflater;
		this.callback=callback;
	}

	
	public ArrayList<CheckBean> getList() {
		return list;
	}


	public void setList(ArrayList<CheckBean> list) {
		if(list!=null){
			this.list = list;
		}else{
			list=new ArrayList<CheckBean>();
		}
	}


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

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

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

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder v=null;
		if(convertView==null){
			v=new ViewHolder();
			convertView=inflater.inflate(R.layout.item_main, null);
			v.name=(TextView)convertView.findViewById(R.id.textView_a);
			v.checkbox=(CheckBox)convertView.findViewById(R.id.checkBox1);
			convertView.setTag(v);
		}else{
			v=(ViewHolder) convertView.getTag();
		}
		CheckBean bean=getItem(position);
		if(type==0){
			v.checkbox.setVisibility(View.GONE);
		}else if(type==1){
			v.checkbox.setVisibility(View.VISIBLE);
		}
		v.checkbox.setChecked(bean.isCheck());
		String name=bean.getName();
		v.name.setText(name);
		
		v.checkbox.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			callback.callbackDeletePosition(position);	
			}
		});
		return convertView;
	}
	
	public interface CallbackPosition{
		void callbackDeletePosition(int position);
	}
}

MainActivity:

package baidu.example.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Notification.Action;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import baidu.example.ui.MyAdapter.CallbackPosition;

public class MainActivity extends Activity implements OnItemLongClickListener,OnClickListener,CallbackPosition{

	private ListView listview;
	private MyAdapter adapter;
	private Button cancel;
	private Button delete;
	private baidu.example.ui.ViewHolder vhs;
	private ImageView load_null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listview=(ListView)findViewById(R.id.listView1);
		cancel=(Button)findViewById(R.id.button_cancel);
		delete=(Button)findViewById(R.id.button_delete);
		load_null=(ImageView)findViewById(R.id.imageView1);

	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MyAdapter.type=0;
		adapter=new MyAdapter(getLayoutInflater(),getData(),this);
		listview.setAdapter(adapter);
		listview.setOnItemLongClickListener(this);
		cancel.setOnClickListener(this);
		delete.setOnClickListener(this);
	}
	private ArrayList<CheckBean> getData() {
		// TODO Auto-generated method stub
		ArrayList<CheckBean> list=new ArrayList<CheckBean>();
		CheckBean bean1=new CheckBean(0, false, "A");
		CheckBean bean2=new CheckBean(1, false, "B");
		CheckBean bean3=new CheckBean(2, false, "C");
		CheckBean bean4=new CheckBean(3, false, "D");
		CheckBean bean5=new CheckBean(4, false, "E");
		CheckBean bean6=new CheckBean(5, false, "F");
		CheckBean bean7=new CheckBean(6, false, "G");
		CheckBean bean8=new CheckBean(7, false, "H");
		CheckBean bean9=new CheckBean(8, false, "I");
		CheckBean bean10=new CheckBean(9, false, "J");	
		CheckBean bean11=new CheckBean(10, false, "K");
		CheckBean bean12=new CheckBean(11, false, "L");	
		CheckBean bean13=new CheckBean(12, false, "M");
		CheckBean bean14=new CheckBean(13, false, "N");
		list.add(bean1);
		list.add(bean2);
		list.add(bean3);
		list.add(bean4);
		list.add(bean5);
		list.add(bean6);	
		list.add(bean7);
		list.add(bean8);
		list.add(bean9);
		list.add(bean10);
		list.add(bean11);
		list.add(bean12);
		list.add(bean13);
		list.add(bean14);

		return list;
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		boolean flag=KeyDownUtils.onKeyDownEvent(event, MyAdapter.type, this);
		if(flag){
			return flag;
		}else{
			return super.onKeyDown(keyCode, event);
		}
	}
	@Override
	public void callbackDeletePosition(int position) {
		// TODO Auto-generated method stub
		CheckBean entity = adapter.getItem(position);
		int visiblePosition = listview.getFirstVisiblePosition();
		// 得到你需要更新item的View
		vhs = new ViewHolder();
		int vpotion = position - visiblePosition;
		Log.i("info", "vpotion:" + vpotion);
		View view = listview.getChildAt(vpotion);
		vhs.checkbox=(CheckBox)view.findViewById(R.id.checkBox1);
		entity.setCheck(vhs.checkbox.isChecked());
		adapter.getList().get(position).setCheck(entity.isCheck());
		vhs.checkbox.setChecked(entity.isCheck());
		adapter.notifyDataSetChanged();

	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, final int position,
			long id) {
		// TODO Auto-generated method stub
		if(MyAdapter.type==0){
			MyAdapter.type=1;
			cancel.setVisibility(View.VISIBLE);
			delete.setVisibility(View.VISIBLE);
			CheckBean beanIndex=adapter.getList().get(position);
			beanIndex.setCheck(true);
			listview.post(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					listview.setSelection(position);	
				}
			});
			adapter.notifyDataSetChanged();
		}
		return false;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==cancel){
			cancel.setVisibility(View.GONE);
			delete.setVisibility(View.GONE);
			MyAdapter.type=0;
			for (int i = 0; i < adapter.getList().size(); i++) {
				adapter.getList().get(i).setCheck(false);
			}
			listview.post(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					listview.setSelection(0);	
				}
			});
			adapter.notifyDataSetChanged();
		}else if(v==delete){
			ArrayList<CheckBean> deletelist=new ArrayList<CheckBean>();
			ArrayList<CheckBean> list=new ArrayList<CheckBean>();
			for (int i = 0; i < adapter.getList().size(); i++) {
				if(!adapter.getList().get(i).isCheck()){
					list.add(adapter.getList().get(i));
				}else{
					deletelist.add(adapter.getList().get(i));
				}
			}


			//调用删除(网络或者本地缓存)逻辑
			if(deletelist.size()==0){
				Toast.makeText(this, "请选中删除项", Toast.LENGTH_SHORT).show();
			}else{
				cancel.setVisibility(View.GONE);
				delete.setVisibility(View.GONE);
				MyAdapter.type=0;
				listview.post(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						listview.setSelection(0);	
					}
				});
				if(list.size()==0){
					load_null.setVisibility(View.VISIBLE);
				}
				adapter.setList(list);
				adapter.notifyDataSetChanged();
			}
		}
	}



}

demo就不上传了,如果有需要就留言

    

ListView CheckBox 仿百度小说界面UI