凌宇的项目之旅

来源:互联网 发布:f117和f22 知乎 编辑:程序博客网 时间:2024/04/30 00:44

新接了一个项目,感觉有必要做做笔记了。不然老是忘记东西,这脑袋...

首先说的是BaseActivity,这个感觉挺重要的,把一些基础的东西抽取出来进行封装,先上代码,纯净版的base,以后后续会再加入新的东西。

import android.app.Activity;import android.content.Context;import android.content.pm.ActivityInfo;import android.os.Bundle;import android.os.IBinder;import android.view.MotionEvent;import android.view.View;import android.view.Window;import android.view.inputmethod.InputMethodManager;import android.widget.EditText;public class BaseActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        requestWindowFeature(Window.FEATURE_NO_TITLE);        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);        super.onCreate(savedInstanceState);    }    @Override    public boolean dispatchTouchEvent(MotionEvent ev) {        if (ev.getAction() == MotionEvent.ACTION_DOWN) {            // 获得当前得到焦点的View,一般情况下就是EditText(特殊情况就是轨迹求或者实体案件会移动焦点)            View v = getCurrentFocus();            if (isShouldHideInput(v, ev)) {                hideSoftInput(v.getWindowToken());            }        }        return super.dispatchTouchEvent(ev);    }    /**     * 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘,因为当用户点击EditText时没必要隐藏     *     * @param v     * @param event     * @return     */    private boolean isShouldHideInput(View v, MotionEvent event) {        if (v != null && (v instanceof EditText)) {            int[] l = {0, 0};            v.getLocationInWindow(l);            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left                    + v.getWidth();            if (event.getX() > left && event.getX() < right                    && event.getY() > top && event.getY() < bottom) {                // 点击EditText的事件,忽略它。                return false;            } else {                return true;            }        }        // 如果焦点不是EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditView上,和用户用轨迹球选择其他的焦点        return false;    }    /**     * 多种隐藏软件盘方法的其中一种     *     * @param token     */    private void hideSoftInput(IBinder token) {        if (token != null) {            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);            im.hideSoftInputFromWindow(token,                    InputMethodManager.HIDE_NOT_ALWAYS);        }    }}

这个主要是实现了输入框的问题而已。


接下来是用到的SlidingMenu的导入与使用的方法,感觉这个还是挺重要的,首先是文件的导入,我就不贴github地址了,待会上传一个zip。

导入library包的方法的话,应该不算难,我用的as2.0,所以还算可以。步骤就不写了,要是这个都忘记了,我跳楼算了。

导入之后记得先别makeproject,先把build.grade里面的配置统一成我们的项目配置,比如classpath,min,tar等等。

然后记得设置依赖,就可以makeproject了。 至此导包完成,这个方法可以适用于普遍的library包。


SlideMenu的话,

1,可以直接在xml中构建一个view,然后跟使用其他view一样,findViewByid,然后进行各种属性设置,然后就可以了。然而这种方法不太好,毕竟代码冗余。而且不好操作。

2,可以继承SlideActivity,通过getSlideMenu()的方法得到一个SlideMenu对象。然后就可以进行如方法1的操作了。

3,上面三种我都没贴代码,因为我一开始就是这么写的,然后觉得好麻烦,一块块的代码好恶心。我们要怎么办呢!抽出来,然后封印到Fragment里面去,对应的操作我们就放在Fragment里面去,在MainActivity里面我们就只有设置布局的代码: 

代码如下:

  3.1 侧滑的fragment:

import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;import com.messcat.qijisuoping.R;import com.messcat.qijisuoping.utils.TsUtils;import com.messcat.qijisuoping.widget.CircleImageView;/** * Created by LHT on 2016/4/27. */public class L_LeftMenu extends Fragment {    private View mView;    private CircleImageView circleLeftmenuUserpic;    private TextView tvLeftmenuUsername;    private TextView tvLeftmenuUserjifen;    private ListView lvLeftmenuList;    private LeftMenuApdater leftMenuApdater;    private int icons[] = new int[]{R.mipmap.l_tasklist, R.mipmap.l_messagecenter, R.mipmap.l_electronicmall            , R.mipmap.l_gamblinghouse, R.mipmap.l_invitefriends, R.mipmap.l_contactsus};    private int menus[] = new int[]{R.string.Tast_List, R.string.Message_Center, R.string.Electronic_Mall            , R.string.Gambling_House, R.string.Invite_Friends, R.string.Contact_Us};    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {        if (mView == null) {            mView = inflater.inflate(R.layout.l_leftmenulayout, container, false);        }        return mView;    }    @Override    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {        if (view == null) {            return;        }        circleLeftmenuUserpic = (CircleImageView) view.findViewById(R.id.circle_leftmenu_userpic);        tvLeftmenuUsername = (TextView) view.findViewById(R.id.tv_leftmenu_username);        tvLeftmenuUserjifen = (TextView) view.findViewById(R.id.tv_leftmenu_userjifen);        lvLeftmenuList = (ListView) view.findViewById(R.id.lv_leftmenu_list);        leftMenuApdater = new LeftMenuApdater();        lvLeftmenuList.setAdapter(leftMenuApdater);        lvLeftmenuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {                TsUtils.ToastShort(getString(menus[i]));            }        });        super.onViewCreated(view, savedInstanceState);    }    private class LeftMenuApdater extends BaseAdapter {        @Override        public int getCount() {            if (icons.length != menus.length) {                return 0;            } else {                return icons.length;            }        }        @Override        public Object getItem(int i) {            return null;        }        @Override        public long getItemId(int i) {            return 0;        }        @Override        public View getView(int i, View view, ViewGroup viewGroup) {            View mView = view;            LeftMenuHolder leftMenuHolder;            if (mView == null) {                mView = View.inflate(getActivity(), R.layout.l_leftmenu_item, null);                leftMenuHolder = new LeftMenuHolder();                leftMenuHolder.menuitemIcon = (ImageView) mView.findViewById(R.id.menuitem_icon);                leftMenuHolder.menuitemPoint = (ImageView) mView.findViewById(R.id.menuitem_point);                leftMenuHolder.menuitemMenustr = (TextView) mView.findViewById(R.id.menuitem_menustr);                mView.setTag(leftMenuHolder);            } else {                leftMenuHolder = (LeftMenuHolder) mView.getTag();            }            leftMenuHolder.menuitemIcon.setImageResource(icons[i]);            leftMenuHolder.menuitemMenustr.setText(menus[i]);            leftMenuHolder.menuitemPoint.setVisibility(View.GONE);            return mView;        }        private class LeftMenuHolder {            private ImageView menuitemIcon;            private ImageView menuitemPoint;            private TextView menuitemMenustr;        }    }}

 3.2 相对应的xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/leftmenu_bgcolor"    android:orientation="vertical">    <LinearLayout        android:id="@+id/ll_pic"        android:layout_width="match_parent"        android:layout_height="140dp"        android:orientation="horizontal">        <com.messcat.qijisuoping.widget.CircleImageView            android:id="@+id/circle_leftmenu_userpic"            android:layout_width="95dp"            android:layout_height="95dp"            android:layout_marginLeft="25dp"            android:layout_marginTop="25dp"            android:src="@mipmap/l_tempwillian"            app:border_color="@color/leftmenu_itemselected"            app:border_width="2dp" />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_marginLeft="15dp"            android:gravity="center_vertical"            android:orientation="vertical">            <TextView                android:id="@+id/tv_leftmenu_username"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:ellipsize="end"                android:singleLine="true"                android:text="Willian  Chan"                android:textColor="@color/leftmenu_namecolor"                android:textSize="17sp" />            <LinearLayout                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginLeft="5dp"                android:layout_marginTop="3dp"                android:orientation="horizontal">                <ImageView                    android:layout_width="18dp"                    android:layout_height="18dp"                    android:background="@mipmap/l_jifen"                    android:scaleType="centerCrop" />                <TextView                    android:id="@+id/tv_leftmenu_userjifen"                    android:layout_width="match_parent"                    android:layout_height="match_parent"                    android:layout_marginLeft="5dp"                    android:singleLine="true"                    android:text="4000"                    android:textColor="@color/leftmenu_jifencolor"                    android:textSize="15sp" />            </LinearLayout>        </LinearLayout>    </LinearLayout>    <ListView        android:listSelector="@android:color/white"        android:id="@+id/lv_leftmenu_list"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_below="@id/ll_pic"        android:layout_marginTop="15dp" />    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:background="#fff"        android:paddingBottom="25dp"        android:paddingTop="25dp">        <ImageView            android:id="@+id/iv_settings"            android:layout_width="25dp"            android:layout_height="25dp"            android:layout_centerHorizontal="true"            android:src="@mipmap/l_setting" />        <TextView            android:id="@+id/menuitem_menustr"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_below="@id/iv_settings"            android:layout_centerHorizontal="true"            android:layout_centerVertical="true"            android:layout_marginLeft="5dp"            android:layout_marginTop="3dp"            android:text="@string/Settings"            android:textColor="#333"            android:textSize="16sp" />    </RelativeLayout></RelativeLayout>
