自定义listview的使用与性能优化

今天工作内容主要是listview相关的,简单的使用就不再赘述,现在总结一下自定义listview的使用步骤:
第一步,创建一个类来描述将要在listview的item中所显示内容的类Goods,这里只放了一个名字,可以根据需要放任意多(额,只是这么说说),源码示例:

public class Goods
{
    private String goodsName;

    public Goods(String goodsName)
    {
        this.goodsName = goodsName;
    }

    public String getGoodsName()
    {
        return goodsName;
    }
}

第二步,创建布局文件listview_goods_item,由于上面的类只有一个name,所以呢,布局也只要一个textview即可,源码示例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/lv_goods_layerlist"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/goods_name"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:gravity="center_vertical"
        android:textSize="17sp"
        android:textColor="#474747" >
    </TextView>

</LinearLayout>

第三步,自定义适配器GoodsAdapter,直接上源码:

public class GoodsAdapter extends ArrayAdapter<Goods>
{
    private int resourceId;

    public GoodsAdapter(Context context, int tvGoodsNameResourceId, List<Goods> objects)
    {
        super(context, tvGoodsNameResourceId, objects);
        resourceId = tvGoodsNameResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        Goods goods = getItem(position); // 获取当前项的Goods实例

        View view = LayoutInflater.from(getContext()).inflate(resourceId, null);

        TextView fruitName = (TextView) view.findViewById(R.id.goods_name);
        fruitName.setText(goods.getGoodsName());

        return view;
    }

    @Override
    public void notifyDataSetChanged()
    {
        super.notifyDataSetChanged();
    }

}

第四步,activity中如下方式调用:

public class MainGoodsFragment extends BaseFragment implements OnClickListener
{
    private Button btnAll, btnClassify;

    private List<Goods> goodsList = new ArrayList<Goods>();

    GoodsAdapter goodsAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_main_goods, container, false);

        btnAll = (Button) view.findViewById(R.id.btnAll);
        btnClassify = (Button) view.findViewById(R.id.btnClassify);

        btnAll.setOnClickListener(this);
        btnClassify.setOnClickListener(this);

        initGoods();

        goodsAdapter = new GoodsAdapter(getActivity(), R.layout.listview_goods_item, goodsList);
        ListView listView = (ListView) view.findViewById(R.id.lvShowGoods);
        listView.setAdapter(goodsAdapter);

        return view;
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.btnAll:

                initGoods();
                goodsAdapter.notifyDataSetChanged();

                break;

            case R.id.btnClassify:

                updateList();

                break;

            default:
                break;
        }
    }

    /**
     * 初始化全部商品数据源list
     */
    private void initGoods()
    {
        goodsList.clear();

        Goods apple = new Goods("Apple");
        goodsList.add(apple);

        Goods banana = new Goods("Banana");
        goodsList.add(banana);

        Goods orange = new Goods("Orange");
        goodsList.add(orange);

    }

    /**
     * 重新填写数据源list内容,刷新listview,实现动态加载内容
     */
    private void updateList()
    {
        goodsList.clear();

        Goods classify1 = new Goods("鲜果蔬菜");
        goodsList.add(classify1);

        Goods classify2 = new Goods("智能家电");
        goodsList.add(classify2);

        Goods classify3 = new Goods("厨卫用品");
        goodsList.add(classify3);

        Goods classify4 = new Goods("图书音像");
        goodsList.add(classify4);

        goodsAdapter.notifyDataSetChanged();
    }

}

上面可以实现listview的展示,以及动态刷新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值