ListView example in Android

The following is a complete example of 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="Add new address"
            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="[default]"/>
    <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="Changsha City, Hunan Province"
        />

    <!-- 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="edit"/>
        <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="Set as default"/>

    </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;

/**
 * AddressAdapter
 * 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 must determine whether to show or hide "default"
            holder.txtDefault.setText("[默认]");
            holder.txtName.setText(addressModel.getName());
            holder.txtPhone.setText(addressModel.getPhone());
            holder.txtAddress.setText(addressModel.getAddress());
            convertView.setTag (holder);
        }else{
            //Get the child control directly through the holder, without using findviewbyid, which speeds up the response speed of the UI
            holder = (ViewHolder) convertView.getTag();
            //TODO must determine whether to show or hide "default"
            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;

/**
 * Shipping address
 * 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("Test 001");
        model.setPhone("13900000000");
        model.setAddress("Changsha City, Hunan Province, Hunan Province");
        addressList.add(model);
        AddressModel model2 = new AddressModel();
        model2.setIsDefault(1);
        model2.setName("Test 002");
        model2.setPhone("13900000000");
        model2.setAddress("Changsha City, Hunan Province, Hunan Province");
        addressList.add(model2);
        AddressModel model3 = new AddressModel();
        model3.setIsDefault(1);
        model3.setName("Test 003");
        model3.setPhone("13900000000");
        model3.setAddress("Changsha City, Hunan Province, Hunan Province");
        addressList.add(model3);
        AddressModel model4 = new AddressModel();
        model4.setIsDefault(1);
        model4.setName("Test 004");
        model4.setPhone("13900000000");
        model4.setAddress("Changsha City, Hunan Province, Hunan Province");
        addressList.add(model4);
        AddressModel model5 = new AddressModel();
        model4.setIsDefault(1);
        model4.setName("Test 005");
        model4.setPhone("13900000000");
        model4.setAddress("Changsha City, Hunan Province, Hunan Province");
        addressList.add(model4);

    }
}

 

An example is shown below:



 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326004717&siteId=291194637