1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| package com.gallifrey.tablayouttest;
import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.TextView;
import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayoutMediator;
import java.util.ArrayList; import java.util.List;
public class MainActivity extends AppCompatActivity { private ViewPager2 viewPager2; private TabLayout tabLayout; private List<IconRelation> iconRelationList; private List<Fragment> fragmentList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager2=findViewById(R.id.view_pager2); tabLayout=findViewById(R.id.tab_layout); initIcon(); initFragment();
viewPager2.setAdapter(new ImageStateAdapter(this,fragmentList)); new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() { @Override public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) { tab.setCustomView(getTabView(iconRelationList,position)); } }).attach();
tabLayout.getTabAt(1).select();
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { View view=tab.getCustomView(); ImageView imageView=view.findViewById(R.id.tab_iv); TextView textView=view.findViewById(R.id.tab_tv); imageView.setSelected(true); textView.setTextAppearance(R.style.TabSelectedStyle); }
@Override public void onTabUnselected(TabLayout.Tab tab) { View view=tab.getCustomView(); ImageView imageView=view.findViewById(R.id.tab_iv); TextView textView=view.findViewById(R.id.tab_tv); imageView.setSelected(false); textView.setTextAppearance(R.style.TabUnSelectedStyle); }
@Override public void onTabReselected(TabLayout.Tab tab) {
} }); }
private View getTabView(List<IconRelation> data, int position) { View view= LayoutInflater.from(this).inflate(R.layout.tab_item,null); ImageView imageView=view.findViewById(R.id.tab_iv); TextView textView=view.findViewById(R.id.tab_tv); imageView.setImageResource(data.get(position).getImageId()); textView.setText(data.get(position).getName()); return view; }
private void initIcon() { iconRelationList=new ArrayList<>(); iconRelationList.add(new IconRelation(R.drawable.sunny,"晴天")); iconRelationList.add(new IconRelation(R.drawable.cloudyy,"多云")); iconRelationList.add(new IconRelation(R.drawable.rainy,"下雨"));
}
private void initFragment() { fragmentList=new ArrayList<>(); fragmentList.add(ImageFragment.newInstance(R.drawable.sunday)); fragmentList.add(ImageFragment.newInstance(R.drawable.cloudyday)); fragmentList.add(ImageFragment.newInstance(R.drawable.rainyday)); } }
|