listView多种布局嵌套Checkbox冲突上啦刷新下拉加载

实现后的效果

MainActivity

package com.example.demopulltolistview;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MainActivity extends Activity {
	PullToRefreshListView listView;
	myListViewAdapter ada;
	ILoadingLayout iLL;
	private HashMap<Integer, Boolean> isSelected = new HashMap<Integer, Boolean>();
	List<Map<String, String>> data = new ArrayList<>();
	Handler han = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				ada.notifyDataSetChanged();
				listView.onRefreshComplete();
				break;
			case 2:
				ada.notifyDataSetChanged();
				listView.onRefreshComplete();
				break;

			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView = (PullToRefreshListView) findViewById(R.id.listView);
		listView.setMode(Mode.BOTH);//设置支持上啦 和下拉双种 模式
		iLL  = listView.getLoadingLayoutProxy(false, true);//设置下啦加载的文字内容
		iLL.setPullLabel("下拉加载");//设置显示文字
		iLL.setRefreshingLabel("主公不要着急");//设置加载中的文字
		iLL.setReleaseLabel("请放开");//设置手指按下后的文字
		init(16, "中国国家体育会");
		
		ada = new myListViewAdapter(this, data, isSelected);
		listView.setAdapter(ada);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Log.d("--==tag", "--==tag-点击了第" + arg2);
			}
		});
		// ----------------------------------------------------------------------------/
		listView.setOnRefreshListener(new OnRefreshListener<ListView>() {
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				if (listView.isHeaderShown()) {//下拉刷新
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								Thread.sleep(500);
								data.clear();
								init(32, "美国国家委员会");
								Message msg = Message.obtain();
								msg.obj = 1;
								msg.what = 1;
								han.sendMessage(msg);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}).start();
				} else {//上啦加载
					new Thread(new Runnable() {
						public void run() {
							try {
								Thread.sleep(1000);
								int size = data.size();

								for (int i = size; i < size +5; i++) {
									Map<String, String> map = new HashMap<String, String>();
									map.put("name", "加载更多的内容在这里 是 " + i);
									map.put("nameId", "加载更多的Id是在这里" + i);
									data.add(map);
								}
								initPosition(size);
								Message msg = Message.obtain();
								msg.obj = 1;
								msg.what = 2;
								han.sendMessage(msg);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}).start();
				}

			}
		});

	}

	private void init(int count, String value) {
		for (int i = 0; i < count; i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("name", value + i);
			map.put("nameId", "中国国家体育馆ID" + i);
			data.add(map);
			// map.put("time", new
			// SimpleDateFormat("YYYY-MM-DD HH:mm:ss ").format(new
			// Date())+"--"+i);
		}
		Log.d("--==tag", "--==data长度 " + data.size());
		initPosition(0);
	}

	private void initPosition(int pi) {
		Log.d("--==tag", "--==data长度 " + data.size());

		for (int i = pi; i < data.size(); i++) {
			int position = i;
			if (position > 9) {
				String strPosition = String.valueOf(position);
				position = Integer.parseInt(strPosition.substring(strPosition
						.length() - 1));
			}
			if (!(position <= 2 || (position <= 7 && position >= 5))) {

				isSelected.put(i, false);
			}
		}

		Log.d("--==tag", "--==tag长度 " + isSelected.size());

	}
}
适配器

