我在导航抽屉中有两个片段ListNav和SwipeNav。 ListNav显示来自String数组的数据的列表视图。 SwipeNav将数据显示为字符串数组中相同数据的滑动视图。

我正在为Viewpager滑动视图的多个选项卡使用单个片段。

我的问题是:

When I click on B in List View, it shows A in SwipeNav,
When I click on C in List View, it shows A in SwipeNav,
When I click on D in List View, it shows A in SwipeNav,
When I click on E in List View, it shows A in SwipeNav,


我想得到结果:

If I click on B in List View, it will show B in SwipeNav,
If I click on C in List View, it will show C in SwipeNav,
If I click on D in List View, it will show D in SwipeNav


字符串数组:

<string-array name="tab_titles">
    <item>A</item>
    <item>B</item>
    <item>C</item>
    <item>D</item>
    <item>E</item>
    <item>F</item>
    <item>G</item>
    <item>H</item>
    <item>I</item>
    ........
</string-array>


我在下面使用ListNav用OnItemClickListener替换为SwipeNav:

ListNav:

public class ListNav extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //returning our layout file
        //change R.layout.yourlayoutfilename for each of your fragments
        return inflater.inflate(R.layout.nav_lis_layout, container, false);

    }

 @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        FragmentManager fm=getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragment12, new ListNavAdapter());
        ft.commit();


ListNavAdapter:

public class ListNavAdapter extends ListFragment {

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.nav_list_layout, container, false);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(),
            R.array.tab_titles, android.R.layout.simple_list_item_1);
    setListAdapter(adapter);
    getListView().setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            String[] tabTitlesArray = getContext().getResources().getStringArray(R.array.tab_titles);
            Bundle bundle = new Bundle();
            bundle.putString("tab_titles", tabTitlesArray[i].toString());

            SwipeNav fragobj = new SwipeNav();
            fragobj.setArguments(bundle);

            FragmentManager fm=getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(((ViewGroup)(getView().getParent())).getId(), fragobj);
            ft.addToBackStack(null);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.commit();
        }
    });
}


在SwipeNav中:

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    if (getArguments() != null && !getArguments().isEmpty()) {
        Bundle bundle = this.getArguments();
        String myInt = bundle.getString("tab_titles");
    }
        return inflater.inflate(R.layout.nav_swipe, container, false);

}


@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    myPagerAdapter = new MyPagerAdapter(getChildFragmentManager(),getContext());
    viewPager = (ViewPager) view.findViewById(R.id.pager);

    viewPager.setAdapter(myPagerAdapter);

}


这是MyPagerAdapter:

public class MyPagerAdapter extends FragmentStatePagerAdapter {
private Context context;


private String[] tabTitlesArray = null;
public MyPagerAdapter(FragmentManager fm, Context context) {
    super(fm);

    tabTitlesArray = context.getResources().getStringArray(R.array.tab_titles);
    this.context= context;
}

@Override
public Fragment getItem(int i) {
    Fragment fragment = new AFragment();
    Bundle args = new Bundle();

    args.putString(AFragment.ARG_OBJECT, tabTitlesArray[i]);
    fragment.setArguments(args);
    return fragment;
}

@Override
public int getCount() {
    return tabTitlesArray.length;
}

@Override
public CharSequence getPageTitle(int position) {


    return "OBJECT " + (position + 1);
}
}


选项卡式活动AFragment的单个片段:

public class AFragment extends Fragment {
public static final String ARG_OBJECT = "object";

public AFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // The last two arguments ensure LayoutParams are inflated
    // properly.


    View rootView = inflater.inflate(R.layout.fragment_a, container, false);
    Bundle args = getArguments();
    ((TextView) rootView.findViewById(R.id.text1)).setText(args.getString(ARG_OBJECT));
    return rootView;


}


nav_lis_layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment12"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


nav_list_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>


我在Stackoverflow中搜索了所有答案。但是无法解决我的问题,为什么特定的项目单击不显示与下一个片段相同的项目。
我需要你的帮助。

最佳答案

在SwipeNav中,设置ViewPager之后,您没有使用viewPager.setCurrentItem()更改为所需的页面。由于setCurrentItem()需要一个整数参数,因此列表中的单击位置需要从ListNavAdapter传递到SwipeNav。

ListNavAdapter.java:

public class ListNavAdapter extends ListFragment {

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.nav_list_layout, container, false);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(),
            R.array.tab_titles, android.R.layout.simple_list_item_1);
    setListAdapter(adapter);
    getListView().setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            //Toast.makeText(getActivity(), "Item: " + (i +1), Toast.LENGTH_SHORT).show();
            String[] tabTitlesArray = getContext().getResources().getStringArray(R.array.tab_titles);
            Bundle bundle = new Bundle();
            bundle.putString("tab_titles", tabTitlesArray[i].toString());

            bundle.putInt("tab_int", i);    //Added

            // set Fragmentclass Arguments
            SwipeNav fragobj = new SwipeNav();
            fragobj.setArguments(bundle);

            FragmentManager fm=getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(((ViewGroup)(getView().getParent())).getId(), fragobj);
            ft.addToBackStack(null);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.commit();

            //Fragment f=getFragmentManager().findFragmentById(R.id.fragment12);
           // if(f==null){
               // getFragmentManager().beginTransaction().replace(((ViewGroup)(getView().getParent())).getId(), new SwipeNav()).commit();
           // }

        }
    });
}





  // Toast.makeText(getActivity(), "Item: " + (position +1), Toast.LENGTH_SHORT).show();
    // Create new fragment and transaction



}


SwipeNav.java:

public class SwipeNav extends Fragment {

private MyPagerAdapter myPagerAdapter;
private ViewPager viewPager;

private int tab_int;    //Added


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    //returning our layout file
    //change R.layout.yourlayoutfilename for each of your fragments
    //String passingString = getArguments().getString("TAG");
    if (getArguments() != null && !getArguments().isEmpty()) {
        Bundle bundle = this.getArguments();
        String myInt = bundle.getString("tab_titles");

        tab_int = bundle.getInt("tab_int"); //Added

        return inflater.inflate(R.layout.nav_swipe, container, false);
    } else {
        return inflater.inflate(R.layout.nav_swipe, container, false);
    }
}


@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    myPagerAdapter = new MyPagerAdapter(getChildFragmentManager(),getContext());
    viewPager = (ViewPager) view.findViewById(R.id.pager);
    //viewPager.setId( R.id.pager );
    viewPager.setAdapter(myPagerAdapter);

    viewPager.setCurrentItem(tab_int);  //Added

    //you can set the title for your toolbar here for different fragments different titles
    getActivity().setTitle("Satyanusaran Bengali");
}

}


希望有帮助!

09-06 06:52