设置android:divider="@null"也即不为listview设置分割线 设置android:divider="@android:color/transparent"就是分割线透明,相当于没有分割线,不过分割线还是会占一些位置 设置android:dividerHeight="0dp"设置分割线高度为0,也就没有分割线了
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dip"
android:divider="#00000000"
android:dividerHeight="0dip"
android:listSelector="@android:color/transparent"
android:layout_gravity="center_vertical"/>
lvMain = (ListView)view.findViewById(R.id.list_bookcontent);
lvMain.setDivider(null);//去除listview的分割线
public class QuanDialogAdapter extends BaseAdapter {
private List<YouHuiQuanBean> mList;
private Context mContext;
public QuanDialogAdapter(List<YouHuiQuanBean> list,Context context) {
mList = list;
mContext=context;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
QuanDialogViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_dialog_quan, null);
holder = new QuanDialogViewHolder();
holder.tvRMB = (TextView) convertView.findViewById(R.id.tv_rmb);
holder.tvMoney = (TextView) convertView.findViewById(R.id.tv_momey);
holder.tvMoneyLast = (TextView) convertView.findViewById(R.id.tv_money_last);
holder.tvQuan = (TextView) convertView.findViewById(R.id.tv_quan);
holder.ivGou = (ImageView) convertView.findViewById(R.id.iv_gou);
holder.llBg = (LinearLayout) convertView.findViewById(R.id.ll_quan_dialog);
convertView.setTag(holder);
} else {
holder = (QuanDialogViewHolder) convertView.getTag();
}
在adapter内部写如下点击事件可能会出现明显的卡顿和延迟,甚至不能点击
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mContext.startActivity(new Intent(mContext, DetailGrayActivity.class));
}
});
final YouHuiQuanBean quanBean = mList.get(position);
holder.tvMoney.setText(quanBean.money);
holder.tvQuan.setText(quanBean.youhuiquan);
if("Y".equals(quanBean.bg)){
holder.llBg.setBackgroundResource(R.mipmap.quan_bg_yellow);
holder.tvMoney.setTextColor(mContext.getResources().getColor(R.color.quan_yellow));
holder.tvMoneyLast.setTextColor(mContext.getResources().getColor(R.color.quan_yellow));
holder.tvRMB.setVisibility(View.GONE);
holder.tvMoneyLast.setText("%");
}else {
holder.llBg.setBackgroundResource(R.mipmap.quan_bg_red);
holder.tvMoney.setTextColor(mContext.getResources().getColor(R.color.colorPrimary));
holder.tvMoneyLast.setTextColor(mContext.getResources().getColor(R.color.colorPrimary));
holder.tvRMB.setVisibility(View.VISIBLE);
holder.tvMoneyLast.setText(".0");
}
if (quanBean.gou){
holder.ivGou.setImageResource(R.mipmap.quan_gou);
}else {
holder.ivGou.setImageResource(R.mipmap.circle_quan_dialog);
}
holder.ivGou.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(quanBean.gou){
quanBean.gou=false;
}else {
quanBean.gou=true;
for(int i=0;i<mList.size();i++){
if(i==position){
continue;
}
mList.get(i).gou=false;
}
}
//接口回调 设置监听器
if(mClick!=null){
mClick.gouOnClick(quanBean);
}
notifyDataSetChanged();
}
});
return convertView;
}
static class QuanDialogViewHolder {
TextView tvRMB;
TextView tvMoney;
TextView tvMoneyLast;
TextView tvQuan;
ImageView ivGou;
LinearLayout llBg;
}
//接口回调
public interface GouOnClick{
void gouOnClick(YouHuiQuanBean quanBean);
}
GouOnClick mClick;
public void GouListener(GouOnClick click){
mClick=click;
}
}
设置某一条目不能点击
@Override
public boolean isEnabled(int position) {
String s = mList.get(position);
if (s.contains("年")){
return false;
}else {
return true;
}
}
取消点击效果
android:listSelector="@android:color/transparent"
多个type的adapter,可以共用holder
public class RepaymentPlanAdapter extends BaseAdapter {
// List<RepaymentPayBean.ResultBean.ListBean.DataBean> mList;
List<RepaymentPayBean.ResultBean.ListBean> mList;
Context mContext;
private String mtotalMoney;
// private String mMonth;
private SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private Calendar mCalendar = Calendar.getInstance();
private int HEADER_VIEW = 0;
private int NORMAL_VIEW = 1;
private int FOOT_VIEW = 2;
private int mCount = 0;
// public RepaymentPlanAdapter(List<RepaymentPayBean.ResultBean.ListBean> list, Context context,String totalMoney) {
public RepaymentPlanAdapter(Context context) {
// mList = list;
mContext = context;
// mtotalMoney=totalMoney;
}
@Override
public int getCount() {
int count = 0;
if (mList != null) {
for (int i = 0; i < mList.size(); i++) {
count++;
count += mList.get(i).getData().size();
}
}else {
return 0;
}
mCount=count + 1;
// mCount=count ;
return mCount;
}
@Override
public Object getItem(int position) {
// return mList.get(getHeaderRealPosition(position));
return null;
}
@Override
public long getItemId(int position) {
return position;
}
public int getHeaderRealPosition(int position) {
if (position==mCount){
return mList.size()-1;
}
int count = 0;
for (int i = 0; i < mList.size(); i++) {
count++;
count += mList.get(i).getData().size();
if (position < count) {
return i;
}
}
return 0;
}
public int getNormalViewInDataRealPosition(int position) {
/*if (position==mCount){
return mList.get(mList.size()-1).getData().size()-1;
}*/
int headerRealPosition = getHeaderRealPosition(position);
int count = 0;
for (int i = 0; i < headerRealPosition; i++) {
count += mList.get(i).getData().size();
}
return position - headerRealPosition - count - 1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int itemViewType = getItemViewType(position);
int headerRealPosition = getHeaderRealPosition(position);
if (itemViewType == HEADER_VIEW) {
HeaderViewHolder holder = null;
if (convertView == null) {
holder = new HeaderViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_time_line_header, parent, false);
holder.tvYears = (TextView) convertView.findViewById(R.id.tv_years_timeLine);
holder.vHeader=convertView.findViewById(R.id.v_header);
convertView.setTag(holder);
// convertView = View.inflate(mContext, R.layout.item_time_line_repayment_plan, null);
} else {
holder = (HeaderViewHolder) convertView.getTag();
}
if (position==0){
holder.vHeader.setVisibility(View.VISIBLE);
}else {
holder.vHeader.setVisibility(View.GONE);
}
if (headerRealPosition!=0){
//判断下一页的开头数据跟上一页的末尾是不是属于同一个月份
if (mList.get(headerRealPosition-1).getTime().equals(mList.get(headerRealPosition).getTime())){
holder.tvYears.setVisibility(View.GONE);
}else {
holder.tvYears.setVisibility(View.VISIBLE);
holder.tvYears.setText(mList.get(headerRealPosition).getTime());
}
}else {
holder.tvYears.setVisibility(View.VISIBLE);
holder.tvYears.setText(mList.get(headerRealPosition).getTime());
}
} else if (itemViewType == NORMAL_VIEW) {
try {
int normalViewInDataRealPosition = getNormalViewInDataRealPosition(position);
TimeLineViewHolder holder = null;
if (convertView == null) {
holder = new TimeLineViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_time_line_repayment_plan, parent, false);
holder.bottomLine = convertView.findViewById(R.id.xuxian_bottom_timeLine);
holder.tvMoney = (TextView) convertView.findViewById(R.id.tv_money_timeLine);
holder.tvMonthAndDay = (TextView) convertView.findViewById(R.id.tv_month_timeLine);
holder.llMid = (LinearLayout) convertView.findViewById(R.id.ll_mid_timeLine);
convertView.setTag(holder);
} else {
holder = (TimeLineViewHolder) convertView.getTag();
}
final RepaymentPayBean.ResultBean.ListBean.DataBean dataBean = mList.get(headerRealPosition).getData().get(normalViewInDataRealPosition);
String capital = dataBean.getCapital();
String rate = dataBean.getRate();
holder.tvMoney.setText("¥"+Double.parseDouble(capital)+"+"+Double.parseDouble(rate));
Date date = mFormat.parse(mFormat.format((Long.parseLong(dataBean.getRepay_time())) * 1000));
mCalendar.setTime(date);
int realMonth = mCalendar.get(Calendar.MONTH) + 1;
int day = mCalendar.get(Calendar.DAY_OF_MONTH);
holder.tvMonthAndDay.setText(realMonth + "月" + day + "日");
if (normalViewInDataRealPosition == mList.get(headerRealPosition).getData().size() - 1) {
//判断此页末尾的最后一个跟下一页开头的第一个是否属于同一个月份
if (headerRealPosition+1<=mList.size()-1){
if (mList.get(headerRealPosition+1).getTime().equals(mList.get(headerRealPosition).getTime())){
holder.bottomLine.setVisibility(View.GONE);
}
}else {
holder.bottomLine.setVisibility(View.VISIBLE);
}
} else {
holder.bottomLine.setVisibility(View.GONE);
}
} catch (ParseException e) {
e.printStackTrace();
}
}else if (itemViewType == FOOT_VIEW){
FootViewHolder holder=null;
if (convertView == null) {
holder = new FootViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_time_line_foot, parent, false);
holder.tvTotal= (TextView) convertView.findViewById(R.id.tv_total_repaymentPay);
convertView.setTag(holder);
}else {
holder = (FootViewHolder) convertView.getTag();
}
holder.tvTotal.setText(mtotalMoney);
}
return convertView;
}
public void setData(List<RepaymentPayBean.ResultBean.ListBean> list, String total) {
/*if (mList != null){
mList.clear();
}*/
mList=list;
mtotalMoney=total;
notifyDataSetChanged();
}
/*public void addData(List<RepaymentPayBean.ResultBean.ListBean> list, String total) {
mList.addAll(list);
mtotalMoney=total;
notifyDataSetChanged();
}*/
private static class HeaderViewHolder {
View bottomLine;
TextView tvMonthAndDay;
TextView tvMoney;
LinearLayout llMid;
TextView tvYears;
TextView tvTotal;
View vHeader;
}
private static class TimeLineViewHolder {
View bottomLine;
TextView tvMonthAndDay;
TextView tvMoney;
LinearLayout llMid;
TextView tvYears;
TextView tvTotal;
View vHeader;
}
private static class FootViewHolder {
View bottomLine;
TextView tvMonthAndDay;
TextView tvMoney;
LinearLayout llMid;
TextView tvYears;
TextView tvTotal;
View vHeader;
}
/*private static class TimeLineHeaderViewHolder {
TextView tvYears;
}*/
@Override
public int getItemViewType(int position) {
if (position==getCount() - 1){
return FOOT_VIEW;
}
int count = 0;
for (int i = 0; i < getHeaderRealPosition(position); i++) {
count += mList.get(i).getData().size();
count++;
}
/*if (position==mCount){
return FOOT_VIEW;
}else*/ if (position == count) {
return HEADER_VIEW;
} else {
return NORMAL_VIEW;
}
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public boolean isEnabled(int position) {
if (getItemViewType(position)==NORMAL_VIEW) {
return true;
} else {
return false;
}
}
}
如果item里面嵌套了抢焦点的控件(比如Button ,CheckBox等),那么点击item的时候,button等抢焦点的控件会抢先反应,这是因为item的焦点被button等控件抢了,想要item有自己的焦点,button等控件有自己的焦点的话,需要在item的根控件里面设置:android:descendantFocusability="blocksDescendants",这个属性值表示子有字的焦点,父有父的焦点:
该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。
属性的值有三种:
beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点,也就是各有各的焦点
如果还是抢焦点的话,在抢焦点的子控件里面设置android:focusable="false",基本上是没有抢焦点的问题的。