Android support library View Page: interaction with frame
Android support library View Page: interaction with frame
In Android development, View Pages is a common interface component that can be switched between multiple frameings.However, if we want to transmit data between different Fragment or implement certain interactive logic, we need to understand how to interact with Fragment in the View Page.This article will introduce how to use View Pages to implement interaction with Fragment in Android and provide corresponding Java code examples.
The use of View Pages to implement interaction with Fragment is mainly composed of the following steps:
1. Create a viewPager
First, we need to create a viewPager in the XML layout file.You can use the following code example:
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
2. Create Fragment
Next, we need to create Fragment associated with the View Page.Each Fragment represents a page and is responsible for displaying the corresponding content and processing interactive logic.The following is a simple Fragment example:
public class MyFragment extends Fragment {
public static MyFragment newInstance(String data) {
MyFragment fragment = new MyFragment();
Bundle args = new Bundle();
args.putString("data", data);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
// Get the data passed
String data = getArguments().getString("data");
// Update the interface according to the data update
TextView textView = view.findViewById(R.id.textView);
textView.setText(data);
return view;
}
}
3. Create a Fragment adapter
Then, we need to create a subclass of FragmentPageradapter or FragmentStatePageradapter to manage Fragment switching.The following is a simple adapter example:
public class MyPagerAdapter extends FragmentPagerAdapter {
private List<String> dataList;
public MyPagerAdapter(FragmentManager fragmentManager, List<String> dataList) {
super(fragmentManager);
this.dataList = dataList;
}
@Override
public Fragment getItem(int position) {
// Create the Fragment that passed the data
return MyFragment.newInstance(dataList.get(position));
}
@Override
public int getCount() {
return dataList.size();
}
}
4. Set ViewPager adapter
Finally, in the Activity or Fragment, we need to instantiated ViewPager and associate the adapter with it.The following is a simple example:
ViewPager viewPager = findViewById(R.id.viewPager);
List <string> datalist = Arrays.aslist ("Data 1", "Data 2", "Data 3");
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), dataList);
viewPager.setAdapter(adapter);
Through the above steps, we can use View Pages to achieve interaction with Fragment in Android.By passing different data to Fragment, we can implement data transmission and interactive operations between different pages.
I hope this article can help you understand how to use View Pages in Android to realize your interaction with Fragment.