简体   繁体   中英

Android Java Viewpager loads in the same viewPager view position

I'm new in Java/Android programming.

I've got a Fragment with tabLayout and viewPager. This fragment get's a list of groupId/groupName from the server then needs to create one page for each groupId. It sends the value to the adapter (the fragment to show in the viewpager pages is always ContentRoster() but the groupId define the data to show)

My issue is that this ContentRoster() fragment only loads in position 0 of the viewpager. I can swipe and slect the tabs as expected, but all loads in the same view...

If I have two sets of groupId, the first one will load in position 0, then the second one groupId#2 (which is autoloaded to populate next view) update position 0 view and erase groupID#1 view.

    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
        super.onPostExecute(result);

        TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tabLayout);
        final ViewPager viewPager = (ViewPager) getActivity().findViewById(R.id.viewPager);
        tabLayout.setupWithViewPager(viewPager);

        AdapterViewPager mCustomPagerAdapter = new AdapterViewPager(getChildFragmentManager());

        //add Fragment with specific arguments
        for(int i = 0; i < result.size() ; i++) {
            mCustomPagerAdapter.addFragment(new ContentRoster(), (result.get(i)).get("groupname"), (result.get(i)).get("groupid") );
        }

        //Set adapter if not null
        if (viewPager != null) {
            viewPager.setAdapter(mCustomPagerAdapter);
            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        }

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (viewPager != null) {
                    Log.i("#TAB_POS", "" + tab.getPosition());
                    viewPager.setCurrentItem(tab.getPosition());
                }
            }

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

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                if (viewPager != null) {
                    Log.i("#TAB_POS", "" + tab.getPosition());
                    viewPager.setCurrentItem(tab.getPosition());
                }
            }
        });

    }

The adapter looks like this :

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class AdapterViewPager extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();
    private final List<String> mFragmentParamsList = new ArrayList<>();

    public void addFragment(Fragment fragment, String title, String params) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
        mFragmentParamsList.add(params);
    }

    public AdapterViewPager(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        String groupId = mFragmentParamsList.get(position);
        Fragment fragment = ContentRoster.newInstance(groupId);
        return fragment;
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }

}

and the ContentRoster() Fragment is like this : I retrieve the groupId from the bundle and 'LoadRosterList().execute(groupId)' charge the data and show only the groupId data in line with the selected tab.

   public static ContentRoster newInstance(String groupId) {
        ContentRoster ContentRoster = new ContentRoster();

        Bundle args = new Bundle();
        args.putString("groupId", groupId);
        ContentRoster.setArguments(args);

        return ContentRoster;
    }

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

        View view = inflater.inflate(R.layout.content_listview, container, false);

        Bundle args = getArguments();
        groupId = args.getString("groupId");

        progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
        new LoadRosterList().execute(groupId);

        return view;
    }

UPDATE : Could this be link to the last part where I update the listview in the ContentRoster Fragment (which is shown in the viewPager) As I get the listview Id from the activity?

protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
    super.onPostExecute(result);

    try {
        // Getting adapter by passing xml data ArrayList
        ListView mListView;
        mListView = (ListView) getActivity().findViewById(R.id.listView);

        AdapterRoster adapter = new AdapterRoster(getActivity().getApplicationContext(), result);
        mListView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        progressBar.setVisibility(View.GONE); //Effacage du Progress Bar

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id){
                // Send intent to SingleViewActivity
                Intent i = new Intent(getActivity().getApplicationContext(), ActivitySingleGuardian.class);
                // Pass image index
                i.putExtra("psnid", ((TextView) v.findViewById(R.id.psnid)).getText().toString());
                i.putExtra("rank", ((TextView) v.findViewById(R.id.rank)).getText().toString());
                i.putExtra("lastplay", ((TextView) v.findViewById(R.id.lastplay)).getText().toString());
                i.putExtra("bungiename", ((TextView) v.findViewById(R.id.bungiename)).getText().toString());
                i.putExtra("bungieid", ((TextView) v.findViewById(R.id.bungieid)).getText().toString());
                i.putExtra("picpath", ((TextView) v.findViewById(R.id.picpath)).getText().toString());
                i.putExtra("membershipType", ((TextView) v.findViewById(R.id.membershipType)).getText().toString());
                startActivity(i);
            }
        });

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

Alright, you're close, but not yet there.

In your onPostExecute method, consider changing the last line from:

viewPager.setAdapter(mCustomPagerAdapter);

to something like:

if (viewPager != null) {
        viewPager.setAdapter(mCustomPagerAdapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        if (viewPager != null) {
            viewPager.setCurrentItem(tab.getPosition());
        }
    }

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

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
        if (viewPager != null) {
            viewPager.setCurrentItem(tab.getPosition());
        }
    }
});

this would result in removing this line as well (more like moving it and adding to it):

tabLayout.addOnTabSelectedListener(onTabSelectedListener(viewPager));

Hope this helps!

Finally found the issue ! This was on my last part when I call the adapter inside the ContentRoster() Fragment which is part of the viewPager !

I was using : mListView = (ListView) getActivity().findViewById(R.id.listView);

Instead of : mListView = (ListView) getView().findViewById(R.id.listView);

The list now show in the right place!

Thanks for all guys !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM