Android开发

Android强化——项目基本准备

本文主要是介绍Android强化——项目基本准备,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

项目基本准备:

  包:model,ui(activity,fragment,adapter),base,utils,persenter,view

  主题:theme中设置NOACTIONBAR去除标题

  color.xml:设置颜色

  Material新组件:BottomNavigationView

  Fragment:设置基础BaseFragment简化操作。通过getSurpootFragmentManger()获取fragment事务管理器

  LogUtils:方便操作,上线后不用输出log

 

BottomNavigationView:底部导航栏

  属性:

    app:menu 设置菜单(记住,menu里都是控件)

    app:itemIconTint:用selector设置点击后icon颜色

    app:itemTextColor: 用selector设置点击后text颜色

 

  方法:

    setOnNavigationItemSelectedListener(): 点击子项触发事件

 

FragmentTransaction 

  获取:

    通过getSupportFragmentManger().beigin()

  方法:

    replace(View container , Fragment fragment)

      参数:

        container:包裹fragment的容器,是FramLayout

        fragment:切换的fragment

      返回值:无

      返回值意义:无

      作用:切换到目标fragment,需要commit()才能切换。

 

  

使用:

  MENU

 <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/home"
        android:title="主页"
        android:icon="@mipmap/home_normal"/>

    <item
        android:id="@+id/selected"
        android:title="挑选"
        android:icon="@mipmap/select_normal"/>

    <item
        android:id="@+id/red_packet"
        android:title="特惠"
        android:icon="@mipmap/red_packet_normal"/>

    <item
        android:id="@+id/search"
        android:title="搜索"
        android:icon="@mipmap/search_normal"/>

</menu>

 

  BottomNavigationView

 <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/navigationView"
        android:layout_alignParentBottom="true"
        app:menu="@menu/navigation_menu"
        app:itemIconTint="@drawable/navigation_selector"
        app:itemTextColor="@drawable/navigation_selector"
        android:background="@color/white" />

 

  综合


public class MainActivity extends AppCompatActivity {
    protected BottomNavigationView bottomNavigationView;
    private HomeFragment homeFragment;
    private SelectedFragment selectedFragment;
    private RedPacketFragment redPacketFragment;
    private SearchFragment searchFragment;
    private FragmentManager supportFragmentManager;
    private FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // model ui(activity,adapter,fragment) base utils presenter view(自定义view)
        // BottomNavigationView

        /**
         *  属性:app:menu 给底部导航栏添加menu
         *      app:itemIconTint 底部导航栏icon设置颜色
         *      app:itemTextColor 底部导航栏text设置颜色
         *
         * */

        /**
         *  menu 中item当单控件来看
         *  item
         *     id id
         *     title 文字
         *     icon 图标
         * */
        initView();
        initData();
        initEvent();
        initFragment();
    }

    public void initView(){
        bottomNavigationView = this.findViewById(R.id.navigationView);
    }

    public void initData(){

    }

    public void initEvent(){
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch ( item.getItemId() ){
                    case R.id.home:
                        fragmentTransaction = supportFragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.main_container,homeFragment);
                        fragmentTransaction.commit();
                        break;
                    case R.id.selected:
                        fragmentTransaction = supportFragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.main_container,selectedFragment);
                        fragmentTransaction.commit();
                        break;
                    case R.id.red_packet:
                        fragmentTransaction = supportFragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.main_container,redPacketFragment);
                        fragmentTransaction.commit();
                        break;
                    case R.id.search:
                        fragmentTransaction = supportFragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.main_container,searchFragment);
                        fragmentTransaction.commit();
                        break;
                }

                return true;
            }
        });
    }

    public void initFragment(){
        homeFragment = new HomeFragment();
        selectedFragment = new SelectedFragment();
        redPacketFragment = new RedPacketFragment();
        searchFragment = new SearchFragment();
        supportFragmentManager = getSupportFragmentManager();
        fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.main_container,homeFragment);
        fragmentTransaction.commit();
    }

}

 

public abstract class BaseFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(getLayoutId(),container,false);
    }

    public abstract int getLayoutId();

}

 

public class HomeFragment extends BaseFragment {
    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }
}

 

public class LogUtils {
    private static int current = 5;
    private static final int INFO_LEV = 4;
    private static final int DEBUG_LEV = 3;
    private static final int WARING_LEV = 2;
    private static final int ERROR_LEV = 1;
    
    public static void i(Class clazz,String str){
        if( current > INFO_LEV ){
            Log.i(clazz.getSimpleName(), str);
        }
    }

    public static void d(Class clazz,String str){
        if( current > DEBUG_LEV ){
            Log.i(clazz.getSimpleName(), str);
        }
    }

    public static void w(Class clazz,String str){
        if( current > WARING_LEV ){
            Log.i(clazz.getSimpleName(), str);
        }
    }

    public static void e(Class clazz,String str){
        if( current > ERROR_LEV ){
            Log.i(clazz.getSimpleName(), str);
        }
    }
}

 

这篇关于Android强化——项目基本准备的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!