Detailed explanation of Android's implementation of Tab switching interface functions
1. The purpose of the experiment
1. Master the basic use of various advanced UI controls;
2. Able to achieve Tab switching effect.
2. Experimental tasks
1. Design the interface according to the prototype diagram;
2. Implement Tab switching;
3. Experiment content and requirements
3.1 Interface Design:
(1) Use linear layout to realize the basic layout of the interface;
(2) Use different Tab implementations to implement tab layout.
3.2 Tab switching
(1) Monitor Tab change events;
(2) Switch the corresponding page content;
Fourth, to achieve the effect
UI
hide interface
remove interface
Five, code implementation
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);
}}
Main interface
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();
}
});
}}
6. Experiment Summary
Through this experiment, I learned the operations of adding, deleting, modifying and checking the tab switching interface. The proficiency of these skills can lay the foundation for my further study. Secondly, I learned that in addition to fragments, there are also methods such as view and fragment+view to design tab switching. They each have their own characteristics. One is click switching, the other is sliding switching, and the combination is a composite function. In this study, there are still some parts that need to be strengthened.
0 Comments