parent
							
								
									d05ec5d649
								
							
						
					
					
						commit
						d82be8bc75
					
				
				 15 changed files with 245 additions and 2 deletions
			
			
		| @ -1,19 +1,107 @@ | ||||
| package com.project.survey.activity | ||||
| 
 | ||||
| import androidx.fragment.app.Fragment | ||||
| import androidx.fragment.app.FragmentManager | ||||
| import androidx.fragment.app.FragmentPagerAdapter | ||||
| import androidx.viewpager.widget.ViewPager | ||||
| import com.bingce.ui.TabEntity | ||||
| import com.flyco.tablayout.listener.CustomTabEntity | ||||
| import com.flyco.tablayout.listener.OnTabSelectListener | ||||
| import com.project.survey.R | ||||
| import com.project.survey.activity.base.BaseBindingActivity | ||||
| import com.project.survey.databinding.ActivityMainBinding | ||||
| import com.project.survey.fragment.home.HomeFragment | ||||
| import com.project.survey.fragment.home.InstrumentFragment | ||||
| import com.project.survey.fragment.home.MeFragment | ||||
| 
 | ||||
| 
 | ||||
| class MainActivity : BaseBindingActivity<ActivityMainBinding>() { | ||||
| 
 | ||||
|     private val mFragments = mutableListOf<Fragment>() | ||||
| 
 | ||||
|     override fun getBinding(): ActivityMainBinding { | ||||
|         return ActivityMainBinding.inflate(layoutInflater) | ||||
|     } | ||||
| 
 | ||||
|     override fun initView() { | ||||
| 
 | ||||
|         initTabFragment() | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     private fun initTabFragment() { | ||||
|         //设置tab | ||||
|         val mTabEntities = ArrayList<CustomTabEntity>() | ||||
|         mTabEntities.add( | ||||
|             TabEntity( | ||||
|                 "首页", | ||||
|                 R.mipmap.icon_home_selected, | ||||
|                 R.mipmap.icon_home_selected_not | ||||
|             ) | ||||
|         ) | ||||
|         mTabEntities.add( | ||||
|             TabEntity( | ||||
|                 "仪器", | ||||
|                 R.mipmap.icon_instrument_selected, | ||||
|                 R.mipmap.icon_instrument_selected_not | ||||
|             ) | ||||
|         ) | ||||
|         mTabEntities.add( | ||||
|             TabEntity( | ||||
|                 "我的", | ||||
|                 R.mipmap.icon_me_selected, | ||||
|                 R.mipmap.icon_me_selected_not | ||||
|             ) | ||||
|         ) | ||||
|         mBinding.tabLayout.setTabData(mTabEntities) | ||||
| 
 | ||||
|         mBinding.tabLayout.setOnTabSelectListener(object : OnTabSelectListener { | ||||
|             override fun onTabSelect(position: Int) { | ||||
|                 mBinding.vp.setCurrentItem(position) | ||||
|             } | ||||
| 
 | ||||
|             override fun onTabReselect(position: Int) { | ||||
|             } | ||||
|         }) | ||||
| 
 | ||||
|         //设置viewPager | ||||
|         mFragments.add(HomeFragment()) | ||||
|         mFragments.add(InstrumentFragment()) | ||||
|         mFragments.add(MeFragment()) | ||||
|         mBinding.vp.adapter = MyPagerAdapter(supportFragmentManager) | ||||
|         mBinding.vp.offscreenPageLimit = 3 | ||||
|         mBinding.vp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener { | ||||
|             override fun onPageScrolled( | ||||
|                 position: Int, | ||||
|                 positionOffset: Float, | ||||
|                 positionOffsetPixels: Int | ||||
|             ) { | ||||
|             } | ||||
| 
 | ||||
|             override fun onPageSelected(position: Int) { | ||||
|                 mBinding.tabLayout.currentTab = position | ||||
|             } | ||||
| 
 | ||||
|             override fun onPageScrollStateChanged(state: Int) { | ||||
|             } | ||||
| 
 | ||||
|         }) | ||||
|     } | ||||
| 
 | ||||
|     override fun initData() { | ||||
|     } | ||||
| 
 | ||||
|     private inner class MyPagerAdapter : FragmentPagerAdapter { | ||||
|         constructor(fm: FragmentManager) : super(fm) | ||||
| 
 | ||||
|         constructor(fm: FragmentManager, behavior: Int) : super(fm, behavior) | ||||
| 
 | ||||
|         override fun getItem(position: Int): Fragment { | ||||
|             return mFragments[position] | ||||
|         } | ||||
| 
 | ||||
|         override fun getCount(): Int { | ||||
|             return mFragments.size | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,59 @@ | ||||
| package com.project.survey.fragment.base; | ||||
| 
 | ||||
| import android.os.Bundle; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
| 
 | ||||
| import androidx.annotation.NonNull; | ||||
| import androidx.annotation.Nullable; | ||||
| import androidx.fragment.app.Fragment; | ||||
| import androidx.viewbinding.ViewBinding; | ||||
| 
 | ||||
| 
 | ||||
| public abstract class BaseFragmentBinding<VB extends ViewBinding> extends Fragment { | ||||
|     protected VB mBinding; | ||||
| 
 | ||||
|     @Nullable | ||||
|     @Override | ||||
|     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | ||||
|         mBinding = getViewBinding(inflater, container); | ||||
|         return mBinding.getRoot(); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { | ||||
|         super.onViewCreated(view, savedInstanceState); | ||||
|         initView(); | ||||
|         initData(); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onDestroyView() { | ||||
|         super.onDestroyView(); | ||||
|         mBinding = null; | ||||
|     } | ||||
| 
 | ||||
|     protected abstract VB getViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container); | ||||
| 
 | ||||
|     protected abstract void initView(); | ||||
| 
 | ||||
|     protected abstract void initData(); | ||||
| 
 | ||||
|     protected String getArgumentString(String key) { | ||||
|         Bundle bundle = getArguments(); | ||||
|         if (bundle != null) { | ||||
|             return bundle.getString(key); | ||||
|         } | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|     protected int getArgumentInt(String key) { | ||||
|         Bundle bundle = getArguments(); | ||||
|         if (bundle != null) { | ||||
|             return bundle.getInt(key); | ||||
|         } | ||||
|         return -1; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,21 @@ | ||||
| package com.project.survey.fragment.home | ||||
| 
 | ||||
| import android.view.LayoutInflater | ||||
| import android.view.ViewGroup | ||||
| import com.project.survey.databinding.FragmentHomeBinding | ||||
| import com.project.survey.fragment.base.BaseFragmentBinding | ||||
| 
 | ||||
| class HomeFragment : BaseFragmentBinding<FragmentHomeBinding>() { | ||||
|     override fun getViewBinding( | ||||
|         inflater: LayoutInflater, | ||||
|         container: ViewGroup? | ||||
|     ): FragmentHomeBinding { | ||||
|         return FragmentHomeBinding.inflate(inflater, container, false) | ||||
|     } | ||||
| 
 | ||||
|     override fun initView() { | ||||
|     } | ||||
| 
 | ||||
|     override fun initData() { | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,21 @@ | ||||
| package com.project.survey.fragment.home | ||||
| 
 | ||||
| import android.view.LayoutInflater | ||||
| import android.view.ViewGroup | ||||
| import com.project.survey.databinding.FragmentInstrumentBinding | ||||
| import com.project.survey.fragment.base.BaseFragmentBinding | ||||
| 
 | ||||
| class InstrumentFragment : BaseFragmentBinding<FragmentInstrumentBinding>() { | ||||
|     override fun getViewBinding( | ||||
|         inflater: LayoutInflater, | ||||
|         container: ViewGroup? | ||||
|     ): FragmentInstrumentBinding { | ||||
|         return FragmentInstrumentBinding.inflate(inflater, container, false) | ||||
|     } | ||||
| 
 | ||||
|     override fun initView() { | ||||
|     } | ||||
| 
 | ||||
|     override fun initData() { | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,21 @@ | ||||
| package com.project.survey.fragment.home | ||||
| 
 | ||||
| import android.view.LayoutInflater | ||||
| import android.view.ViewGroup | ||||
| import com.project.survey.databinding.FragmentMeBinding | ||||
| import com.project.survey.fragment.base.BaseFragmentBinding | ||||
| 
 | ||||
| class MeFragment : BaseFragmentBinding<FragmentMeBinding>() { | ||||
|     override fun getViewBinding( | ||||
|         inflater: LayoutInflater, | ||||
|         container: ViewGroup? | ||||
|     ): FragmentMeBinding { | ||||
|         return FragmentMeBinding.inflate(inflater, container, false) | ||||
|     } | ||||
| 
 | ||||
|     override fun initView() { | ||||
|     } | ||||
| 
 | ||||
|     override fun initData() { | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,11 @@ | ||||
| <?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="vertical"> | ||||
| 
 | ||||
|     <TextView | ||||
|         android:layout_width="wrap_content" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:text="首页" /> | ||||
| </LinearLayout> | ||||
| @ -0,0 +1,11 @@ | ||||
| <?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="vertical"> | ||||
| 
 | ||||
|     <TextView | ||||
|         android:layout_width="wrap_content" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:text="仪器" /> | ||||
| </LinearLayout> | ||||
| @ -0,0 +1,11 @@ | ||||
| <?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="vertical"> | ||||
| 
 | ||||
|     <TextView | ||||
|         android:layout_width="wrap_content" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:text="我的" /> | ||||
| </LinearLayout> | ||||
| After Width: | Height: | Size: 906 B | 
| After Width: | Height: | Size: 983 B | 
| After Width: | Height: | Size: 954 B | 
| After Width: | Height: | Size: 963 B | 
| After Width: | Height: | Size: 933 B | 
| After Width: | Height: | Size: 958 B | 
					Loading…
					
					
				
		Reference in new issue