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(); } }); } }
六、實驗總結
通過這次實驗,我學習瞭tab切換界面的增刪改查的操作,這些技能的熟練能夠為我的進一步學習打下瞭基礎。其次我瞭解到除瞭fragment,還有view、fragment+view等方法設計tab切換,它們各有特色,一者是點擊切換,一者是滑動切換,組合後是一種復合功能。在此次學習中,仍有部分內容需要加強。
以上就是Android實現Tab切換界面功能詳解的詳細內容,更多關於Android Tab切換界面的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- Android Fragment源碼分析Add方法
- Android實現音樂視頻播放
- Android 中使用RadioGroup和Fragment實現底部導航欄的功能
- Android Fragment的具體使用方式詳解
- Android應用實現點擊按鈕震動