如何在每次将容器活动中的包传递到片段

时间:2018-06-11 10:10:38

标签: android android-activity fragment spinner tabactivity

这是我的MediaTab.java活动,它有片段TabImage

public class MediaTab extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener,
    TabImage.OnFragmentInteractionListener,
    TabPdf.OnFragmentInteractionListener {
Toolbar toolbar;
HashMap<String, String> groupName;
private ArrayList<String> groupNameArray;
Spinner group;
UserService userService;
SharedPreferences.Editor sharedPref;
String groupIdString;
private String TAG = MediaTab.class.getSimpleName();
ViewPager viewPager;
TabImage obj;

@SuppressLint("CommitPrefEdits")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_media_tab);
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    sharedPref = getSharedPreferences(getPackageName(), Context.MODE_PRIVATE).edit();
    groupNameArray = new ArrayList<>();
    userService = ApiUtils.getUserService();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle("Media");
    group = findViewById(R.id.groupspinner);
    groupName = new HashMap<>();

    viewPager = findViewById(R.id.viewpager);

    TabLayout tabLayout = findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    getCategories();

    group.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            groupIdString = groupName.get(groupNameArray.get(position));
            Log.d(TAG, "onItemClick:valueID " + groupIdString);
            Bundle bundle = new Bundle();
            bundle.putString("params", groupIdString);
            obj = new TabImage();
            obj.setArguments(bundle);
            setupViewPager(viewPager);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

    adapter.addFragment(obj, "MEDIA");
    adapter.addFragment(new TabPdf(), "FILE");
    viewPager.setAdapter(adapter);
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    return false;
}

@Override
public void onFragmentInteraction(Uri uri) {

}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

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

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

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

private void getCategories() {
    Call<CreatePostList> userServicePost = userService.getschema(App.getToken());
    userServicePost.enqueue(new Callback<CreatePostList>() {
        @Override
        public void onResponse(Call<CreatePostList> call, Response<CreatePostList> response) {
            if (response.isSuccessful()) {
                CreatePostList createPostList = response.body();
                List<Group> groups;
                groups = createPostList.getGroups();

                for (int j = 0; j < groups.size(); j++) {
                    groupName.put(groups.get(j).getName(), groups.get(j).getId());
                }

                Set<String> strings1 = groupName.keySet();
                ArrayList<String> arrayList1 = new ArrayList<>();
                arrayList1.addAll(strings1);

                groupNameArray = arrayList1;

                SpinnerMediaAdapter customAdapter2 = new SpinnerMediaAdapter(getApplicationContext(), R.layout.spinnertems_layout, R.id.spinnertems_text, groupNameArray);
                group.setAdapter(customAdapter2);
                //  if(groupNameArray.size() > 0) {
                //   groupIdString = groupName.get(groupNameArray.get(0));
                //     Log.d(TAG, "onResponse: .... setting at load " + groupIdString);
                //     Bundle bundle = new Bundle();
                //     bundle.putString("params", groupIdString);
                //     TabImage myObj = new TabImage();
                //     myObj.setArguments(bundle);
                //       }
                //  setupViewPager(viewPager);
            }
        }

        @Override
        public void onFailure(Call<CreatePostList> call, Throwable t) {
            Toast.makeText(MediaTab.this, "Something went wrong", Toast.LENGTH_SHORT).show();
        }
    });
}

这是我的片段&#34; TabImage&#34;在哪里我得到捆绑。

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_tab_image, container, false);
    if (getArguments() != null) {
        myDataFromActivity = getArguments().getString("params");
        Log.d(TAG, "onCreate: tab activity " + myDataFromActivity);

    }
    mRecyclerView = view.findViewById(R.id.tabimgrv);
    mRecyclerView.setFitsSystemWindows(true);
    mRecyclerView.setHasFixedSize(true);
    progressBar = view.findViewById(R.id.myprog);
    userService = ApiUtils.getUserService();
    activity = (MediaTab) getActivity();
    GridLayoutManager manager = new GridLayoutManager(this.getActivity(), 2, GridLayoutManager.VERTICAL, false);
    mRecyclerView.setLayoutManager(manager);
    getPostList(currentPage);

    mRecyclerView.addOnScrollListener(new PaginationMediaScrolling(manager) {
        @Override
        protected void loadMoreItems() {
            Log.d("TabImage :", "loadMoreItems() called");
            if (!isLastPage()) {
                {
                    isLoading = true;
                    currentPage = currentPage + 1;
                    loadNextpage(currentPage);

                }
            }
        }

        @Override
        public int getTotalPageCount() {
            return TOTAL_PAGES;
        }

        @Override
        public boolean isLastPage() {
            return isLastPage;
        }

        @Override
        public boolean isLoading() {
            return isLoading;
        }
    });
    return view;
}
private void getPostList(int currentPage) {
  //  progressBar.setVisibility(View.VISIBLE);
    Call<GetImage> userServicePost = userService.getMediaImage(App.getToken(), myDataFromActivity, currentPage);
    userServicePost.enqueue(new Callback<GetImage>() {
        @Override
        public void onResponse(Call<GetImage> call, Response<GetImage> response) {
           // progressBar.setVisibility(View.GONE);
            if (response.isSuccessful()) {
                GetImage getImagemodel = response.body();
                TOTAL_PAGES = totalPages(response);
                Media media = getImagemodel.getMedia();
                List<ResultMedia> resultMediaList = media.getResults();
                mAdapter = new TabImageAdapter(getContext(), resultMediaList);
                mRecyclerView.setAdapter(mAdapter);
                if (currentPage <= TOTAL_PAGES) mAdapter.addLoadingFooter();
                else isLastPage = true;
                isLoading = false;
            } else {
                Toast.makeText(context, "Could not complete the request! Please try again.", Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFailure(Call<GetImage> call, Throwable t) {
        }
    });
}

如何更改&#34; myDataFromActivity&#34;每当我从微调器中选择值。以及如何通过bundle传递此更改的值 因为groupid是动态值,每次都会改变

0 个答案:

没有答案