动态设置时查看寻呼机未加载

时间:2016-04-02 00:56:58

标签: android android-fragments tabs android-viewpager bottom-sheet

我在一个活动上打开了一个带有viewpager的底部工作表,其中包含详细信息以及评论和活动流。但是,单击活动中片段上的列表项时,将打开底部工作表。通过传递id的网络呼叫单击列表项时,将单独调用注释和活动流。

当我传递静态ID并在片段填充数据后立即设置底部工作表视图时,一切顺利。

在尝试动态传递id时遇到了挑战。我在点击listitem时尝试设置viewpager和tab布局。这是怎么......

    mViewPager = (ViewPager)getActivity().findViewById(R.id.viewpager_story);
    mSectionsPagerAdapter = new ActivityCommentAdapter(getFragmentManager(),storyId);

    TabLayout tabLayout = (TabLayout)getActivity().findViewById(R.id.tabs);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    tabLayout.setupWithViewPager(mViewPager);

然而,这永远不会在UI中加载视图寻呼机,因此不会显示任何列表。 除了No-View之外,我在调试时注意到一个奇怪的事情,这肯定会进入网络并获取数据,但设置列表的适配器不会被正确调用。只调用构造函数而不是onBindViewHolder()...

我已经做了几天的搜索,请帮忙......

public class ActivityCommentAdapter extends FragmentStatePagerAdapter {

private  String storyId;
public ActivityCommentAdapter(FragmentManager fm, String storyId) {
    super(fm);
    this.storyId=storyId;

}

@Override
public Fragment getItem(int position) {
     return PlaceholderFragment.newInstance(position, storyId);
}

@Override
public int getItemPosition(Object object) {
    return super.getItemPosition(object);
}

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

@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
        case 0:
            return "Comments";
        case 1:
            return "Activity";
    }
    return null;
}

public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";
    List<CommentListModal.Datum> comments = new ArrayList<>();
    RecyclerView rvList;
    CommentsAdapter mAdapter;
    SharedPreferences mPrefs;
    String token;
    Retrofit retrofit;
    ListApis requestApi;
    String storyId;

    public PlaceholderFragment() {

    }

    public void getCommentsForStory(String storyId) {

        Call<CommentListModal> call = requestApi.getCommentsForStory(token, "525", "story", storyId);
        call.enqueue(new Callback<CommentListModal>() {
            @Override
            public void onResponse(Call<CommentListModal> call, Response<CommentListModal> response) {

                if (response.body().getStatus() == 100) {
                    mAdapter = new CommentsAdapter(response.body().getData(), "1", "1");
                    rvList.setAdapter(mAdapter);
                } else if (response.body().getStatus() == 101) {
                    System.out.println("Error--->" + response.body().getError());
                    //Snackbar.make(fabButton, response.body().getError().get(0), Snackbar.LENGTH_SHORT).show();
                } else if (response.body().getStatus() == 102) {
                    System.out.println("Error--->" + response.body().getError());
                    //Snackbar.make(fabButton, response.body().getError().get(0), Snackbar.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<CommentListModal> call, Throwable t) {

                System.out.println("Failure Response---->" + call);
            }
        });

    }

    public static PlaceholderFragment newInstance(int sectionNumber, String storyId) {
        System.out.println("Place holder instance: " + storyId);
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        args.putString("sid", storyId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_comment, container, false);
        Button btn = (Button) rootView.findViewById(R.id.button2);

        retrofit = new Retrofit.Builder()
                .baseUrl(getString(R.string.BASE_URL))
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        requestApi = retrofit.create(ListApis.class);

        token = getString(R.string.token);

        mPrefs = getActivity().getSharedPreferences(getString(R.string.pref_filename),
                Context.MODE_PRIVATE);

        rvList = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        rvList.setHasFixedSize(true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        rvList.setLayoutManager(layoutManager);
        rvList.setAdapter(null);

        getCommentsForStory(storyId);
        /*mAdapter = new CommentsAdapter(comments, "1", "1");
        rvList.setAdapter(mAdapter);*/
        return rootView;
    }
}

}

0 个答案:

没有答案