android ListView 自訂 BaseAdapter,androidbaseadapter

來源:互聯網
上載者:User

android ListView 自訂 BaseAdapter,androidbaseadapter

先上

 

由上面的可以看出:頁面中的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 @Override13 public int getCount() {14 // TODO Auto-generated method stub15 return titles.length;16 }17 18 @Override19 public Object getItem(int position) {20 // TODO Auto-generated method stub21 return position;22 }23 24 @Override25 public long getItemId(int position) {26 // TODO Auto-generated method stub27 return position;28 }29 30 @Override31 public View getView(int position, View convertView, ViewGroup parent) {32 // TODO Auto-generated method stub33 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 @Override11 protected void onListItemClick(ListView l, View v, int position, long id) {12 ((MyBaseAdapter)getListAdapter()).toggler(position);13 }14 15 }MainActivity

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.