3.2,然后我们的Activity记得继承SlidingFragmentActivity

代码如下:

package com.messcat.qijisuoping;import android.os.Bundle;import android.util.DisplayMetrics;import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;import com.messcat.qijisuoping.fragment.leftmenu.L_LeftMenu;public class MainActivity extends SlidingFragmentActivity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        assignViews();    }    private void assignViews() {        initSlidingMenu();    }    //初始化左滑动菜单    private void initSlidingMenu() {        DisplayMetrics dm = new DisplayMetrics();        getWindowManager().getDefaultDisplay().getMetrics(dm);        int mScreenWidth = dm.widthPixels;// 获取屏幕分辨率宽度        SlidingMenu mSlidingMenu = getSlidingMenu();        if (mSlidingMenu==null){            //防止空指针            return;        }        L_LeftMenu leftMenu = new L_LeftMenu();        setBehindContentView(R.layout.l_leftmenumainlayout);        getSupportFragmentManager().beginTransaction()                .replace(R.id.fl_leftmenureplace, leftMenu).commit();        mSlidingMenu.setMode(SlidingMenu.LEFT);// 设置是左滑还是右滑,还是左右都可以滑,我这里左右都可以滑        mSlidingMenu.setShadowWidth(mScreenWidth / 120);// 设置阴影宽度        mSlidingMenu.setBehindOffset(mScreenWidth / 6);// 设置菜单宽度        mSlidingMenu.setFadeDegree(0.35f);// 设置淡入淡出的比例        mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);        mSlidingMenu.setShadowDrawable(R.drawable.l_shadow);// 设置左菜单阴影图片        mSlidingMenu.setFadeEnabled(true);// 设置滑动时菜单的是否淡入淡出        mSlidingMenu.setBehindScrollScale(0.333f);// 设置滑动时拖拽效果    }}
相关的设置已经写出来了,注释也有,对了 阴影的文件代码如下:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" >    <gradient        android:endColor="#73000000"        android:centerColor="#33000000"        android:startColor="#00000000" /></shape>
好像就没什么了,今天基本就搞这个了。 特别注意下,下午弄全局的时候一直出现一个空指针的问题,然而好奇怪,不知道出在哪里了。

附下单例Application的代码:

package com.messcat.qijisuoping.application;import android.app.Application;/** * Created by LHT on 2016/4/27. */public class MyApplication extends Application {    private static MyApplication myApplication;    @Override    public void onCreate() {        super.onCreate();        // 此处不可以用myapplication=new MyApplication();        // 一用的话,在对应的操作会报空指针异常。        // 而改用myapplication=this;的话  就没问题了,如果知道为什么会这样,请联系我  137544897  我也会去查答案         myApplication = this;    }        public static MyApplication getInstance() {        return myApplication;    }}


0 0