近期更新下tablayout扩展-------联动
一般用到的基本是TabLayoutd+Veiwpager+Fragment 联动
效果如下:
cc4160bc54631c458a08d4d5c04823c6
主要实现步骤
1. 创建view pager适配器
2. 给view pager控件设置设配器
3. 然后tablelayout设置联动
xml
```
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/rg" />
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:padding="8dp">
android:id="@+id/rb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="首页" />
android:id="@+id/rb2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="购物车" />
android:id="@+id/rb3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="我的" />
```
activity
```
public class MainActivity2extends AppCompatActivity {
private RadioGroupm Rg;
private RadioButtonm Rb1;
private A mA;
private B mB;
private C mC;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
}
private void initView() {
mRg = (RadioGroup) findViewById(R.id.rg);
mRb1 = (RadioButton) findViewById(R.id.rb1);
mRb1.setChecked(true);
if (mA ==null) {
mA =new A();
getSupportFragmentManager().beginTransaction().add(R.id.framelayout, mA).show(mA).commit();
}
mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
switch (checkedId) {
case R.id.rb1:
if (mA ==null) {
mA =new A();
transaction.add(R.id.framelayout, mA);
}
hideFragment(transaction);
transaction.show(mA);
break;
case R.id.rb2:
if (mB ==null) {
mB =new B();
transaction.add(R.id.framelayout, mB);
}
hideFragment(transaction);
transaction.show(mB);
break;
case R.id.rb3:
if (mC ==null) {
mC =new C();
transaction.add(R.id.framelayout, mC);
}
hideFragment(transaction);
transaction.show(mC);
break;
}
transaction.commit();
}
});
}
private void hideFragment(FragmentTransaction transaction) {
if (mA !=null) {
transaction.hide(mA);
}
if (mB !=null) {
transaction.hide(mB);
}
if (mC !=null) {
transaction.hide(mC);
}
}
}
```
viewpageradapter
```
/**
* @auther eerdunsang
* @date 2020/10/27
* @time 0:49.
*/
public class Vadapterextends FragmentPagerAdapter {
private ArrayListf;
private ArrayLists;
public Vadapter(@NonNull FragmentManager fm) {
super(fm);
}
public Vadapter(@NonNull FragmentManager fm, ArrayList f, ArrayList s) {
super(fm);
this.f = f;
this.s = s;
}
@NonNull
@Override
public FragmentgetItem(int position) {
return f.get(position);
}
@Override
public int getCount() {
return f.size();
}
@Nullable
@Override
public CharSequencegetPageTitle(int position) {
return s.get(position);
}
}
```
网友评论