zoukankan      html  css  js  c++  java
  • 安卓侧面滑动组件设计

    要想做一个跟通讯录类似的在右侧实现滑动,

    看看界面

    下面看一下实现的代码:

    friend.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background"
        android:orientation="vertical" >
        
        <include layout="@layout/friend_header"/>
        
        <RelativeLayout 
            android:orientation="vertical"
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent">
            
        <ListView android:id="@+id/list_view" 
    		android:layout_height="wrap_content" 
    		android:layout_width="fill_parent"
    		android:scrollbars="none"
    		android:cacheColorHint="@color/white"
    		></ListView>
        
        <com.example.widget.MyLetterListView
        	android:id="@+id/my_list_view" 
    		android:background="@drawable/corner"  
    		android:layout_width="30dip" 
    		android:layout_height="fill_parent"
    		android:layout_alignParentRight="true" />
        
        </RelativeLayout>
    </LinearLayout> 

    friend_header.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
       	android:layout_width="fill_parent"
       	android:layout_height="wrap_content"
       	android:paddingLeft="2dip"
       	android:paddingRight="2dip"
       	android:gravity="center_vertical"
       	android:orientation="horizontal"
       	android:background="@drawable/widget_bar_bg">
       	<TextView 
       		android:id="@+id/friend_search_head_title"
       		style="@style/detail_head_title"
       		android:layout_gravity="center"
       		android:clickable="true"
       		android:textSize="20sp"
       		android:text="@string/my_friend"
       		></TextView>
     
       		<ImageView
       		android:id="@+id/friend_center_back"
    	   	android:layout_width="wrap_content" 
    	   	android:layout_height="wrap_content" 
    	   	android:layout_gravity="right|center"
    	   	android:clickable="true"
    	   	android:textColor="@color/white"
    	   	android:background="@drawable/add_friend" 
    	   	></ImageView>
       		
    </FrameLayout>


    这里主要把组件构建出来,滑动功能还没有实现

    MyLetterListView.java

    package com.example.widget;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Typeface;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;
    
    public class MyLetterListView extends View
    {
    	OnTouchingLetterChangedListener onTouchingLetterChangedListener;
    	String[] b = {"@","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","#"};
    	int choose = -1;
    	Paint paint = new Paint();
    	boolean showBkg = false;
    
    	public MyLetterListView(Context context, AttributeSet attrs, int defStyle) {
    		super(context, attrs, defStyle);
    		
    	}
    
    	public MyLetterListView(Context context, AttributeSet attrs) {
    		super(context, attrs);
    		
    	}
    
    	public MyLetterListView(Context context) {
    		super(context);
    	}
    	
    	protected void onDraw(Canvas canvas) {
    		super.onDraw(canvas);
    		if(showBkg){
    		    canvas.drawColor(Color.parseColor("#40000000"));
    		}
    		
    	    int height = getHeight();
    	    int width = getWidth();
    	    int singleHeight = height / b.length;
    	    for(int i=0;i<b.length;i++){
    	       paint.setColor(Color.BLACK);
    	       paint.setTypeface(Typeface.DEFAULT_BOLD);
    	       paint.setAntiAlias(true);
    	       if(i == choose){
    	    	   paint.setColor(Color.parseColor("#3399ff"));
    	    	   paint.setFakeBoldText(true);
    	       }
    	       float xPos = width/2  - paint.measureText(b[i])/2;
    	       float yPos = singleHeight * i + singleHeight;
    	       
    	       canvas.drawText(b[i], xPos, yPos, paint);
    	       
    	       paint.reset();
    	    }
    	   
    	}
    	
    	@Override
    	public boolean dispatchTouchEvent(MotionEvent event) {
    		final int action = event.getAction();
    	    final float y = event.getY();
    	    final float x = event.getX();
    	    final int oldChoose = choose;
    	    final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
    	    final int c = (int) (y/getHeight()*b.length);
    	    
    		switch (action) {
    			case MotionEvent.ACTION_DOWN:
    				showBkg = true;
    				if(oldChoose != c && listener != null){
    					if(c > 0 && c< b.length){
    						listener.onTouchingLetterChanged(b[c],y,x);
    						choose = c;
    						invalidate();
    					}
    				}
    				
    				break;
    			case MotionEvent.ACTION_MOVE:
    				if(oldChoose != c && listener != null){
    					if(c > 0 && c< b.length){
    						listener.onTouchingLetterChanged(b[c],y,x);
    						choose = c;
    						invalidate();
    					}
    				}
    				break;
    			case MotionEvent.ACTION_UP:
    				showBkg = false;
    				choose = -1;
    				listener.onTouchingLetterEnd();
    				invalidate();
    				break;
    		}
    		return true;
    	}
    
    	@Override
    	public boolean onTouchEvent(MotionEvent event) {
    		return super.onTouchEvent(event);
    	}
    
    	public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
    		this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
    	}
    
    	public interface OnTouchingLetterChangedListener{
    		public void onTouchingLetterEnd();
    		public void onTouchingLetterChanged(String s,float y,float x);
    	}
    }
    



  • 相关阅读:
    JQuery基础知识--方便忘记时查看
    关于jquery.validate.js的用法
    JQuery ajax提交表单及表单验证
    thinkphp rabc权限总结
    关于jquery ajax项目总结
    中国剩余定理及其拓展 CRT&EXGCD
    bzoj 4899 记忆的轮廓 题解(概率dp+决策单调性优化)
    bzoj3307 雨天的尾巴题解及改题过程(线段树合并+lca+树上差分)
    20190614考试心态爆炸记
    fhq Treap(无旋Treap)
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3084582.html
Copyright © 2011-2022 走看看