【Android】ListView控件总结

       ListView控件是列表视图展示,排列方式是纵向。其中ListView控件有自己默认的布局,当默认布局不能满足我们要求时,我们可以进行自定义控件,下面我们详细分析ListView控件的使用。

一、默认效果

思路:在layout下创建一个布局,添加ListView控件,在Activity中对ListView控件赋值,其中ListView的方法setAdapter起了举足轻重的作用,而setAdapter()方法中需要传入一个adapter类型的数据,从此可以进入了童话世界。

其中官方解释文档是这样说的:

/**
     * Sets the data behind this ListView.
     *
     * The adapter passed to this method may be wrapped by a {@link WrapperListAdapter},
     * depending on the ListView features currently in use. For instance, adding
     * headers and/or footers will cause the adapter to be wrapped.
     *
     * @param adapter The ListAdapter which is responsible for maintaining the
     *        data backing this list and for producing a view to represent an
     *        item in that data set.
     *
     * @see #getAdapter()
     */

其大意是:这个适配器通过这个方法可以封装成你具有你自己特点的ListView控件

      Activity详解

package com.first.tglistviewdemo.ListViewDefault;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.first.tglistviewdemo.R;

public class ListViewDefaultActivity extends AppCompatActivity {
    private ListView lv;
    private String[] fruit = {
            "apple",
            "pear",
            "Orange",
            "Cherry",
            "Mango",
            "apple",
            "pear",
            "Orange",
            "Cherry",
            "Mango",
            "apple",
            "pear",
            "Orange",
            "Cherry",
            "Mango",
            "apple",
            "pear",
            "Orange",
            "Cherry",
            "Mango"
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview_default);
        lv = findViewById(R.id.lv_1);
        ArrayAdapter arrayAdapter = new ArrayAdapter(ListViewDefaultActivity.this, android.R.layout.simple_list_item_1, fruit);
        lv.setAdapter(arrayAdapter);  //
        //修改标题栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle("默认的ListView样式");
        }

        //点击事件
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(ListViewDefaultActivity.this,"position"+position+":"+fruit[position]+",id"+id,Toast.LENGTH_SHORT).show();
            }
        });

        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(ListViewDefaultActivity.this,"position"+position+":"+fruit[position]+",id"+id,Toast.LENGTH_SHORT).show();
                return true;  //fasle 长按点击回调函数不会销毁,会继续执行click  true:长按回调函数将销毁
            }
        });
    }
}

 

【Android】ListView控件总结

二、自定义效果

自定义效果只不过在setAdapter()方法中的参数动了下手脚

思路:我们创建一个adapter的子类,其所有的adapter都是继承自BaseAdapter的,同样的我们只要继承一个其adapter类即可

 /**
     * Get a View that displays the data at the specified position in the data set. You can either
     * create a View manually or inflate it from an XML layout file. When the View is inflated, the
     * parent View (GridView, ListView...) will apply default layout parameters unless you use
     * {@link android.view.LayoutInflater#inflate(int, android.view.ViewGroup, boolean)}
     * to specify a root view and to prevent attachment to the root.
     * 
     * @param position The position of the item within the adapter's data set of the item whose view
     *        we want.
     * @param convertView The old view to reuse, if possible. Note: You should check that this view
     *        is non-null and of an appropriate type before using. If it is not possible to convert
     *        this view to display the correct data, this method can create a new view.
     *        Heterogeneous lists can specify their number of view types, so that this View is
     *        always of the right type (see {@link #getViewTypeCount()} and
     *        {@link #getItemViewType(int)}).
     * @param parent The parent that this view will eventually be attached to
     * @return A View corresponding to the data at the specified position.
     */

官方是这样解释的:在数据集中得到一个子列,你同样可以创建一个视图或者填充这个视图来之XML布局,当这个视图控件被填充,将会提供一个默认的布局样式,除非你用了一个自己的布局

package com.first.tglistviewdemo.ListViewDefault;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.first.tglistviewdemo.R;

import java.util.List;

public class LvdAdapter extends ArrayAdapter<Fruit> {
    private int resourceid;
    public LvdAdapter(Context context, int resourceViewId, List<Fruit> data) {
        super(context, resourceViewId,data);
        resourceid=resourceViewId;//当前自己构造的Layout布局的ID
    }

    static class ViewHolder{
        private TextView fname;
        private TextView price;
        private TextView protime;
    }

    @Override
    public View getView(int position, View convertView,  ViewGroup parent) {
        Fruit fruit=getItem(position);
        Toast.makeText(getContext(),fruit.getFname(),Toast.LENGTH_SHORT).show();
        ViewHolder viewHolder;
        if(convertView==null){
            convertView= LayoutInflater.from(getContext()).inflate(resourceid,null);
            viewHolder=new ViewHolder();
            viewHolder.fname= convertView.findViewById(R.id.txt_fname);
            viewHolder.price=convertView.findViewById(R.id.txt_price);
            viewHolder.protime=convertView.findViewById(R.id.txt_protime);
            convertView.setTag(viewHolder);
        }else{
            convertView=convertView;
            viewHolder= (ViewHolder) convertView.getTag();
        }
        viewHolder.fname.setText(fruit.getFname());
        viewHolder.protime.setText(fruit.getPrice());
        viewHolder.price.setText(fruit.getPrice());
        return convertView;
    }
}