片段中的RecyclerView - 没有附加适配器,跳过布局

时间:2016-12-14 03:36:58

标签: java android android-fragments android-recyclerview

我有一个奇怪的情况,我有一个Activity托管TabLayout/Viewpager,根据对Firebase数据库的更改来填充数据。不幸的是,RecyclerView没有填充,我在logcat中收到以下错误。我相信即使片段不在屏幕上也会加载片段,这会导致RecyclerView的填充错误。当我旋转屏幕时,RecyclerView会填充,这也很奇怪:

12-13 21:31:40.013 12490-12490/com.troychuinard.fanpolls E/RecyclerView: No adapter attached; skipping layout

以下是我在主持人ViewPager中的Activity适配器:

public class SectionPagerAdapter extends FragmentPagerAdapter {

    public SectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new TrendingFragment();
            case 1:
                return new FollowingFragment();
            case 2:
                return new LiveFragment();
            default:
                return new TrendingFragment();
        }
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return getResources().getString(R.string.trending_text);
            case 1:
                return getResources().getString(R.string.following_text);
            case 2:
                return getResources().getString(R.string.new_text);
            default:
                return getResources().getString(R.string.trending_text);
        }
    }
}

以下是我在RecyclerView中初始化onCreateView() LiveFragment内部TabLayout功能的完整片段,即public class LiveFragment extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; private RecyclerView mRecyclerview; private DatabaseReference mBaseRef; private DatabaseReference mPollsRef; private LinearLayoutManager mLayoutManager; private FirebaseRecyclerAdapter <Poll, PollHolder> mFireAdapter; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; private OnFragmentInteractionListener mListener; public LiveFragment() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment LiveFragment. */ // TODO: Rename and change types and number of parameters public static LiveFragment newInstance(String param1, String param2) { LiveFragment fragment = new LiveFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBaseRef = FirebaseDatabase.getInstance().getReference(); mPollsRef = mBaseRef.child("Polls"); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment final View v = inflater.inflate(R.layout.fragment_new, container, false); Log.v("TAG", "ON CREATE CALLED FROM NEW"); mLayoutManager = new LinearLayoutManager(getActivity()); mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); mRecyclerview = (RecyclerView)v.findViewById(R.id.new_RecyclerView); if (mRecyclerview != null){ mRecyclerview.setHasFixedSize(true); } if (mRecyclerview == null){ Log.v("TAG", "RECYCLERVIEW NULL"); } else if (mLayoutManager == null){ Log.v("TAG", "LAYOUTMANAGER NULL"); } else if (mFireAdapter == null) { Log.v("TAG", "mFIREADAPTER NULL"); } return v; } // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); } } @Override public void onStart() { super.onStart(); } @Override public void onStop() { super.onStop(); if (mFireAdapter != null){ mFireAdapter.cleanup(); } } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mRecyclerview.setLayoutManager(mLayoutManager); mFireAdapter = new FirebaseRecyclerAdapter<Poll, PollHolder>(Poll.class, R.layout.latest_item, PollHolder.class, mPollsRef) { @Override protected void populateViewHolder(PollHolder viewHolder, Poll model, int position) { viewHolder.mPollQuestion.setText(model.getQuestion()); Picasso.with(getActivity().getApplicationContext()) .load(model.getImage_URL()) .fit() .into(viewHolder.mPollImage); Log.v("QUESTION", model.getQuestion()); Log.v("IMAGE", model.getImage_URL()); } }; mRecyclerview.setAdapter(mFireAdapter); mPollsRef.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot x : dataSnapshot.getChildren()){ mFireAdapter.notifyItemInserted(0); } } @Override public void onCancelled(DatabaseError databaseError) { } }); } @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof OnFragmentInteractionListener) { mListener = (OnFragmentInteractionListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } public static class PollHolder extends RecyclerView.ViewHolder { TextView mPollQuestion; ImageView mPollImage; public PollHolder(View itemView) { super(itemView); mPollQuestion = (TextView) itemView.findViewById(R.id.latest_item_question); mPollImage = (ImageView) itemView.findViewById(R.id.pollThumbNailImage); } } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. * <p> * See the Android Training lesson <a href= * "http://developer.android.com/training/basics/fragments/communicating.html" * >Communicating with Other Fragments</a> for more information. */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); } } 中的标签3:

Braintree_Exception_SSLCertificate

1 个答案:

答案 0 :(得分:1)

从firebase读取数据后,将适配器从onCreateView移动到onActivityCreated方法。

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

//read Poll data from firebase mPollsRef for example
//.... database reference to firebase
//Query mPollsRef = databaseReference.child("pools").limitToFirst(200);


    mRecyclerview.setLayoutManager(mLayoutManager);
    mFireAdapter = new FirebaseRecyclerAdapter<Poll, PollHolder>(Poll.class, R.layout.latest_item, PollHolder.class, mPollsRef) {
    @Override
    protected void populateViewHolder(PollHolder viewHolder, Poll model, int position) {
        viewHolder.mPollQuestion.setText(model.getQuestion());
        Picasso.with(getActivity().getApplicationContext())
                .load(model.getImage_URL())
                .fit()
                .into(viewHolder.mPollImage);
        Log.v("QUESTION", model.getQuestion());
        Log.v("IMAGE", model.getImage_URL());
        }
    };
    mRecyclerview.setAdapter(mFireAdapter);

}

我在HostActivity.java初始化寻呼机的代码

private FragmentPagerAdapter mPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_absence);


    // Create the adapter that will return a fragment for each section
    mPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
        private final Fragment[] mFragments = new Fragment[] {
                new MyAbsenceFragment(),
                new MyAttendanceFragment(),
                new AbsTypesFragment(),
                new LiveFragment(),
        };
        private final String[] mFragmentNames = new String[] {
                "Absences",
                "Attendances",
                "AbsTypes",
                "Live"
        };
        @Override
        public Fragment getItem(int position) {
            return mFragments[position];
        }
        @Override
        public int getCount() {
            return mFragments.length;
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentNames[position];
        }
    };
    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mPagerAdapter);