我有一个包含导航抽屉的活动,在该类中我的导航项指向片段类,在其中一个片段类中我使用动态视图寻呼机来显示项目,但我如何使用片段的片段管理器class作为视图寻呼机的参数,需要片段活动或活动的片段参数,需要一些解决方案。
答案 0 :(得分:3)
我现在正在做类似的事情。我只有导航抽屉下面的ViewPager,但是如果你有一个更复杂的布局,那么相同的主体应该可以工作。
首先,我有活动布局(大部分是由Android Studio生成的)
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.you.yourapp.YourActivity">
<FrameLayout android:id="@+id/your_container" android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.example.you.yourapp.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
活动使用FragmentManager将包含ViewPager的另一个片段交换到FrameLayout
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your);
...
Fragment fragment = YourFragment.newInstance(Bundle args);
getSupportFragmentManager()
.beginTransaction().replace(R.id.your_container, fragment)
.commit();
}
拥有ViewPager的Fragment很简单
<android.support.v4.view.ViewPager android:id="@+id/your_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
public class YourFragment extends android.support.v4.app.Fragment {
private ViewPager mPager;
public YourFragment() {
}
public static TaskFragment newInstance(Uri uri, String outlineText) {
TaskFragment fragment = new YourFragment();
Bundle args = new Bundle();
//set args
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_your, null);
mPager = (ViewPager) rootView.findViewById(R.id.your_view_pager);
TaskPagerAdapter adapter = new TaskPagerAdapter(getFragmentManager());
mPager.setAdapter(adapter);
return rootView;
}
...
private class YourPagerAdapter extends FragmentStatePagerAdapter {
public YourPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return count; //however you get the count
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
//set the arguments for your page
return PageFragment.newInstance(args);
}
}
}
ETA: 所以现在为导航抽屉回调实现自定义界面
...
private NavigationDrawerCallbacks mCallbacks
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mCallbacks = (NavigationDrawerCallbacks) activity;
}
...
@Override
public void onClick(View view) { //whatever your click listener is
Fragment fragment = getFragmentSomehow(); //get your fragment
mCallbacks.onNavigationItemSelected(fragment);
}
...
public static interface NavigationDrawerCallbacks {
void onNavigationDrawerItemSelected(Fragment fragment);
}
现在让YourActivity实现此接口
public class YourFragment extends Activity
implements NavigationDrawerCallbacks
{
...
@Override
public void onNavigationDrawerItemSelected(Fragment fragment) {
getSupportFragmentManeger()
.beginTransaction()
.replace(R.id.you_container, fragment)
.commit();
}
...
}
我认为这就是你的意思。 希望这有帮助