自定义adapter实现listview双列显示

package com.appshare;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class DoubleAdapter extends BaseAdapter implements OnClickListener{
private List<String> datas = new ArrayList<String>();
private Context context;

public DoubleAdapter(Context context,List<String> datas){
this.context = context;
this.datas = datas;
}
@Override
public int getCount() {
return datas.size() % 2 == 0 ? datas.size() / 2 : datas.size() / 2 + 1;
}

@Override
public Object getItem(int position) {
return null;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHodler hodler = null;
if(convertView == null){
hodler = new ViewHodler();
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.item, null);
hodler.textView1 = (TextView) convertView.findViewById(R.id.text1);
hodler.textView2 = (TextView) convertView.findViewById(R.id.text2);
hodler.layout1 = (LinearLayout) convertView.findViewById(R.id.item_layout1);
hodler.layout2 = (LinearLayout) convertView.findViewById(R.id.item_layout2);

convertView.setTag(hodler);
}else{
hodler = (ViewHodler) convertView.getTag();
}

String item1 = "";
String item2 = "";

if(position * 2 + 1 < datas.size()){
item1 = datas.get(position * 2);
item2 = datas.get(position * 2 + 1);
}else if(position * 2 + 1 == datas.size()){
item1 = datas.get(position * 2);
item2 = "";
}

if(item1 != null){
hodler.textView1.setText(item1);
hodler.layout1.setOnClickListener(this);
hodler.layout1.setTag(item1);
}

if(item2 != null){
hodler.textView2.setText(item2);
hodler.layout2.setOnClickListener(this);
hodler.layout2.setTag(item2);
}




return convertView;
}

class ViewHodler {
TextView textView1;
TextView textView2;
LinearLayout layout1;
LinearLayout layout2;
}

@Override
public void onClick(View v) {
Toast.makeText(context, v.getTag().toString(), Toast.LENGTH_LONG).show();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值