读书人

通讯录侧边栏高速滑动效果的实现

发布时间: 2012-06-26 10:04:13 作者: rapoo

通讯录侧边栏快速滑动效果的实现
那,我原创的文章比较少,所以基本上都是自己看了,然后稍加修改的!

综合这两个帖子:
http://blog.csdn.net/ilysony/article/details/6292771
http://blog.csdn.net/yan_daoqiu/article/details/6393300

中文的还没去实验
[img]
我不知道怎么传图 - - !
[/img]


大概思路就是在listView里面设置了 section,然后侧边栏是一个自定义的组件

侧边栏ontouch中会去 让listview 跳到相应位置list.setSelection();

listview中设置onScroll监听器,滑动时候让中间弹出windows,不滑动时候隐藏

window中显示的就是首字母。

侧边栏代码

public class SideBar extends View{private char[] l;      private SectionIndexer sectionIndexter = null;      private ListView list;      private final int m_nItemHeight = 29;      public SideBar(Context context) {          super(context);          init();      }      public SideBar(Context context, AttributeSet attrs) {          super(context, attrs);          init();      }      private void init() {          l = new char[] { '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' };          setBackgroundColor(0x44FFFFFF);      }      public SideBar(Context context, AttributeSet attrs, int defStyle) {          super(context, attrs, defStyle);          init();      }      public void setListView(ListView _list) {          list = _list;          sectionIndexter = (SectionIndexer) _list.getAdapter();      }      public boolean onTouchEvent(MotionEvent event) {          super.onTouchEvent(event);          int i = (int) event.getY();          int idx = i / m_nItemHeight;          if (idx >= l.length) {              idx = l.length - 1;          } else if (idx < 0) {              idx = 0;          }          if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {              if (sectionIndexter == null) {                  sectionIndexter = (SectionIndexer) list.getAdapter();              }              int position = sectionIndexter.getPositionForSection(l[idx]);              if (position == -1) {                  return true;              }              list.setSelection(position);          }          return true;      }      protected void onDraw(Canvas canvas) {          Paint paint = new Paint();          paint.setColor(0xFFA6A9AA);          paint.setTextSize(20);          paint.setTextAlign(Paint.Align.CENTER);          float widthCenter = getMeasuredWidth() / 2;          for (int i = 0; i < l.length; i++) {              canvas.drawText(String.valueOf(l[i]), widthCenter, m_nItemHeight + (i * m_nItemHeight), paint);          }          super.onDraw(canvas);      }  }



listView的adapter
涉及到section的显示 以及 sidebar中要获取positon的方法


public class MyAdapter extends BaseAdapter implements SectionIndexer{private ArrayList<String> stringArray;      private Context context;      public MyAdapter(Context _context, ArrayList<String> arr) {          stringArray = arr;          context = _context;      }      public int getCount() {          return stringArray.size();      }      public Object getItem(int arg0) {          return stringArray.get(arg0);      }      public long getItemId(int arg0) {          return 0;      }      public View getView(int position, View v, ViewGroup parent) {          LayoutInflater inflate = ((Activity) context).getLayoutInflater();          View view = (View) inflate.inflate(R.layout.listview_row, null);          LinearLayout header = (LinearLayout) view.findViewById(R.id.section);          String label = stringArray.get(position);          char firstChar = label.toUpperCase().charAt(0);          if (position == 0) {              setSection(header, label);          } else {              String preLabel = stringArray.get(position - 1);              char preFirstChar = preLabel.toUpperCase().charAt(0);              if (firstChar != preFirstChar) {                  setSection(header, label);              } else {                  header.setVisibility(View.GONE);              }          }          TextView textView = (TextView) view.findViewById(R.id.textView);          textView.setText(label);          return view;      }      private void setSection(LinearLayout header, String label) {          TextView text = new TextView(context);          header.setBackgroundColor(0xffaabbcc);          text.setTextColor(Color.WHITE);          text.setText(label.substring(0, 1).toUpperCase());          text.setTextSize(20);          text.setPadding(5, 0, 0, 0);          text.setGravity(Gravity.CENTER_VERTICAL);          header.addView(text);      }      public int getPositionForSection(int section) {          if (section == 35) {              return 0;          }          for (int i = 0; i < stringArray.size(); i++) {              String l = stringArray.get(i);              char firstChar = l.toUpperCase().charAt(0);              if (firstChar == section) {                  return i;              }          }          return -1;      }      public int getSectionForPosition(int arg0) {          return 0;      }      public Object[] getSections() {          return null;      }  }



主程序的中显示的window相关:

TextView overlay = (TextView) View.inflate(this, R.layout.overlay, null);         getWindowManager() .addView(                        overlay, new WindowManager.LayoutParams(                                 LayoutParams.WRAP_CONTENT,                                 LayoutParams.WRAP_CONTENT,                                 WindowManager.LayoutParams.TYPE_APPLICATION,                                 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE                                         | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,                                     PixelFormat.TRANSLUCENT)                        );                list.setOnScrollListener(onScroll);OnScrollListener onScroll = new OnScrollListener() {    boolean visible;    @Override     public void onScrollStateChanged(AbsListView view, int scrollState) {         visible = true;         if (scrollState == ListView.OnScrollListener.SCROLL_STATE_IDLE) {             overlay.setVisibility(View.INVISIBLE);         }     }    @Override     public void onScroll(AbsListView view, int firstVisibleItem,             int visibleItemCount, int totalItemCount) {         if (visible) {             overlay.setText(list.getItemAtPosition(firstVisibleItem).toString().substring(0, 1));             overlay.setVisibility(View.VISIBLE);         }     }};






读书人网 >移动开发

热点推荐