首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

view拖动、listview加载、touch事件散发

2013-03-17 
view拖动、listview加载、touch事件分发package com.xiaxing.slidingdrawerimport java.util.ArrayListimp

view拖动、listview加载、touch事件分发

package com.xiaxing.slidingdrawer;import java.util.ArrayList;import android.content.Context;import android.util.Log;import android.view.KeyEvent;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.SurfaceView;import android.view.View;import android.view.ViewGroup;import android.view.animation.Animation;import android.view.animation.TranslateAnimation;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.FrameLayout;import android.widget.ListView;import android.widget.TextView;import com.xiaxing.slidingdrawer.R;public class LeftListView implements View.OnClickListener {    public static final int DIRECTION_LEFT = 0;    public static final int DIRECTION_RIGHT = 1;    public static final int DIRECTION_UP = 2;    public static final int DIRECTION_DOWN = 3;    private static final String TAG = "xiaxing";    private Context mContext;    private final LayoutInflater mInflater;    private ListView mAchivementList;    private ListView mFriendList;    private AchivementAdapter mAchivementAdapter;    private FriendAdapter mFriendAdapter;    private boolean mIsVisible = true;    private Button mAchivementBtn;    private Button mFriendBtn;    private boolean mIsFriendListVisable = false;        private FrameLayout mContainer;    public interface OnVisibilityChangedListener {        public void onVisibilityChanged(boolean visbility);    }    public LeftListView(Context context, FrameLayout rootView) {        mContext = context;        mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        mContainer = rootView;        if (this.mContainer == null)             Log.e("xiaxing", "mContainer is null");        mInflater.inflate(R.layout.anyclock_layout_achivement_list, mContainer);        mFriendList = (ListView)(mContainer.findViewById(R.id.friend_view));        mAchivementList = (ListView)(mContainer.findViewById(R.id.achivement_view));        mAchivementBtn = (Button)(mContainer.findViewById(R.id.btn_achivement));        mAchivementBtn.setOnClickListener(this);        mFriendBtn = (Button)(mContainer.findViewById(R.id.btn_friend));        mFriendBtn.setOnClickListener(this);    }    public boolean isVisible() {        return mIsVisible;    }    public void testPreference() {        ArrayList<String> list = new ArrayList<String>();        for (int i = 0, n = 10; i < n; ++i) {            list.add("item");        }        mFriendAdapter = new FriendAdapter(mContext, list);        mFriendList.setAdapter(mFriendAdapter);        mFriendList.setOnItemClickListener(mFriendAdapter);        mAchivementAdapter = new AchivementAdapter(mContext, list);        mAchivementList.setAdapter(mAchivementAdapter);        mAchivementList.setOnItemClickListener(mAchivementAdapter);    }        public void processTouchEvent(MotionEvent event) {        Log.v(TAG, "LeftListView onTouch");//        mContainer.onTouchEvent(event);        this.mContainer.dispatchTouchEvent(event);    }    private class FriendAdapter extends BaseAdapter            implements OnItemClickListener {        private final ArrayList<String> mPreferences;        public FriendAdapter(Context context, ArrayList<String> preferences) {            mPreferences = preferences;        }        public void onItemClick(                AdapterView<?> parent, View view, int position, long id) {            // Preference preference = mPreferences.get(position);            // TODO::            Log.v(TAG, "MainMenuAdapter -- " + "onItemClick :  " + position);        }        public View getView(int position, View convertView, ViewGroup parent) {            convertView = inflateIfNeed(convertView, R.layout.anyclock_content_achivement_item, parent, false);            TextView musictime = (TextView)convertView.findViewById(R.id.grid_music_time);            musictime.setText("20120000");            TextView musicname = (TextView)convertView.findViewById(R.id.grid_music_name);            musicname.setText("好友名称");            TextView artist = (TextView)convertView.findViewById(R.id.grid_music_artist);            artist.setText("你是我的好友吗");            return convertView;        }        @Override        public boolean areAllItemsEnabled() {            return false;        }        // @Override        // public boolean isEnabled(int position) {        // Preference preference = mPreferences.get(position);        // return !(preference instanceof PreferenceGroup);        // }        public int getCount() {            return mPreferences.size();        }        public Object getItem(int position) {            return null;        }        public long getItemId(int position) {            return position;        }        // @Override        // public int getItemViewType(int position) {        // Preference pref = mPreferences.get(position);        // if (pref instanceof PreferenceGroup)        // return 0;        // if (pref instanceof ListPreference)        // return 1;        // throw new IllegalStateException();        // }        @Override        public int getViewTypeCount() {            // we have two types, see getItemViewType()            return 2;        }        @Override        public boolean hasStableIds() {            return true;        }        @Override        public boolean isEmpty() {            return mPreferences.isEmpty();        }    }    private class AchivementAdapter extends BaseAdapter            implements OnItemClickListener {        private final ArrayList<String> mPreferences;        public AchivementAdapter(Context context, ArrayList<String> preferences) {            mPreferences = preferences;        }        public void onItemClick(                AdapterView<?> parent, View view, int position, long id) {            // Preference preference = mPreferences.get(position);            // TODO::            Log.v(TAG, "MainMenuAdapter -- " + "onItemClick :  " + position);        }        public View getView(int position, View convertView, ViewGroup parent) {            convertView = inflateIfNeed(convertView, R.layout.anyclock_content_achivement_item, parent, false);            TextView musictime = (TextView)convertView.findViewById(R.id.grid_music_time);            musictime.setText("20120000");            TextView musicname = (TextView)convertView.findViewById(R.id.grid_music_name);            musicname.setText("成就名称");            TextView artist = (TextView)convertView.findViewById(R.id.grid_music_artist);            artist.setText("成就说明:早起的鸟儿啊???");            return convertView;        }        @Override        public boolean areAllItemsEnabled() {            return false;        }        // @Override        // public boolean isEnabled(int position) {        // Preference preference = mPreferences.get(position);        // return !(preference instanceof PreferenceGroup);        // }        public int getCount() {            return mPreferences.size();        }        public Object getItem(int position) {            return null;        }        public long getItemId(int position) {            return position;        }        // @Override        // public int getItemViewType(int position) {        // Preference pref = mPreferences.get(position);        // if (pref instanceof PreferenceGroup)        // return 0;        // if (pref instanceof ListPreference)        // return 1;        // throw new IllegalStateException();        // }        @Override        public int getViewTypeCount() {            // we have two types, see getItemViewType()            return 2;        }        @Override        public boolean hasStableIds() {            return true;        }        @Override        public boolean isEmpty() {            return mPreferences.isEmpty();        }    }    private View inflateIfNeed(            View view, int resource, ViewGroup root, boolean attachToRoot) {        if (view != null)            return view;        return mInflater.inflate(resource, root, attachToRoot);    }    public static Animation slideOut(View view, int to) {        view.setVisibility(View.INVISIBLE);        Animation anim;        switch (to) {            case DIRECTION_LEFT:                anim = new TranslateAnimation(0, -view.getWidth() + 20, 0, 0);                break;            case DIRECTION_RIGHT:                anim = new TranslateAnimation(0, view.getWidth(), 0, 0);                break;            case DIRECTION_UP:                anim = new TranslateAnimation(0, 0, 0, -view.getHeight());                break;            case DIRECTION_DOWN:                anim = new TranslateAnimation(0, 0, 0, view.getHeight());                break;            default:                throw new IllegalArgumentException(Integer.toString(to));        }        anim.setDuration(500);        view.startAnimation(anim);        return anim;    }    public static Animation slideIn(View view, int from) {        view.setVisibility(View.VISIBLE);        Animation anim;        switch (from) {            case DIRECTION_LEFT:                anim = new TranslateAnimation(-view.getWidth(), 0, 0, 0);                break;            case DIRECTION_RIGHT:                anim = new TranslateAnimation(view.getWidth(), 0, 0, 0);                break;            case DIRECTION_UP:                anim = new TranslateAnimation(0, 0, -view.getHeight(), 0);                break;            case DIRECTION_DOWN:                anim = new TranslateAnimation(0, 0, view.getHeight(), 0);                break;            default:                throw new IllegalArgumentException(Integer.toString(from));        }        anim.setDuration(500);        view.startAnimation(anim);        return anim;    }    @Override    public void onClick(View v) {        // TODO Auto-generated method stub        if (v.equals(mFriendBtn)) {            Log.v(TAG, "mAchivementBtn clicked");            slideIn(mFriendList, DIRECTION_RIGHT);            mIsFriendListVisable = true;        } else if (v.equals(mAchivementBtn)) {            if (mIsFriendListVisable) {                slideOut(mFriendList, DIRECTION_RIGHT);                mIsFriendListVisable = false;            }            Log.v(TAG, "mFriendBtn clicked");        }    }}

?

?

?

package com.xiaxing.slidingdrawer;import com.xiaxing.slidingdrawer.R;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.GestureDetector;import android.view.Menu;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.widget.FrameLayout;import android.widget.ImageView;public class MainActivity extends Activity implements OnClickListener, OnTouchListener {    private LeftListView mAchivementList;    private ImageView mGripBtn;    private View mListView;    private boolean mIsListViewVisale = false;    private GestureDetector mGestureDetector;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main_activity);        findViewById(R.id.viewtag).setOnTouchListener(this);    }    @SuppressWarnings("deprecation")    @Override    public void onStart() {        super.onStart();        showAchivementList();        mGestureDetector = new GestureDetector(new MyGestureListener());    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    }    public void showAchivementList() {        if (mAchivementList == null) {            FrameLayout fl = (FrameLayout)findViewById(R.id.mainview);            mAchivementList = new LeftListView(this, fl);            mAchivementList.testPreference();        }    }    @Override    public void onClick(View view) {        // TODO Auto-generated method stub        if (view.getId() == mGripBtn.getId()) {            Log.v("xiaxing", "main activity onClick");            if (mIsListViewVisale) {                LeftListView.slideIn(mListView, LeftListView.DIRECTION_LEFT);                mIsListViewVisale = false;            } else {                LeftListView.slideOut(mListView, LeftListView.DIRECTION_LEFT);                mIsListViewVisale = true;            }        }    }        @Override    public boolean onTouch(View v, MotionEvent event) {        mAchivementList.processTouchEvent(event);        return mGestureDetector.onTouchEvent(event);    }        private int verticalMinDistance = 120;    private int minVelocity = 0;    private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {        @Override        public boolean onDown(MotionEvent arg0) {            // TODO Auto-generated method stub//            Log.v("xiaxing", "onDown 1");//            if (mIsListViewVisale)//                return true;//            else//                return false;                        return true;        }        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {            Log.v("xiaxing", "onFling");            if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {                Log.v("xiaxing", "onFling 2");                if (!mIsListViewVisale) {                    LeftListView.slideOut(mListView, LeftListView.DIRECTION_LEFT);                    mIsListViewVisale = false;                    }                            } else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {                Log.v("xiaxing", "onFling 1");                if (mIsListViewVisale) {                    LeftListView.slideIn(mListView, LeftListView.DIRECTION_LEFT);                    mIsListViewVisale = false;                }            }                        return true;                    }        @Override        public void onLongPress(MotionEvent arg0) {            // TODO Auto-generated method stub            Log.v("xiaxing", "onLongPress 2");        }        @Override        public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) {            // TODO Auto-generated method stub            Log.v("xiaxing", "onScroll 1");            return true;        }        @Override        public void onShowPress(MotionEvent arg0) {            // TODO Auto-generated method stub            Log.v("xiaxing", "onShowPress 2");        }        @Override        public boolean onSingleTapUp(MotionEvent arg0) {            // TODO Auto-generated method stub            Log.v("xiaxing", "onSingleTapUp 2");            return true;        }    }}

?

?

?

package com.xiaxing.slidingdrawer;import android.view.View;import android.view.animation.Animation;import android.view.animation.TranslateAnimation;public class Util {    private static final String TAG = "Util";    public static final int DIRECTION_LEFT = 0;    public static final int DIRECTION_RIGHT = 1;    public static final int DIRECTION_UP = 2;    public static final int DIRECTION_DOWN = 3;        public static Animation slideOut(View view, int to) {        view.setVisibility(View.INVISIBLE);        Animation anim;        switch (to) {            case DIRECTION_LEFT:                anim = new TranslateAnimation(0, -view.getWidth(), 0, 0);                break;            case DIRECTION_RIGHT:                anim = new TranslateAnimation(0, view.getWidth(), 0, 0);                break;            case DIRECTION_UP:                anim = new TranslateAnimation(0, 0, 0, -view.getHeight());                break;            case DIRECTION_DOWN:                anim = new TranslateAnimation(0, 0, 0, view.getHeight());                break;            default:                throw new IllegalArgumentException(Integer.toString(to));        }        anim.setDuration(500);        view.startAnimation(anim);        return anim;    }    public static Animation slideIn(View view, int from) {        view.setVisibility(View.VISIBLE);        Animation anim;        switch (from) {            case DIRECTION_LEFT:                anim = new TranslateAnimation(-view.getWidth(), 0, 0, 0);                break;            case DIRECTION_RIGHT:                anim = new TranslateAnimation(view.getWidth(), 0, 0, 0);                break;            case DIRECTION_UP:                anim = new TranslateAnimation(0, 0, -view.getHeight(), 0);                break;            case DIRECTION_DOWN:                anim = new TranslateAnimation(0, 0, view.getHeight(), 0);                break;            default:                throw new IllegalArgumentException(Integer.toString(from));        }        anim.setDuration(500);        view.startAnimation(anim);        return anim;    }        public static boolean equals(Object a, Object b) {        return (a == b) || (a == null ? false : a.equals(b));    }        public static <T> T checkNotNull(T object) {        if (object == null) throw new NullPointerException();        return object;    }}

?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/main_layout"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@android:color/black" >    <RelativeLayout        android:id="@+id/main_panel"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_toLeftOf="@+id/btn_gripper_hide"        android:background="#ffffff00" >        <FrameLayout            android:id="@+id/list_view"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_marginLeft="10dp"            android:layout_marginRight="10dp"            android:layout_toEndOf="@+id/btn_list"            android:background="@android:color/black" >            <ListView                android:id="@+id/friend_view"                android:layout_width="fill_parent"                android:layout_height="fill_parent" />            <ListView                android:id="@+id/achivement_view"                android:layout_width="fill_parent"                android:layout_height="fill_parent" />        </FrameLayout>        <LinearLayout            android:id="@+id/btn_list"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"            android:background="@android:color/black"            android:orientation="horizontal" >            <Button                android:id="@+id/btn_achivement"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:layout_weight="1"                android:text="成就" />            <Button                android:id="@+id/btn_friend"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:layout_weight="1"                android:text="好友" />            <Button                android:id="@+id/btn_more"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:layout_weight="1"                android:text="更多" />        </LinearLayout>    </RelativeLayout>    <ImageView        android:id="@+id/btn_gripper_hide"        android:layout_width="20dp"        android:layout_height="fill_parent"        android:layout_alignParentRight="true" />    <ImageView        android:id="@+id/touch_mask"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_alignParentRight="true" /></RelativeLayout>

?

<?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:orientation="horizontal"    android:paddingTop="0dip"    android:background="@android:color/black" >    <FrameLayout        android:layout_width="70dip"        android:layout_height="70dip"        android:layout_gravity="left"        android:gravity="right"        android:paddingBottom="5dip"        android:paddingLeft="5dip"        android:paddingRight="5dip"        android:paddingTop="5dip" >        <ImageView            android:id="@+id/grid_item_img"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_gravity="center"            android:scaleType="centerCrop"            android:src="@drawable/anyshare_content_music_grid_item_bg" />        <ImageView            android:id="@+id/grid_item_shared"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_gravity="center"            android:background="#77e0e0e0"            android:visibility="invisible" />    </FrameLayout>    <FrameLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_gravity="left|center_vertical"        android:gravity="left|center_vertical" >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_gravity="left|center_vertical"            android:layout_marginRight="40dip"            android:gravity="left|center_vertical"            android:orientation="vertical" >            <LinearLayout                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:orientation="horizontal" >                <TextView                    android:id="@+id/grid_music_name"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_marginTop="0dip"                    android:layout_weight="1"                    android:gravity="left|center_vertical"                    android:singleLine="true"                    android:textColor="#ff7c7c7c"                    android:textSize="18sp" />                <TextView                    android:id="@+id/grid_music_time"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_gravity="right|center_vertical"                    android:layout_weight="0"                    android:gravity="right"                    android:singleLine="true"                    android:textColor="#ff7c7c7c"                    android:textSize="13sp" />            </LinearLayout>            <LinearLayout                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:orientation="horizontal" >                <TextView                    android:id="@+id/grid_music_artist"                    android:layout_width="0dip"                    android:layout_height="wrap_content"                    android:layout_gravity="left|center_vertical"                    android:layout_weight="1"                    android:gravity="left"                    android:singleLine="true"                    android:maxLines="2"                    android:textColor="#ff7c7c7c"                    android:textSize="13sp" />            </LinearLayout>        </LinearLayout>        <ImageView            android:id="@+id/grid_item_check"            android:layout_width="30dip"            android:layout_height="30dip"            android:layout_gravity="right|center_vertical"            android:paddingBottom="0dip"            android:paddingRight="10dip"            android:src="@drawable/anyshare_photo_check_on" />    </FrameLayout></LinearLayout>

?

?

?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/main_layout"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@android:color/black" >    <FrameLayout        android:id="@+id/mainview"        android:layout_width="fill_parent"        android:layout_height="fill_parent" />    <ImageView        android:id="@+id/viewtag"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_alignParentRight="true" /></RelativeLayout>

?

热点排行