Android實(shí)現(xiàn)Tab切換界面功能詳解
一、實(shí)驗(yàn)?zāi)康?/h2>
1. 掌握各種高級UI控件的基本使用;
2. 能夠?qū)崿F(xiàn)Tab切換效果。
二、實(shí)驗(yàn)任務(wù)
1. 根據(jù)原型圖設(shè)計(jì)界面;
2. 實(shí)現(xiàn)Tab切換;
三、實(shí)驗(yàn)內(nèi)容與要求
3.1 界面設(shè)計(jì):
(1)使用線性布局實(shí)現(xiàn)界面的基本布局;
(2)使用不同的Tab實(shí)現(xiàn)方式實(shí)現(xiàn)tab的布局。
3.2 Tab切換
(1)監(jiān)聽Tab變化事件;
(2)切換對應(yīng)的頁面內(nèi)容;
四、實(shí)現(xiàn)效果
顯示界面

隱藏界面

移除界面

五、代碼實(shí)現(xiàn)
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();
}
});
}
}六、實(shí)驗(yàn)總結(jié)
通過這次實(shí)驗(yàn),我學(xué)習(xí)了tab切換界面的增刪改查的操作,這些技能的熟練能夠?yàn)槲业倪M(jìn)一步學(xué)習(xí)打下了基礎(chǔ)。其次我了解到除了fragment,還有view、fragment+view等方法設(shè)計(jì)tab切換,它們各有特色,一者是點(diǎn)擊切換,一者是滑動切換,組合后是一種復(fù)合功能。在此次學(xué)習(xí)中,仍有部分內(nèi)容需要加強(qiáng)。
以上就是Android實(shí)現(xiàn)Tab切換界面功能詳解的詳細(xì)內(nèi)容,更多關(guān)于Android Tab切換界面的資料請關(guān)注腳本之家其它相關(guān)文章!
Android編程判斷網(wǎng)絡(luò)連接是否可用的方法
flutter BottomAppBar實(shí)現(xiàn)不規(guī)則底部導(dǎo)航欄

