0

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

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

挑战一直在尝试动态传递 id。单击列表项时,我正在尝试设置 viewpager 和选项卡布局。就是这样..

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

}

4

0 回答 0