1. 程式人生 > >安卓開發-ListView的介面卡和監聽器/ExpandableListView的使用

安卓開發-ListView的介面卡和監聽器/ExpandableListView的使用

《一:BaseAdapter的簡單使用》

1.定義類MyAdapter,繼承BaseAdapter:

public class MyAdapter extends BaseAdapter {
    //將佈局檔案轉換成View物件
    private LayoutInflater mLayoutInflater;
    //ListView中的值
    private List<String> mDatas;



    //在構造器中將值傳進並初始化成員變數
    public MyAdapter(Context c) {
        mLayoutInflater = LayoutInflater.from(c);
    }

    //告訴ListView預設顯示幾條資料
public int getCount() { return mDatas != null ? mDatas.size() : 0; } //ListView中若子元素有多個元件組成,應定義一個元件快取類 class ViewHolder{ TextView v1; TextView v2; } //只有當getCount不為0時才會被多次呼叫, //position:ListView的索引 //convertView:快取的ItemView,最底層露出一點或不露,會預載入,當滑動了一個時才不為空
//ViewGroup:每個ItemView的容器,返回的view直接新增到容器中 public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; //開始為空,預加載出來後才不為空 if(convertView == null){ //將一個子佈局轉換成view,然後才能對子佈局中的控制元件賦值等操作 convertView = mLayoutInflater.inflate(android.R.layout.simple_list_item_2, null
); viewHolder = new ViewHolder(); viewHolder.v1 = (TextView) convertView.findViewById(android.R.id.text1); viewHolder.v2 = (TextView) convertView.findViewById(android.R.id.text2); convertView.setTag(viewHolder); }else{ viewHolder = (ViewHolder) convertView.getTag(); } //將傳進來的值與子佈局中的控制元件繫結 viewHolder.v1.setText(mDatas.get(position)); viewHolder.v2.setText(mDatas.get(position)); //返回的是整個引用的子佈局物件,而不是裡面的一個小控制元件 return convertView; } public void setDatas(List<String> datas){ mDatas = datas; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } }
2.在MainActivity中呼叫MyAdapter:
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //1.獲取ListView控制元件
        ListView listView = (ListView)findViewById(R.id.lv);

        //2.給列表ListView設定值
        List<String> datas = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            datas.add("介面" + i);
        }

        //3.傳入Context物件建立介面卡物件,並與ListView繫結
        MyAdapter adapter = new MyAdapter(this);
        listView.setAdapter(adapter);

        //4.將資料傳到介面卡中:
        adapter.setDatas(datas);

        //5.資料一般是從伺服器傳送而來,有延遲現象,呼叫此方法會執行getCount,getView
        adapter.notifyDataSetChanged();
    }

}

《二:ArrayAdapter》

ListView listView = (ListView)findViewById(R.id.lv);

ArrayAdapter<String> adpater = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1,datas);

listView.setAdapter(adapter);

《三:SimpleAdapter》

1.在MainActivity中:

ListView listView = (ListView) findViewById(R.id.lv);

        List<Map<String, Object>> datas = new ArrayList<>();
        for (int i = 1; i <= 7; i++) {
            Map<String, Object> dataMap = new HashMap<>();
            datas.add(dataMap);
            dataMap.put("pic", R.drawable.a1);
            dataMap.put("textView", "文字" + i);
        }

        SimpleAdapter simpleAdapter = new SimpleAdapter(this, datas,
                R.layout.simpleadapter_item_layout, new String[] { "pic",
                        "textView" }, new int[]{ R.id.iv, R.id.tv });
        listView.setAdapter(simpleAdapter);

2.simpleadapter_item_layout.xml中:

<?xml version="1.0" encoding="utf-8"?>"E:/java/OpenSource/code/AndroidApp/SimpleAdapter/res/layout/activity_main.xml"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="center_vertical" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#F00"
        android:layout_margin="20dp" />

</LinearLayout>

《四:ListView的監聽器》

1.滑動監聽器:
ListView.setOnScrollListener(new OnScrollListener(){
    //1.滑動狀態改變時呼叫:
    //OnScrollListener.SCROLL_STATE_FLING : 慣性滑動
    //OnScrollListener.SCROLL_STATE_IDLE  : 不動
    //OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:拖拽滑動
    public void onScrollStateChanged(AbsListView view, int scrollState){

    }
    //2.列表滑動就會呼叫此函式:列表顯示第一項,可見幾項,總共項數
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount){
    }
2.點選監聽器:
//1.在MyAdapter中:
public Object getItem(int position){
   return mDatas != null ? mDatas.get(position) : null);
   }


//2.在MainActivity中:
ListView.setOnItemClickListener(new OnItemClickListener(){
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
    String item = (String)adapter.getItem(position);
    }
     });

《五:ExpandableListView》

1.建立介面卡類:
public class MyAdapter extends BaseExpandableListAdapter {
    private LayoutInflater mInflater;

    private List<String> mGroupNames;
    private Map<String, List<String>> mChildNames;

    public MyAdapter(Context c, List<String> groupNames, Map<String, List<String>> childNames){
        mInflater = LayoutInflater.from(c);

        mGroupNames = groupNames;
        mChildNames = childNames;
    }

    public int getGroupCount() {
        return mGroupNames.size();
    }

    public int getChildrenCount(int groupPosition) {
        return mChildNames.size();
    }

    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        TextView view = null;
        if(convertView == null){
            convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null);
            view = (TextView)convertView.findViewById(android.R.id.text1);

            convertView.setTag(view);
        }else{
            view = (TextView)convertView.getTag();
        }

        view.setText(mGroupNames.get(groupPosition));

        return convertView;
    }

    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        TextView view = null;
        if(convertView == null){
            convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null);
            view = (TextView)convertView.findViewById(android.R.id.text1);

            convertView.setTag(view);
        }else{
            view = (TextView)convertView.getTag();
        }
        String groupName = mGroupNames.get(groupPosition);
        List<String> childNames = mChildNames.get(groupName);
        view.setText(childNames.get(childPosition));

        return convertView;
    }





    public Object getGroup(int groupPosition) {
        return null;
    }

    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    public long getGroupId(int groupPosition) {
        return 0;
    }

    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    public boolean hasStableIds() {
        return false;
    }


    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

}
2.在MainActivity中:
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //1.得ExpandableListView物件
        ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.ev);

        //2.建立每組的名字集合
        List<String> groupNames = new ArrayList<>();
        //3.建立每組的中的資料map
        Map<String,List<String>> childNames = new HashMap<>();
        for (int i = 0; i < 4; i++) {
            groupNames.add("Group" + i);
        }
        for (int i = 0; i < groupNames.size(); i++) {
            String groupName = groupNames.get(i);
            List<String> childs = new ArrayList<>();
            for (int j = 0; j < 4; j++) {
                childs.add("Child" + j);
            }
            childNames.put(groupName, childs);
        }

        //4.建立介面卡,並繫結資料
        MyAdapter adapter = new MyAdapter(this, groupNames, childNames);
        //5.將列表與介面卡繫結
        expandableListView.setAdapter(adapter);
    }

}