Question of passing values from the main of the viewfager to the fragment.

Asked 1 years ago, Updated 1 years ago, 109 views

The main is currently using the View Pager tab layout toolbar. When you click Search here, you perform the SearchAsyncTask. Then, you want to get a long value from SearchAsyncTask's onPostExecute and send this long value to each tab (fragment) to do another AsyncTask in the fragment. However, I don't know how to send the long value to the fragment in the viewfager even if I look at the links

This is the main code.

searchBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String name = inputName.getText().toString();
        new SearchAsyncTask(name).execute ("URL");
    }
});

toolbar = (Toolbar) findViewById(R.id.searchToolbar);
setSupportActionBar(toolbar);
toolbar.setTitle ("search");

tabLayout = (TabLayout) findViewById(R.id.searchTabLayout);
tabLayout.addTab(tabLayout.newTab().setText("m")"));
tabLayout.addTab(tabLayout.newTab().setText("ㄴ")"));
tabLayout.addTab(tabLayout.newTab().setText("ㄹ")"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

viewPager = (ViewPager) findViewById(R.id.searchViewPager);
SearchViewPagerAdapter adapter = new SearchViewPagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.setOffscreenPageLimit(3);

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
});

This is the viewfager adapter in the main function. How should I send the long value? <

private class SearchViewPagerAdapter extends FragmentStatePagerAdapter {

    int tabCount;

    public SearchViewPagerAdapter(FragmentManager fm, int tabCount) {
        super(fm);
        this.tabCount = tabCount;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                return new STab1();
            case 1:
                return new STab2();
            case 2:
                return new STab3();
            default:
                return null;
        }
    }

    public Object instantiateItem(ViewGroup container, int position) {
        Fragment createdFragment = (Fragment) super.instantiateItem(container, position);

        switch (position) {
            case 0:
                sTab1 = (STab1) createdFragment;
                break;
            case 1:
                sTab2 = (STab2) createdFragment;
                break;
            case 2:
                sTab3 = (STab3) createdFragment;
                break;
        }
        return createdFragment;
    }

    @Override
    public int getCount() {
        return tabCount;
    }
}

android-viewpager tablayout json android fragment

2022-09-21 19:47

3 Answers

Try the function below. You can access the fragments that correspond to the index of the viewpager.

public Fragment findFragmentByPosition(int position) {
    FragmentPagerAdapter fragmentPagerAdapter = getFragmentPagerAdapter();
    return getSupportFragmentManager().findFragmentByTag(
            "android:switcher:" + getViewPager().getId() + ":"
                    + + fragmentPagerAdapter.getItemId(position));
}

A simple method is

If you use the findFragmentByPosition() function, the code will be written in the following shape.

STab1 fragment = (STab1) findFragmentByPosition(0);
fragment.setXXX(long);


2022-09-21 19:47

Current postExecute

     @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        prd.dismiss();
        try {
            JSONObject parentJsonObject = new JSONObject(result);

            long responseCode = parentJsonObject.getLong("responseCode");

            if(responseCode == 33) {
                long id = parentJsonObject.getLong("id");
                STab3 fragment = (STab3) findFragmentByPosition(2);
                fragment.setLong(id);
                //someMethod(id);
            } } else if(responseCode == 34) {
                Toast.makeText (SearchHwamyun.this, "Nickname does not exist!" and Toast.LENGTH_SHORT).show();
            }

        } } catch (JSONException e) {
            e.printStackTrace();
        }
    }

This is a function created by Maine.

public Fragment findFragmentByPosition(int position) {
    FragmentStatePagerAdapter asdf = adapter;
    return getSupportFragmentManager().findFragmentByTag(
            "android:switcher:" + viewPager.getId() + ":" +
                    asdf.getItem(position));
}

Fragment made a function like this

public long setLong(long asdf) {
    asd = asdf;
    return asd;
}

There is a nullPointerException error in postExecute


2022-09-21 19:47

If you do not need to inherit the FragmentStatePagerAdapter, modify the findFragmentByPosition() function below after changing it to inherit the FragmentPagerAdapter.

public Fragment findFragmentByPosition(int position) {
    return getSupportFragmentManager().findFragmentByTag(
            "android:switcher:" + viewPager.getId() + ":"
                    + + ((FragmentPagerAdapter) viewPager.getAdapter()).getItemId(position));
}

It's the same code as the first one I uploaded. I modified the getViewPager() and getFragmentPagerAdapter() functions to match the code you uploaded.


2022-09-21 19:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.