Android ListView同一个item显示2列的实现方法(仿2列商品列表)

Android实战开发中,ListView控件用途十分广泛,各种自定义控件多种多样。当项目要求实现一个2列的商品列表形式的界面,我们首先肯定想到用ListView,然后我们可以使用百度的自定义ListView,当然也可以用以下方法实现。

Android实战开发中ListView同一个item显示2列的实现方法其实主要不在于ListView控件本身,是在于ListView的适配器Adapter的自定义方法,其实方法很简单,直接上代码就可以简而易懂了。

一、首先看最主要的Adapter代码:

package com.tongleer.demo.android.listview;
import java.util.ArrayList;
import java.util.List;
import com.tongleer.demo.android.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
/**
 * 2列ListView的适配器
 * @author tongleer.com
 *
 */
public class MyAdapter extends BaseAdapter{
    protected Context context;
    protected LayoutInflater inflater;
    protected int resource;
    protected ArrayList list;
    public MyAdapter(Context context, int resource, ArrayList list){
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.resource = resource;
        if(list==null){
            this.list=new ArrayList<>();
        }else{
            this.list = list;
        }
    }
    @Override
    public int getCount() {
        if(list.size()%2>0) {
            return list.size()/2+1;
        } else {
            return list.size()/2;
        }
    }
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null ) {
            convertView = inflater.inflate(resource, null);
            vh = new ViewHolder();
            vh.tv1=(TextView)convertView.findViewById(R.id.tv1);
            vh.tv2=(TextView)convertView.findViewById(R.id.tv2);
            convertView.setTag(vh);
        }else {
            vh = (ViewHolder)convertView.getTag();
        }
        int distance =  list.size() - position*2;
        int cellCount = distance >= 2? 2:distance;
        final List itemList = list.subList(position*2,position*2+cellCount);
        if (itemList.size() >0) {
            vh.tv1.setText(itemList.get(0));
            vh.tv1.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, itemList.get(0), Toast.LENGTH_SHORT).show();
                }
            });
            if (itemList.size() >1){
                vh.tv2.setVisibility(View.VISIBLE);
                vh.tv2.setText(itemList.get(1));
                vh.tv2.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context, itemList.get(1), Toast.LENGTH_SHORT).show();
                    }
                });
            }else{
                vh.tv2.setVisibility(View.INVISIBLE);
            }
        }
        return convertView;
    }
    /**
     * 封装ListView中item控件以优化ListView
     * @author tongleer
     *
     */
    public static class ViewHolder{
        TextView tv1;
        TextView tv2;
    }
}

二、然后看Activity和布局的代码:

1、Activity:

package com.tongleer.demo.android.listview;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import com.tongleer.demo.android.R;
/**
 * 实现2列的ListView
 * @author tongleer.com
 *
 */
public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView lv = (ListView)findViewById(R.id.lv);
        ArrayList list = new ArrayList();
        for (int i = 0; i < 21; i++) {
            list.add("测试:"+i);
        }
        MyAdapter adapter = new MyAdapter(this,R.layout.moban,list);
        lv.setAdapter(adapter);
    }
}

2、moban.xml布局:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/tv1"
        android:gravity="center"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:text="tv1" />
    <TextView
        android:id="@+id/tv2"
        android:gravity="center"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:text="tv2" />
LinearLayout>

3、activity_main_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="com.tongleer.demo.android.listview.MainActivity" >  

    <ListView  
        android:id="@+id/lv"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent" >  
    ListView>  

RelativeLayout>  

到此结束,是不是so easy?

最后将代码的下载链接发出来:http://download.csdn.net/download/ruxishanshi/9469835

你可能感兴趣的:(【android学习】,android,listview,adapter,仿商品列表,android,listview)