2011.04.14——— android 可伸缩的listview 但不是ExpandableList

这个activity的功能 就类似于经常见到的js手风琴菜单一样的效果


activity代码:

package com.lp.activity;import java.util.List;import android.app.ListActivity;import android.content.Context;import android.graphics.Color;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.TextView;import com.lp.bean.Help;import com.lp.dao.HelpDao;public class HelpActivity extends ListActivity {private HelpDao dao;private List<Help> helps;private boolean[] mExpanded;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);dao = new HelpDao(this);helps = dao.getAll();mExpanded = new boolean[helps.size()];for(int i=0;i<helps.size();i++){mExpanded[i] = false;}setListAdapter(new SpeechListAdapter(this));}@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {((SpeechListAdapter)getListAdapter()).toggle(position);}//定义一个LinearLayout 上下结构  下面的textview可显示可隐藏private class SpeechView extends LinearLayout {        public SpeechView(Context context, String title, String dialogue, boolean expanded) {            super(context);                        this.setOrientation(VERTICAL);                        // Here we build the child views in code. They could also have            // been specified in an XML file.                        mTitle = new TextView(context);            //mTitle.setTextColor(R.color.title);            mTitle.setTextColor(Color.RED);            mTitle.setTextSize(20);            mTitle.setText(title);            addView(mTitle, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));                        mDialogue = new TextView(context);            mDialogue.setTextSize(15);            mDialogue.setText(dialogue);            addView(mDialogue, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));                        mDialogue.setVisibility(expanded ? VISIBLE : GONE);        }                /**         * Convenience method to set the title of a SpeechView         */        public void setTitle(String title) {            mTitle.setText(title);        }                /**         * Convenience method to set the dialogue of a SpeechView         */        public void setDialogue(String words) {            mDialogue.setText(words);        }                /**         * Convenience method to expand or hide the dialogue         */        public void setExpanded(boolean expanded) {            mDialogue.setVisibility(expanded ? VISIBLE : GONE);        }                private TextView mTitle;        private TextView mDialogue;    }private class SpeechListAdapter extends BaseAdapter {        public SpeechListAdapter(Context context)        {            mContext = context;        }                /**         * The number of items in the list is determined by the number of speeches         * in our array.         *          * @see android.widget.ListAdapter#getCount()         */        public int getCount() {            return helps.size();        }        /**         * Since the data comes from an array, just returning         * the index is sufficent to get at the data. If we         * were using a more complex data structure, we         * would return whatever object represents one          * row in the list.         *          * @see android.widget.ListAdapter#getItem(int)         */        public Object getItem(int position) {            return position;        }        /**         * Use the array index as a unique id.         * @see android.widget.ListAdapter#getItemId(int)         */        public long getItemId(int position) {            return position;        }        /**         * Make a SpeechView to hold each row.         * @see android.widget.ListAdapter#getView(int, android.view.View, android.view.ViewGroup)         */        public View getView(int position, View convertView, ViewGroup parent) {        Help help = helps.get(position);        String name = help.getName();        String desc = help.getDescr();            SpeechView sv;            if (convertView == null) {                sv = new SpeechView(mContext, name, desc, mExpanded[position]);            } else {                sv = (SpeechView)convertView;                sv.setTitle(name);                sv.setDialogue(desc);                sv.setExpanded(mExpanded[position]);            }                        return sv;        }        public void toggle(int position) {            mExpanded[position] = !mExpanded[position];            notifyDataSetChanged();        }                /**         * Remember our context so we can use it when constructing views.         */        private Context mContext;                   }}

更多相关文章

  1. Android实现伸缩弹力分布菜单效果
  2. Android UI技巧(一)——Android中伸缩自如的9patch图片切法,没有美
  3. 2010.12.29——— android 可伸缩的listview ExpandableList
  4. 关键(一): android不同机型的适配的解决方案之按比例伸缩篇
  5. android背景自动伸缩
  6. 【转】android 有效解决achartengine在scrollview中伸缩的问题
  7. Android 使用CoordinatorLayout+AppBarLayout+CollapsingToolbar
  8. android ExpandableListView伸缩列表
  9. Android伸缩动画xml配置的代码展示

随机推荐

  1. Android SQLite详解及示例代码
  2. 2014.08.05 ——— android Gson JsonDes
  3. python系列学习六——移动开发
  4. SlidingMenu 在Android Studio使用详解
  5. Android(安卓)WebView 中遇到的问题集锦
  6. Android 布局文件属性讲解
  7. android采用pull解析器解析和生成XML
  8. Android - AutoCompleteTextView (输入框
  9. 1.活用Android线程间通信的Message机制
  10. 简单分析Android中添加shortcut方面的源