Android 台灣中文網

標題: 實現IOS圓角風格的列表ListView [打印本頁]

作者: 暗桌之光    時間: 2013-7-3 16:04
標題: 實現IOS圓角風格的列表ListView
這段代碼目前已經加在我的一個jar包androidkit中,還沒發佈。
適用於android1.6以上,不依賴其他jar包

使用時不需要繼承這裡的RoundListAdapter。只需要在你實現了ListAdapter的類中,傳入一個RoundParams的對象,並在getView方法返回前調用這裡RoundListAdapter類提供的靜態方法。
RoundListAdapter.setItemBackground(position, switcherView, mParams,
getCount());
  1. /*
  2. * @(#)RoundListAdapter.java                       Project:com.sinaapp.msdxblog.androidkit
  3. * Date:2012-12-6
  4. *
  5. * Copyright (c) 2011 CFuture09, Institute of Software,
  6. * Guangdong Ocean University, Zhanjiang, GuangDong, China.
  7. * All rights reserved.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. *  you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. *     http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. package com.lurencun.cfuture09.androidkit.widget.roundlist;

  22. import android.view.View;
  23. import android.widget.ListAdapter;

  24. /**
  25. * @author Geek_Soledad ([email protected])
  26. */
  27. public abstract class RoundListAdapter implements ListAdapter {
  28.         /**
  29.          * 圓角ListView的參數類。定義了頂部背景,底部背景,中間背景及單獨一個時的背景。
  30.          *
  31.          * @author msdx
  32.          *
  33.          */
  34.         public static class RoundParams {
  35.                 public int topResid;
  36.                 public int middleResid;
  37.                 public int bottomResid;
  38.                 public int lonelyResid;

  39.                 public RoundParams(int topResid, int middleReside, int bottomResid,
  40.                                 int lonelyResid) {
  41.                         this.topResid = topResid;
  42.                         this.middleResid = middleReside;
  43.                         this.bottomResid = bottomResid;
  44.                         this.lonelyResid = lonelyResid;
  45.                 }
  46.         }

  47.         public static void setItemBackground(int position, View item,
  48.                         final RoundParams mParams, final int count) {
  49.                 if (count == 1) {
  50.                         item.setBackgroundResource(mParams.lonelyResid);
  51.                 } else if (position > 0 && position < count - 1) {
  52.                         item.setBackgroundResource(mParams.middleResid);
  53.                 } else if (position == 0) {
  54.                         item.setBackgroundResource(mParams.topResid);
  55.                 } else {
  56.                         item.setBackgroundResource(mParams.bottomResid);
  57.                 }
  58.         }
  59. }
複製代碼
  1. /*  
  2. * @(#)LocalAdapter.java              Project:RTKSETTINGS  
  3. * Date:2013-1-9  
  4. *  
  5. * Copyright (c) 2013 Geek_Soledad.  
  6. * All rights reserved.  
  7. *  
  8. * Licensed under the Apache License, Version 2.0 (the "License");  
  9. *  you may not use this file except in compliance with the License.  
  10. * You may obtain a copy of the License at  
  11. *  
  12. *     http://www.apache.org/licenses/LICENSE-2.0  
  13. *  
  14. * Unless required by applicable law or agreed to in writing, software  
  15. * distributed under the License is distributed on an "AS IS" BASIS,  
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  17. * See the License for the specific language governing permissions and  
  18. * limitations under the License.  
  19. */
  20. package com.realtek.msdx.rtksettings.view;

  21. import java.util.ArrayList;
  22. import java.util.List;

  23. import android.app.TvManager;
  24. import android.content.Context;
  25. import android.view.View;
  26. import android.view.ViewGroup;
  27. import android.widget.BaseAdapter;

  28. import com.lurencun.cfuture09.androidkit.widget.roundlist.RoundListAdapter;
  29. import com.lurencun.cfuture09.androidkit.widget.roundlist.RoundListAdapter.RoundParams;
  30. import com.realtek.msdx.rtksettings.activity.MainActivity;
  31. import com.realtek.msdx.rtksettings.bean.LocalSettingsBean;

  32. /**
  33. * @author Geek_Soledad ([email protected])
  34. */
  35. public class LocalAdapter extends BaseAdapter {

  36.         private RoundParams mParams;
  37.         private Context mContext;

  38.         public LocalAdapter(Context context, RoundParams params) {
  39.                 super();
  40.                 mContext = context;
  41.                 mParams = params;
  42.         }

  43.         @Override
  44.         public int getCount() {
  45.                 return 5;
  46.         }

  47.         @Override
  48.         public Object getItem(int position) {
  49.                 return position;
  50.         }

  51.         @Override
  52.         public long getItemId(int position) {
  53.                 return position;
  54.         }

  55.         @Override
  56.         public View getView(int position, View convertView, ViewGroup parent) {
  57.                 // 在這裡建立view,
  58.                 //SwitcherTextView view = new SwitcherTextView(mContext);
  59.                 // 然後在返回view前進行調用
  60.                 RoundListAdapter.setItemBackground(position, view, mParams,
  61.                                 getCount());
  62.                 return view;
  63.         }
  64. }
複製代碼





歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1