1. 程式人生 > >ListView的展開合併

ListView的展開合併

ListView的多佈局
ListView的展開和收起是在上一篇ListView的多佈局的基本上寫,整體佈局只有一個ListView。
今天、昨天、一週內、更早最多隻有一個是展開的狀態,預設展開的是今天。
當點選今天時,則今天合併,即此時所有的都是合併狀態;再次點選今天時,則今天展開;
當點選其他圖片時,今天合併,則被點選的展開。

實現效果:
這裡寫圖片描述

一、實現思路
1、ListView的狀態,ListView只有都合併和某一個開啟2種狀態。
2、當本次點選的上一次點選的是同一張圖片時,若當前處於合併狀態則展開,若當前處於展開狀態則合併;
當本次點選的和上次點選的圖片不同時,則本次點選的圖片展開,上次點選的圖片則要處於合併狀態。
因此,我定義了3個變數boolean isopen用於記錄當前的狀態,展開還是合併,
int last_click上次點選的圖片,int cur_click本次點選的圖片。

二、程式碼實現
XML程式碼:

 <ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@null"
    android:listSelector="#0000"
    android:scrollbars="none" />

Activity程式碼:

public class ListViewActivity extends Activity
{
private List<List<Object>> list; private List<Object> data; private ListViewAdapter adapter; private ListView listView; private int last_click; private boolean isopen; private int[] image = { R.mipmap.ic_today_gaokao, R.mipmap.ic_yesterday, R.mipmap.ic_week, R.mipmap.ic_earlier }; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_view); init(); loadData(); } private void loadData() { List<Object> list1 = new ArrayList<>(); list1.add(image[0]); list1.add("Android高階1"); list1.add("Android高階2"); list1.add("Android高階3"); list1.add("Android高階4"); list.add(list1); list1 = new ArrayList<>(); list1.add(image[1]); list1.add("Android基礎2"); list1.add("Android基礎3"); list1.add("Android基礎4"); list.add(list1); list1 = new ArrayList<>(); list1.add(image[2]); list1.add("Android基礎1"); list.add(list1); list1 = new ArrayList<>(); list1.add(image[3]); list1.add("Java基礎1"); list1.add("Java基礎2"); list1.add("Java基礎3"); list.add(list1); //設定預設第一個展開 getData(0); } private void init() { listView = (ListView) findViewById(R.id.listView); list = new ArrayList<>(); data = new ArrayList<>(); adapter = new ListViewAdapter(this, data); listView.setAdapter(adapter); last_click = -1; isopen = true; /* 為ListView Item中的ImageView和TextView設定點選事 通過設定的tag,找到點選是某項 */ adapter.setOnItemClick(new View.OnClickListener() { @Override public void onClick(View v) { Object tag = v.getTag(); if (tag instanceof Integer) { int img = (int) tag; for (int i = 0; i < image.length; i++) { if (img == image[i]) { getData(i); break; } } } else if (tag instanceof String) { String ret = (String) tag; Toast.makeText(ListViewActivity.this, "點選了 " + ret, Toast.LENGTH_SHORT).show(); } } }); } private void getData(int index) { //清空資料 data.clear(); /* 若當前處於展開狀態,本次點選和上次點選相同, 當前點選的狀態展開變成合並,合併變成展開; 本次點選和上次點選不同,則當前點選的狀態時展開; 若當前處於合併狀態,則當前點選的處於展開狀態。 */ if (isopen) { if (index == last_click) { isopen = !isopen; } else { isopen = true; } } else { isopen = true; } for (int i = 0; i < list.size(); i++) { /* 通過修改資料的長度來設定當前的展開和合並狀態。 只要點選當前圖片處於展開狀態,資料長度為list.size(), 否則長度為1。 */ int len = list.get(i).size(); if (i != index || !isopen) { len = 1; } for (int j = 0; j < len; j++) { data.add(list.get(i).get(j)); } } Log.i("info","資料=" + "cur_click=" + index + ",last_click=" + last_click + ",isopen=" + isopen); last_click = index; adapter.notifyDataSetChanged(); } }
Adapter程式碼:
public class ListViewAdapter extends BaseAdapter {
    private Context context;
    private List<Object> list;
    private View.OnClickListener onItemClick;

    public ListViewAdapter(Context context, List<Object> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (list != null) {
            ret = list.size();
        }
        return ret;
    }

    @Override
    public int getItemViewType(int position) {
        int ret = 0;
        Object object = list.get(position);
        if (object instanceof String) {
            ret = 0;
        } else if (object instanceof Integer) {
            ret = 1;
        }
        return ret;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

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

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

    public void setOnItemClick(View.OnClickListener onItemClick) {
        this.onItemClick = onItemClick;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (getItemViewType(position) == 0) {
            convertView = bindTv(position, convertView, parent);
        } else if (getItemViewType(position) == 1) {
            convertView = bindImg(position, convertView, parent);
        }
        return convertView;
    }

    private View bindImg(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_recent_img, null);
            holder = new ViewHolder();
            holder.img = (ImageView) convertView.findViewById(R.id.img_recent);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Object object = list.get(position);
        if (object instanceof Integer) {
            int img = (int) object;
            holder.img.setImageResource(img);
        }
        holder.img.setTag(object);
        holder.img.setOnClickListener(onItemClick);
        return convertView;
    }

    private View bindTv(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_recent_tv, null);
            holder = new ViewHolder();
            holder.tv = (TextView) convertView.findViewById(R.id.tv_recent);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Object object = list.get(position);
        if (object instanceof String) {
            String ret = (String) object;
            holder.tv.setText(ret);
        }
        holder.tv.setTag(object);
        holder.tv.setOnClickListener(onItemClick);
        return convertView;
    }

    class ViewHolder {
        TextView tv;
        ImageView img;
    }
}