标签:onresume switch getchild slist rac oid round draw pat
项目中要求列表按照ABCD这种字母排序检索的功能,看了大神写的,瞬间崇拜了,接下来借大家参考参考了
首先是自定义view sidebar
1 /** 2 * @author J 3 *一个自定义view 实现a-z的竖直绘制,和监听滑动事件 4 */ 5 public class SideBar extends View { 6 private OnTouchingLetterChangedListener onTouchingLetterChangedListener; 7 public static String[] b = { "A", "B", "C", "D", "E", "F", "G", "H", "I", 8 "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", 9 "W", "X", "Y", "Z", "#" }; 10 private int choose = -1; 11 private Paint paint = new Paint(); 12 13 private TextView mTextDialog; 14 15 public void setTextView(TextView mTextDialog) { 16 this.mTextDialog = mTextDialog; 17 } 18 19 public SideBar(Context context, AttributeSet attrs, int defStyle) { 20 super(context, attrs, defStyle); 21 } 22 23 public SideBar(Context context, AttributeSet attrs) { 24 super(context, attrs); 25 } 26 27 public SideBar(Context context) { 28 super(context); 29 } 30 31 protected void onDraw(Canvas canvas) { 32 super.onDraw(canvas); 33 int height = getHeight(); 34 int width = getWidth(); 35 int singleHeight = height / b.length; 36 37 for (int i = 0; i < b.length; i++) { 38 paint.setColor(Color.parseColor("#333333")); 39 // paint.setColor(Color.WHITE); 40 paint.setAntiAlias(true); 41 paint.setTextSize(14); 42 if (i == choose) { 43 paint.setColor(Color.parseColor("#B8B8B8")); 44 paint.setFakeBoldText(true); 45 } 46 float xPos = width / 2 - paint.measureText(b[i]) / 2; 47 float yPos = singleHeight * i + singleHeight; 48 canvas.drawText(b[i], xPos, yPos, paint); 49 paint.reset(); 50 } 51 52 } 53 54 @Override 55 public boolean dispatchTouchEvent(MotionEvent event) { 56 final int action = event.getAction(); 57 final float y = event.getY(); 58 final int oldChoose = choose; 59 final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener; 60 final int c = (int) (y / getHeight() * b.length); 61 62 switch (action) { 63 case MotionEvent.ACTION_UP: 64 setBackgroundDrawable(new ColorDrawable(0x00000000)); 65 choose = -1;// 66 invalidate(); 67 if (mTextDialog != null) { 68 mTextDialog.setVisibility(View.INVISIBLE); 69 } 70 break; 71 72 default: 73 // setBackgroundResource(R.drawable.sidebar_background); 74 if (oldChoose != c) { 75 if (c >= 0 && c < b.length) { 76 if (listener != null) { 77 listener.onTouchingLetterChanged(b[c]); 78 } 79 if (mTextDialog != null) { 80 mTextDialog.setText(b[c]); 81 mTextDialog.setVisibility(View.VISIBLE); 82 } 83 84 choose = c; 85 invalidate(); 86 } 87 } 88 89 break; 90 } 91 return true; 92 } 93 94 public void setOnTouchingLetterChangedListener( 95 OnTouchingLetterChangedListener onTouchingLetterChangedListener) { 96 this.onTouchingLetterChangedListener = onTouchingLetterChangedListener; 97 } 98 99 public interface OnTouchingLetterChangedListener { 100 void onTouchingLetterChanged(String s); 101 } 102 103 }
接下来是整个布局的部署了activity_sortlist
1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:id="@+id/layout_main" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent"> 5 6 <ListView 7 android:id="@+id/country_lvcountry" 8 android:layout_width="fill_parent" 9 android:layout_height="fill_parent" 10 android:layout_gravity="center" 11 android:divider="#eeeeee" 12 android:dividerHeight="1px" /> 13 14 <LinearLayout 15 android:id="@+id/top_layout" 16 android:layout_width="fill_parent" 17 android:layout_height="wrap_content" 18 android:background="#f2f2f2"> 19 20 <TextView 21 android:id="@+id/top_char" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:paddingBottom="3dip" 25 android:paddingLeft="10dip" 26 android:paddingTop="3dip" 27 android:text="" 28 android:textColor="#333333" 29 android:textSize="14sp" /> 30 </LinearLayout> 31 32 <TextView 33 android:id="@+id/dialog" 34 android:layout_width="80dp" 35 android:layout_height="80dp" 36 android:layout_gravity="center" 37 android:background="#999999" 38 android:gravity="center" 39 android:textColor="#ffffffff" 40 android:textSize="30dp" 41 android:visibility="invisible" /> 42 43 <com.app.activitys.view.SideBar 44 android:id="@+id/sidrbar" 45 android:layout_width="25.0dip" 46 android:layout_height="fill_parent" 47 android:layout_gravity="right|center" 48 android:layout_marginBottom="10dp" 49 android:layout_marginTop="10dp" /> 50 51 52 </FrameLayout>
接下来时使用了Sortlist
@ContentView(R.layout.activity_sortlist) public class Sortlist extends BaseAppActivity { private String[] strzimu = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", " "}; private CharacterParser characterParser; private PinyinComparator pinyinComparator; private List<MyBrandListSortModel> SourceDateList; // 数据 private BrandSortAdapter adapter; // 排序的适配器 String staus=""; private int lastFirstVisibleItem = -1; @ViewInject(R.id.country_lvcountry) private ListView sortListView; @ViewInject(R.id.sidrbar) private SideBar sideBar; @ViewInject(R.id.dialog) private TextView dialog; @ViewInject(R.id.top_layout) private LinearLayout xuanfuLayout; @ViewInject(R.id.top_char) private TextView xuanfaText; @Override protected void init() { characterParser = CharacterParser.getInstance(); pinyinComparator = new PinyinComparator(); sideBar.setTextView(dialog); SourceDateList = new ArrayList<>(); SourceDateList = filledData(SourceDateList); Collections.sort(SourceDateList, pinyinComparator);
//这是设置adapter adapter = new BrandSortAdapter(this, SourceDateList); sortListView.setAdapter(adapter); initEvent(); } @Override public void onResume() { super.onResume(); getData(); } private void initEvent() {
//listview的监听事件 sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() { @Override public void onTouchingLetterChanged(String s) { int position = adapter.getPositionForSection(s.charAt(0)); if (position != -1) { sortListView.setSelection(position); } } }); sortListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //这边是点击处理的事件,大家自己随意写啊 } }); /** * 设置滚动监听, 实时跟新悬浮的字母的值 */ sortListView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // Log.e("zzzzzzzz", "-----firstVisibleItem--->" + firstVisibleItem); int section = adapter.getSectionForPosition(firstVisibleItem); int nextSecPosition = -1; if (!String.valueOf((char) section).equals(" ")) { for (int i = 1; i < 26; i++) { if (adapter.getPositionForSection(section + i) == -1) { } else { nextSecPosition = adapter.getPositionForSection(section + i); // Log.e("nextSecPosition", "-----nextSecPosition--->"+nextSecPosition ); break; } if (i == 25) { nextSecPosition = firstVisibleItem + 1; // Log.e("nextSecPosition2", "-----nextSecPosition2--->"+nextSecPosition ); } } } if ((firstVisibleItem != lastFirstVisibleItem) && adapter.getCount() > 0) { ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) xuanfuLayout .getLayoutParams(); params.topMargin = 0; xuanfuLayout.setLayoutParams(params); xuanfaText.setText(String.valueOf((char) section)); } if (nextSecPosition == firstVisibleItem + 1) { // Log.e("nextSecPosition", "-----nextSecPosition--->" ); View childView = view.getChildAt(0); if (childView != null) { int titleHeight = xuanfuLayout.getHeight(); int bottom = childView.getBottom(); ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) xuanfuLayout .getLayoutParams(); if (bottom < titleHeight) { float pushedDistance = bottom - titleHeight; params.topMargin = (int) pushedDistance; xuanfuLayout.setLayoutParams(params); } else { if (params.topMargin != 0) { params.topMargin = 0; xuanfuLayout.setLayoutParams(params); } } } } lastFirstVisibleItem = firstVisibleItem; } }); } void getData(){
//获取数据 EGRequestParams params=new EGRequestParams();
String url="";//这边是接口 HttpUtil.post(this, url, params, new HttpUtil.Ok() { @Override public void success(String str) {
//通过modle将数据放入SourceDateList中 SourceDateList = JSON.parseArray(str, MyBrandListSortModel.class); if (SourceDateList.size() > 0) { SourceDateList = filledData(SourceDateList); Collections.sort(SourceDateList, pinyinComparator); } updataview(); } @Override public void complete(String str) { } }); } private void updataview() { adapter = new BrandSortAdapter(this, SourceDateList); sortListView.setAdapter(adapter); int section = adapter.getSectionForPosition(0); ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) xuanfuLayout .getLayoutParams(); params.topMargin = 0; xuanfuLayout.setLayoutParams(params); xuanfaText.setText(String.valueOf((char) section)); } public class PinyinComparator implements Comparator<MyBrandListSortModel> { public int compare(MyBrandListSortModel o1, MyBrandListSortModel o2) { if (o1.getSortLetters().equals(" ") || !o2.getSortLetters().equals(" ")) { return 1; } else if (!o1.getSortLetters().equals(" ") || o2.getSortLetters().equals(" ")) { return -1; } else { return o1.getSortLetters().compareTo(o2.getSortLetters()); } } } /** * 填充数据 * * @param date * @return */ private List<MyBrandListSortModel> filledData(List<MyBrandListSortModel> date) { List<MyBrandListSortModel> mSortList = new ArrayList<MyBrandListSortModel>(); List<MyBrandListSortModel> mSortListTemp = new ArrayList<MyBrandListSortModel>(); mSortListTemp = date; if (date.size() == 0) { return mSortList; } for (int i = 0; i < date.size(); i++) { String pinyin = characterParser.getSelling(date.get(i).getName()); String sortString = pinyin.substring(0, 1).toUpperCase(); if (sortString.matches("[A-Z]")) { date.get(i).setSortLetters(sortString.toUpperCase()); } else { date.get(i).setSortLetters(" "); } } for (int i = 0; i < 27; i++) { MyBrandListSortModel tempmodel = new MyBrandListSortModel(); for (int j = 0; j < date.size(); j++) { Integer current = Integer.valueOf(mSortListTemp.get(j).getSortLetters().charAt(0)); Integer current2 = Integer.valueOf(strzimu[i].charAt(0)); if (current == current2) { mSortList.add(date.get(j)); } } } return mSortList; } }
接下来把Adapter也送上吧!(BrandSortAdapter)
1 public class BrandSortAdapter extends BaseListAdapter { 2 private List<MyBrandListSortModel> list = null; 3 private Context mContext; 4 5 public BrandSortAdapter(Context context, List<MyBrandListSortModel> list) { 6 super(context, list); 7 this.list=list; 8 this.mContext=context; 9 } 10 11 public int getCount() { 12 return this.list.size(); 13 } 14 15 public Object getItem(int position) { 16 return list.get(position); 17 } 18 19 public long getItemId(int position) { 20 return position; 21 } 22 23 public View getView(final int position, View view, ViewGroup arg2) { 24 ViewHolder viewHolder = null; 25 final MyBrandListSortModel mContent = list.get(position); 26 if (view == null) { 27 viewHolder = new ViewHolder(); 28 view = LayoutInflater.from(mContext).inflate(R.layout.list_item_mycarbrand, null); 29 viewHolder.tvTitle = (TextView) view 30 .findViewById(R.id.tv_user_item_name); 31 viewHolder.tvLetter = (TextView) view.findViewById(R.id.catalog); 32 view.setTag(viewHolder); 33 } else { 34 viewHolder = (ViewHolder) view.getTag(); 35 } 36 int section; 37 if (list.size()>0){ 38 section = getSectionForPosition(position); 39 }else{ 40 section = 0; 41 } 42 if (position == getPositionForSection(section)) { 43 viewHolder.tvLetter.setVisibility(View.VISIBLE); 44 viewHolder.tvLetter.setText(mContent.getSortLetters()); 45 } else { 46 viewHolder.tvLetter.setVisibility(View.GONE); 47 } 48 MyBrandListSortModel model = list.get(position); 49 String realname="- -"; 50 if (!StringUtils.isEmpty(model.getName())){ 51 realname=model.getName(); 52 } 53 viewHolder.tvTitle.setText(realname); 54 // 头像采用imageloader加载 55 56 return view; 57 58 } 59 60 final static class ViewHolder { 61 TextView tvLetter; 62 TextView tvTitle; 63 } 64 65 /** 66 * 得到首字母的ascii值 67 */ 68 public int getSectionForPosition(int position) { 69 return list.size()>0?list.get(position). 70 getSortLetters(). 71 charAt(0):Integer.valueOf(‘ ‘); 72 73 74 } 75 76 public int getPositionForSection(int section) { 77 for (int i = 0; i < getCount(); i++) { 78 String sortStr = list.get(i).getSortLetters(); 79 char firstChar = sortStr.toUpperCase().charAt(0); 80 if (firstChar == section) { 81 return i; 82 } 83 } 84 85 return -1; 86 } 87 88 public String getAlpha(String str) { 89 String sortStr = str.trim().substring(0, 1).toUpperCase(); 90 if (sortStr.matches("[A-Z]")) { 91 return sortStr; 92 } else { 93 return " "; 94 } 95 } 96 97 public Object[] getSections() { 98 return null; 99 } 100 }
modle也送给大家吧!MyBrandListSortModel
1 public class MyBrandListSortModel extends Entity implements Serializable{ 2 3 private String id; 4 private String children; 5 private String name; 6 private String parentid; 7 private String parentids; 8 private String parentname; 9 private String type; 10 private String value; 11 private String sortLetters; 12 13 public String getId() { 14 return id; 15 } 16 17 public void setId(String id) { 18 this.id = id; 19 } 20 21 public String getSortLetters() { 22 return sortLetters; 23 } 24 25 public void setSortLetters(String sortLetters) { 26 this.sortLetters = sortLetters; 27 } 28 29 public void setChildren(String children) { 30 this.children = children; 31 } 32 public String getChildren() { 33 return children; 34 } 35 36 public void setName(String name) { 37 this.name = name; 38 } 39 40 public String getName() { 41 return name; 42 } 43 44 public void setParentid(String parentid) { 45 this.parentid = parentid; 46 } 47 48 public String getParentid() { 49 return parentid; 50 } 51 52 public void setParentids(String parentids) { 53 this.parentids = parentids; 54 } 55 56 public String getParentids() { 57 return parentids; 58 } 59 60 public void setParentname(String parentname) { 61 this.parentname = parentname; 62 } 63 64 public String getParentname() { 65 return parentname; 66 } 67 68 public void setType(String type) { 69 this.type = type; 70 } 71 72 public String getType() { 73 return type; 74 } 75 76 public void setValue(String value) { 77 this.value = value; 78 } 79 80 public String getValue() { 81 return value; 82 } 83 }
大家参考吧!
标签:onresume switch getchild slist rac oid round draw pat
原文地址:http://www.cnblogs.com/wangying222/p/6490982.html