1. <dd id="erndk"></dd>
                1. Android強化——項目基本準備

                  互聯網 2022/4/29 6:12:59

                  項目基本準備:包:model,ui(activity,fragment,adapter),base,utils,persenter,view主題:theme中設置NOACTIONBAR去除標題color.xml:設置顏色Material新組件:BottomNavigationViewFragment:設置基礎BaseFragment簡化操作。通過getSurpootFragmentManger()獲取fr…

                  項目基本準備:

                    包: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 

                    獲?。?/p>

                      通過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);
                          }
                      }
                  }

                   

                  隨時隨地學軟件編程-關注百度小程序和微信小程序
                  關于找一找教程網

                  本站文章僅代表作者觀點,不代表本站立場,所有文章非營利性免費分享。
                  本站提供了軟件編程、網站開發技術、服務器運維、人工智能等等IT技術文章,希望廣大程序員努力學習,讓我們用科技改變世界。
                  [Android強化——項目基本準備]http://www.yachtsalesaustralia.com/tech/detail-317901.html

                  贊(0)
                  關注微信小程序
                  程序員編程王-隨時隨地學編程

                  掃描二維碼或查找【程序員編程王】

                  可以隨時隨地學編程啦!

                  技術文章導航 更多>
                  掃一掃關注最新編程教程
                  国产在线拍揄自揄视频菠萝

                        1. <dd id="erndk"></dd>