6.17 3
package com.zhen.accountbook; import android.os.Bundle; import android.view.View; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.viewpager.widget.ViewPager; import com.google.android.material.tabs.TabLayout; import com.zhen.accountbook.adapter.RecordPagerAdapter; import com.zhen.accountbook.frag_record.InFragment; import com.zhen.accountbook.frag_record.OutFragment; import java.util.ArrayList; import java.util.List; public class RecordActivity extends AppCompatActivity { TabLayout tabLayout; ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_record); // 1、查找控件 tabLayout = findViewById(R.id.record_tabs); viewPager = findViewById(R.id.record_vp); // 2、设置ViewPaper加载页面 initPager(); } private void initPager() { // 初始化(fragment) ViewPaper的集合 List<Fragment> fragmentList = new ArrayList<>(); // 创建收入和支出页面,放在fragment当中 OutFragment outFragment = new OutFragment(); InFragment inFragment = new InFragment(); fragmentList.add(outFragment); fragmentList.add(inFragment); //创建适配器 RecordPagerAdapter recordPagerAdapter = new RecordPagerAdapter(getSupportFragmentManager(), fragmentList); //设置适配器对象 viewPager.setAdapter(recordPagerAdapter); //将TabLayout和ViewPaper进行关联 tabLayout.setupWithViewPager(viewPager); } public void onClick(View view) { if (view.getId() == R.id.record_iv_back) { finish(); } } }