0

I have two fragments ListNav and SwipeNav in Navigation Drawer. ListNav shows list view of data from String array. SwipeNav shows data as swipe views of the same data from string array. I am able to replace fragment from ListNav to SwipeNav. String array data shows 'A to Z'.

My problem is when I click on list in ListNav (Suppose B)then SwipeNav(replaced Fragment) shows swipe views from start(means A). Suppose, if I click on 'D' in ListNav through Fragment replace, SwipeNav will show 'D'. Plz suggest me how I will implement this.

String Array:

<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>

I ListNav I use below to replace to SwipeNav with OnItemClickListener:

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

SwipeNav shows Data as Swipe Views through PagerAdapter from String Array:

tabTitlesArray = context.getResources().getStringArray(R.array.tab_titles);
achal naskar
  • 710
  • 1
  • 6
  • 19
  • can you explain more clearly. – Sandeep Insan Mar 26 '19 at 06:05
  • How about pass data using Bundle? – iroiroys Mar 26 '19 at 06:05
  • @iroiroys I am not passing data. I am only replacing fragments. both ListNav and SwipeNav shows data from String array. – achal naskar Mar 26 '19 at 06:08
  • You mean at this time you just replace them, right? When the first(ListNav) indicates 'B', then the second(SwipeNav) shows 'B'? – iroiroys Mar 26 '19 at 06:15
  • @iroiroys ListNav shows as list all items from A to Z. If I click on B the SwipeNav will show as a swipe views B. same as If I click on F then it will show F. SwipeNav shows A, B, C,D as swipe views per page. Same item from String array to next fragment. – achal naskar Mar 26 '19 at 06:21
  • As answered below, you need to pass the specific index to SwipeNav. In your code, the SwipeNav will always be created when click. I agree with @Talha Bilal – iroiroys Mar 26 '19 at 06:29

1 Answers1

1

I thing you are missing one step there and that is you need to send data in String form when swipe fragment is replacing .
Like.

Bundle bundle = new Bundle();
bundle.putString("TAG", "D");
// 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();

now on your swipe fragment make sure you are receiving that string that you are passing when creating .
so in onCreateViewmethod .

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String passingString = getArguments().getString("TAG");    
    return inflater.inflate(R.layout.fragment, container, false);
}

Here TAG just identifying what you are passing .

Talha Bilal
  • 167
  • 7
  • When I am using the code, shows error: Caused by: java.lang.NullPointerException at ....SwipeNav.onCreateView(SwipeNav.java:23) Line is : String passingString = getArguments().getString("TAG"); – achal naskar Mar 26 '19 at 06:43
  • Will I have to connect passingString to the layout in SwipeNav? – achal naskar Mar 26 '19 at 06:53
  • Thanks. Now no error issues. But String not passing. When I click F it shows A or when click D it also shows A. Same problem as before – achal naskar Mar 26 '19 at 07:03
  • thanks for your support but i think there is something missing to understand your question please explain clearly . so we can further try to resolve your issue . – Talha Bilal Mar 26 '19 at 09:17
  • I think you should see this link https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android . – Talha Bilal Mar 26 '19 at 09:19
  • Thank for the link. I have checked your link.I am using String array data to show both fragments. Will you kindly clarify 'TAG' and 'A' in the line: bundle.putString("TAG", "D"); – achal naskar Mar 26 '19 at 09:45
  • Actually **Bundle** is use to passing data from one activity or fragment to second . so in this line ` bundle.putString("TAG", "D"); ` bundle is accepting to store string with the value **D** and with key word **TAG** associate with it . – Talha Bilal Mar 26 '19 at 09:56
  • Is it possible to check which string is passing when fragment Manager replace? – achal naskar Mar 27 '19 at 08:26