首頁 > 軟體

Android實現Tab切換介面功能詳解

2022-05-26 22:02:04

一、實驗目的

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切換介面的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com