Android中ListView例子

开发中的点点滴滴 2018-03-30

以下为ListView完整例子:

1、activity_address_list.xml

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

    <include layout="@layout/title"></include>

    <ListView
        android:id="@+id/address_list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="45dp"
        android:layout_marginBottom="50dp">

    </ListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true">
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#45bf23"
            android:textColor="@color/white"
            android:text="添加新地址"
            android:textSize="16dp"/>

    </LinearLayout>
</RelativeLayout

2、item_address.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <TextView
        android:id="@+id/item_address_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/textsize_14dp"
        android:textColor="@color/text_color"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="15dp"
        android:text="[默认]"/>
    <TextView
        android:id="@+id/item_address_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/textsize_14dp"
        android:textColor="@color/text_color"
        android:layout_toRightOf="@+id/item_address_txt"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="15dp"
        android:text="刘智"/>

    <TextView
        android:id="@+id/item_address_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/textsize_14dp"
        android:textColor="@color/text_color"
        android:layout_alignParentRight="true"
        android:layout_marginTop="15dp"
        android:layout_marginRight="20dp"
        android:text="18711865875"/>

    <TextView
        android:id="@+id/item_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="40dp"
        android:textColor="@color/default_text_color"
        android:textSize="@dimen/textsize_12dp"
        android:text="湖南省长沙市"
        />

    <!-- line 1-->
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/eeeeee_color"
        android:layout_marginTop="75dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:background="@color/white">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="43dp"
            android:layout_weight="1"
            android:textSize="13dp"
            android:background="@null"
            android:text="编辑"/>
        <ImageView
            android:layout_width="1dp"
            android:layout_height="15dp"
            android:layout_gravity="center_vertical"
            android:background="@color/eeeeee_color"
            android:layout_weight="0"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="43dp"
            android:layout_weight="1"
            android:textSize="13dp"
            android:background="@null"
            android:text="设为默认"/>

    </LinearLayout>
    <!-- line 2-->
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:background="@color/eeeeee_color"
        android:layout_marginTop="127dp"/>

</RelativeLayout>

3、AddressAdapter.java

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

/**
 * 地址Adapter
 * Created by dwen on 2018/3/30.
 */

public class AddressAdapter extends BaseAdapter {

    List<AddressModel> addressList = new ArrayList<AddressModel>();
    Context context;

    public AddressAdapter( Context context,List<AddressModel> addressList) {
        this.addressList = addressList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return this.addressList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.addressList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        AddressModel addressModel = addressList.get(position);
        if (null == convertView){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_address,null);
            holder = new ViewHolder(convertView);
            holder.txtDefault = convertView.findViewById(R.id.item_address_txt);
            holder.txtName = convertView.findViewById(R.id.item_address_name);
            holder.txtPhone = convertView.findViewById(R.id.item_address_phone);
            holder.txtAddress = convertView.findViewById(R.id.item_address);
            //TODO 须判断 是否显示隐藏"默认"
            holder.txtDefault.setText("[默认]");
            holder.txtName.setText(addressModel.getName());
            holder.txtPhone.setText(addressModel.getPhone());
            holder.txtAddress.setText(addressModel.getAddress());
            convertView.setTag(holder);
        }else{
            //直接通过holder获取子控件,不必使用findviewbyid,加快了 UI 的响应速度
            holder = (ViewHolder) convertView.getTag();
            //TODO 须判断 是否显示隐藏"默认"
            holder.txtDefault.setText("[默认]");
            holder.txtName.setText(addressModel.getName());
            holder.txtPhone.setText(addressModel.getPhone());
            holder.txtAddress.setText(addressModel.getAddress());
        }
        return convertView;
    }

    /**
     * ViewHolder save view and speed UI
     */
    static class ViewHolder{
        TextView txtDefault,txtName,txtPhone,txtAddress;

        public ViewHolder() {

        }

        public ViewHolder(View view) {

        }
    }
}

4、AddressListActivity.java

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

/**
 * 收货地址
 * Created by dwen on 2018/3/30.
 */

public class AddressListActivity extends BaseActivity {

    private List<AddressModel> addressList = new ArrayList<>();
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_list);
        this.initAddressList();
        listView = (ListView) findViewById(R.id.address_list_view);
        AddressAdapter adapter = new AddressAdapter(AddressListActivity.this,addressList);
        listView.setAdapter(adapter);

    }

    /**
     * test address
     */
    private void initAddressList(){
        AddressModel model = new AddressModel();
        model.setIsDefault(1);
        model.setName("测试001");
        model.setPhone("13900000000");
        model.setAddress("湖南省长沙市湖南省长沙市");
        addressList.add(model);
        AddressModel model2 = new AddressModel();
        model2.setIsDefault(1);
        model2.setName("测试002");
        model2.setPhone("13900000000");
        model2.setAddress("湖南省长沙市湖南省长沙市");
        addressList.add(model2);
        AddressModel model3 = new AddressModel();
        model3.setIsDefault(1);
        model3.setName("测试003");
        model3.setPhone("13900000000");
        model3.setAddress("湖南省长沙市湖南省长沙市");
        addressList.add(model3);
        AddressModel model4 = new AddressModel();
        model4.setIsDefault(1);
        model4.setName("测试004");
        model4.setPhone("13900000000");
        model4.setAddress("湖南省长沙市湖南省长沙市");
        addressList.add(model4);
        AddressModel model5 = new AddressModel();
        model4.setIsDefault(1);
        model4.setName("测试005");
        model4.setPhone("13900000000");
        model4.setAddress("湖南省长沙市湖南省长沙市");
        addressList.add(model4);

    }
}

示例展示如下:


Android中ListView例子
 

相关推荐