android ListView 自定义 BaseAdapter

先上效果图

技术分享

 

由上面的效果图可以看出:页面中的LISTVIEW的每一项,都是由两个TEXTVIEW组成,初始化的时候,只显示第一个TEXTVIEW,在这里我们成为标题,而内容部分的setVisible属性为"GONE"。为Ite添加onClick监听器,在每次点击的时候,显示内容部分,这里需要调用BaseAdapter的notifyDataSetChanged()进行刷新显示。

 

代码部分:

 

技术分享
 1 class MyLayout extends LinearLayout {
 2 
 3     private TextView mTitle;
 4     private TextView mWords;
 5 
 6     public MyLayout(Context context, String title, String words,
 7             boolean mVisible) {
 8         super(context);
 9         this.setOrientation(VERTICAL);
10 
11         mTitle = new TextView(context);
12         mTitle.setText(title);
13         addView(mTitle, new LinearLayout.LayoutParams(
14                 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
15 
16         mWords = new TextView(context);
17         mWords.setText(words);
18         addView(mWords, new LinearLayout.LayoutParams(
19                 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
20         mWords.setVisibility(mVisible ? View.VISIBLE : View.GONE);
21     }
22 
23     public void setTitle(String title) {
24         mTitle.setText(title);
25     }
26 
27     public void setWords(String words) {
28         mWords.setText(words);
29     }
30 
31     public void setWordsVisible(boolean mVisible) {
32         mWords.setVisibility(mVisible ? View.VISIBLE : View.GONE);
33     }
34 
35 }
MyLayout

 

* 继承 LinearLayout 在其中添加两个TextView,通过 mVisible ? View.VISIBLE : View.GONE 控制内容是否显示。Tips:这里并没有使用invisible属性。

 

技术分享
 1 class MyBaseAdapter extends BaseAdapter {
 2     private String[] titles = { "标题一", "标题二", "标题三", "标题四", "标题五" };
 3     private String[] words = { "我是内容1,我是内容1", "我是内容2,我是内容2", "我是内容3,我是内容3",
 4             "我是内容4,我是内容4", "我是内容5,我是内容5" };
 5     private boolean[] flags = { false, false, false, false, false };
 6     private Context mContext;
 7 
 8     public MyBaseAdapter(Context context) {
 9         mContext = context;
10     }
11 
12     @Override
13     public int getCount() {
14         // TODO Auto-generated method stub
15         return titles.length;
16     }
17 
18     @Override
19     public Object getItem(int position) {
20         // TODO Auto-generated method stub
21         return position;
22     }
23 
24     @Override
25     public long getItemId(int position) {
26         // TODO Auto-generated method stub
27         return position;
28     }
29 
30     @Override
31     public View getView(int position, View convertView, ViewGroup parent) {
32         // TODO Auto-generated method stub
33         MyLayout mLayout;
34         if (convertView == null) {
35             mLayout = new MyLayout(mContext, titles[position], words[position],
36                     flags[position]);
37         }else{
38             mLayout = (MyLayout) convertView;
39             mLayout.setTitle(titles[position]);
40             mLayout.setWords(words[position]);
41             mLayout.setWordsVisible(flags[position]);
42         }
43         return mLayout;
44     }
45     
46     public void toggler(int position){
47         flags[position] = !flags[position];
48         notifyDataSetChanged(); 
49     }
50     
51     
52     
53 }
MyBaseAdapter

 

* 重写getView()方法,完成初始化 

 

技术分享
 1 public class BaseAdapterActivity extends ListActivity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         // TODO Auto-generated method stub
 6         super.onCreate(savedInstanceState);
 7         this.setListAdapter(new MyBaseAdapter(this));
 8     }
 9 
10     @Override
11     protected void onListItemClick(ListView l, View v, int position, long id) {
12         ((MyBaseAdapter)getListAdapter()).toggler(position);
13     }
14     
15 }
MainActivity

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。