package com.example.demopulltolistview;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;
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.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class myListViewAdapter extends BaseAdapter {

	private List<Map<String, String>> data;//数据源
	LayoutInflater context;
	Context con;
	public HashMap<Integer, Boolean> isSelected;//选中状态的集合 防止复用 view造成选择混乱问题

	public myListViewAdapter(Context con, List<Map<String, String>> data,
			HashMap<Integer, Boolean> isSelected) {
		context = LayoutInflater.from(con);
		this.con = con;
		this.data = data;
		this.isSelected = isSelected;
	}

	public int getCount() {

		return data.size();
	}

	public Object getItem(int pesition) {

		return data.get(pesition);
	}
	public long getItemId(int pesition) {

		return pesition;
	}
	// 返回当前 item是哪一种类型
	@Override
	public int getItemViewType(int position) {
		int type = 0;
		if (position > 9) {
			String strPosition = String.valueOf(position);
			position = Integer.parseInt(strPosition.substring(strPosition
					.length() - 1));

		}

		if (position <= 2 || (position <= 7 && position >= 5)) {
			type = 1;// 第一种类型的view 的布局
		}

		return type;
	}

	// 返回当前有几种 类型
	@Override
	public int getViewTypeCount() {

		return 2;
	}

	@Override
	public View getView(final int pesition, View convertView, ViewGroup arg2) {
		ViewHoderItem01 vh01 = null;
		ViewHoderItem02 vh02 = null;

		int getViewType = getItemViewType(pesition);

		if (convertView == null) {
			switch (getViewType) {
			case 0:// 第二种布局
				vh02 = new ViewHoderItem02();
				convertView = context.inflate(R.layout.listview_item02, null);
				vh02.reItem02Top1 = (TextView) convertView
						.findViewById(R.id.reTextView01);
				vh02.reItem02Top02 = (TextView) convertView
						.findViewById(R.id.reTextView02);
				vh02.reitem02CheckBox01 = (CheckBox) convertView
						.findViewById(R.id.recheckBox);

				convertView.setTag(vh02);
				break;
			case 1:// 第一种布局
				vh01 = new ViewHoderItem01();
				convertView = context.inflate(R.layout.listview_item_01, null);
				vh01.item01Image01 = (ImageView) convertView
						.findViewById(R.id.image);
				vh01.item01Image02 = (ImageView) convertView
						.findViewById(R.id.image02);
				vh01.tvItem01Top1 = (TextView) convertView
						.findViewById(R.id.textTop);
				vh01.tvItem01Top02 = (TextView) convertView
						.findViewById(R.id.textTop2);
				vh01.tvItem01Bottom0 = (TextView) convertView
						.findViewById(R.id.textBottom);
				convertView.setTag(vh01);

				break;

			default:
				Log.d("--==tag", "--==tag返回类型异常0");
				break;
			}
		} else {

			switch (getViewType) {
			case 0:

				vh02 = (ViewHoderItem02) convertView.getTag();

				break;

			case 1:
				vh01 = (ViewHoderItem01) convertView.getTag();
				break;

			default:

				Log.d("--==tag", "--==tag返回类型异常1");
				break;
			}

		}

		switch (getViewType) {
		case 0:
			vh02.reItem02Top1.setText(data.get(pesition).get("name"));
			vh02.reItem02Top02.setText("100002535487896");
			Log.d("--==tag", "--==tag ---" + isSelected.get(pesition));
			vh02.reitem02CheckBox01.setChecked(isSelected.get(pesition));
			vh02.reitem02CheckBox01.setTag(pesition);

			vh02.reitem02CheckBox01.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					Integer tag = (Integer) v.getTag();
					if (isSelected.get(tag)) {

						isSelected.put(tag, false);
					} else {

						isSelected.put(tag, true);

					}

					notifyDataSetChanged();

				}
			});
			break;
		case 1:
			vh01.item01Image01.setBackgroundResource(R.drawable.wine);
			vh01.item01Image02.setBackgroundResource(R.drawable.time);

			vh01.tvItem01Top1.setText(data.get(pesition).get("name"));
			vh01.tvItem01Top02.setText("100002535487896");
			vh01.tvItem01Bottom0.setText(new SimpleDateFormat(
					"yyyy-MM-dd  HH:mm ").format(new Date()));

			break;

		default:
			break;
		}

		return convertView;
	}

	class ViewHoderItem01 {
		TextView tvItem01Top1, tvItem01Top02, tvItem01Bottom0;
		ImageView item01Image01, item01Image02;
	}

	class ViewHoderItem02 {
		TextView reItem02Top1, reItem02Top02;
		CheckBox reitem02CheckBox01;
	}

}


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="${relativePackage}.${activityClass}" >

    <com.handmark.pulltorefresh.library.PullToRefreshListView 
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"  
        android:background="#32CD"       
        android:descendantFocusability="blocksDescendants">
        
    </com.handmark.pulltorefresh.library.PullToRefreshListView>

</RelativeLayout>



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/framLayout"
    android:layout_width="match_parent"
    android:layout_height="100dp" >

    <RelativeLayout
        android:id="@+id/re01"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#32CD"
       >

        <ImageView
            android:id="@+id/image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/redwine"
            android:scaleType="fitXY" />

        <TextView
            android:id="@+id/textTop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/image"
          
            android:gravity="center"
            android:singleLine="true"
            android:text="标题名"
            android:textColor="#ff2211" />

        <TextView
            android:id="@+id/textTop2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/textTop"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/image"
           
            android:gravity="center"
            android:singleLine="true"
            android:text="商品Id"
            android:textColor="#112211" />

        <ImageView
            android:id="@+id/image02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/textTop"
            android:layout_alignParentBottom="true"
            android:background="@drawable/time" />

        <TextView
            android:id="@+id/textBottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_toRightOf="@id/image02"
            android:text="时间显示" />
    </RelativeLayout>
   <!--  <RelativeLayout 
        android:id="@+id/re02"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:background="#98ab"
        android:visibility="gone"
        >
       <CheckBox
           android:id="@+id/checkBox"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentRight="true"
           android:layout_centerVertical="true"
           android:background="#ffddaa" />

       <TextView
           android:id="@+id/reTextView02"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_alignParentLeft="true"
           android:layout_below="@+id/checkBox"
           android:text="商品id" />

       <TextView
           android:id="@+id/reTextView01"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_above="@+id/checkBox"
           android:layout_alignParentLeft="true"
           android:text="商品标题" />
        
    </RelativeLayout> -->

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/framLayout"
    android:layout_width="match_parent"
    android:layout_height="100dp" >

    <RelativeLayout
        android:id="@+id/re02"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="#98ab" >

        <CheckBox
            android:id="@+id/recheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:focusable="false"
            
            />

        <TextView
            android:id="@+id/reTextView02"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/checkBox"
            android:text="商品id" />

        <TextView
            android:id="@+id/reTextView01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/recheckBox"
            android:text="商品标题" />

    </RelativeLayout>

</RelativeLayout>









  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值