这篇文章主要介绍“Android如何实现Tab切换界面功能”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Android如何实现Tab切换界面功能”文章能帮助大家解决问题。
1. 掌握各种高级UI控件的基本使用;
2. 能够实现Tab切换效果。
1. 根据原型图设计界面;
2. 实现Tab切换;
3.1 界面设计:
(1)使用线性布局实现界面的基本布局;
(2)使用不同的Tab实现方式实现tab的布局。
3.2 Tab切换
(1)监听Tab变化事件;
(2)切换对应的页面内容;
显示界面
隐藏界面
移除界面
Fragment
package com.example.shiyan3.fragment; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.shiyan3.R; public class FirstFG extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; public FirstFG() { // Required empty public constructor } // TODO: Rename and change types and number of parameters public static FirstFG newInstance(String param1, String param2) { FirstFG fragment = new FirstFG(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_firstfg, container, false); } }
主界面
package com.example.shiyan3; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.example.shiyan3.fragment.FirstFG; public class MainActivity extends AppCompatActivity { FragmentManager fragmentManager; FragmentTransaction fragmentTransaction; FirstFG fragment; Button badd,bremove,bshow,bhide; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); badd = this.findViewById(R.id.addFragment); bremove =this.findViewById(R.id.removeFragment); bshow = this.findViewById(R.id.showFragment); bhide = this.findViewById(R.id.hideFragment); fragment = new FirstFG(); Bundle bundle = new Bundle(); bundle.putString("key","this is String Value"); fragment.setArguments(bundle); fragmentManager = this.getSupportFragmentManager(); fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.fgcontainer,fragment,"FirstFragment"); fragmentTransaction.commit(); badd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.fgcontainer,fragment,"FirstFragment"); fragmentTransaction.commit(); } }); bremove.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.remove(fragment); fragmentTransaction.commit(); } }); bshow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.show(fragment); fragmentTransaction.commit(); } }); bhide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.hide(fragment); fragmentTransaction.commit(); } }); } }
关于“Android如何实现Tab切换界面功能”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。