如何在片段中实现RecyclerView?

时间:2016-12-12 09:40:37

标签: android android-recyclerview

我正在尝试在我的片段tabbedView中实现recylerView。我的应用程序有一个功能,我需要上传照片并将其发布在recyclerView上。但是,当我尝试上传图片时,应用程序崩溃了。我正在使用firebase-ui。它在android jellybean中运行良好。但是,当我试图在棒棒糖上运行应用程序时,应用程序崩溃了。我安装了必要的SDK平台。这是第一个标签式视图活动的代码:

import android.content.Context;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;

import static android.R.id.list;


public class AllPosts extends Fragment{





private RecyclerView postList;
private DatabaseReference mDatabase;
private LinearLayoutManager layoutManager;




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

    super.onSaveInstanceState(savedInstanceState);
    View rootView = inflater.inflate(R.layout.allposts, container, false);

    mDatabase= FirebaseDatabase.getInstance().getReference().child("Posts");



    //EXPERIMENT WITH LISTVIEW STARTS
    postList=(RecyclerView) rootView.findViewById(R.id.postList);
    postList.setHasFixedSize(true);
    layoutManager=new LinearLayoutManager(getActivity());
    postList.setLayoutManager(layoutManager);



    //EXPERIMENT WITH LISTVIEW ENDS
    return rootView;
}

@Override
public void onStart() {
    super.onStart();

    FirebaseRecyclerAdapter<Posts,PostViewHolder>    firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Posts, PostViewHolder>(

            Posts.class,
            R.layout.single_post,
            PostViewHolder.class,
            mDatabase




    ) {

        @Override
        protected void populateViewHolder(PostViewHolder viewHolder, Posts model, int position) {
            viewHolder.setTitle(model.getTitle());
            viewHolder.setDescription(model.getDescription());
            viewHolder.setImage(getContext(),model.getImage());


        }




    };


    postList.setAdapter(firebaseRecyclerAdapter);

}


public static class PostViewHolder extends RecyclerView.ViewHolder{

    View mView;

    public PostViewHolder(View itemView) {
        super(itemView);
        mView=itemView;

    }

    //Setting the title to the AllActivity
    public void setTitle(String title){
        TextView postTitle=(TextView) mView.findViewById(R.id.allPostTitle);
        postTitle.setText(title);
    }

    public void setDescription(String description){
        TextView postDescription=(TextView) mView.findViewById(R.id.allPostDescription);
        postDescription.setText(description);
    }



    public void setImage(Context context,String image) {
        ImageView postImage=(ImageView) mView.findViewById(R.id.allPostImage);
        Picasso.with(context).load(image).into(postImage);
    }
}

}

//继承我的XML代码:

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/postList"
    ></android.support.v7.widget.RecyclerView>

//单个列表项视图的代码

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:id="@+id/allPostImage"
        android:src="@color/cardview_dark_background"
        android:scaleType="centerCrop" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/allPostTitle"
        android:padding="10dp"
        android:text="post Title"
        android:textStyle="normal|bold"
        android:textAllCaps="true"
        android:textSize="24sp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/allPostDescription"
        android:padding="10dp"
        android:text="post Description"
        android:textSize="16sp"
        android:textAllCaps="true"
        android:textAlignment="viewStart" />

</LinearLayout>

//我在棒棒糖(api level 21)上运行它时显示的错误

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 0(offset:3).state:3
                                                                     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5202)
                                                                     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5158)
                                                                     at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2061)
                                                                     at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1445)
                                                                     at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1408)
                                                                     at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:580)
                                                                     at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3330)
                                                                     at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3186)
                                                                     at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3632)
                                                                     at android.view.View.layout(View.java:15685)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                                                                     at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1692)
                                                                     at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
                                                                     at android.view.View.layout(View.java:15685)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                     at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1799)
                                                                     at android.view.View.layout(View.java:15685)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                     at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)
                                                                     at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                                                                     at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1364)
                                                                     at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:846)
                                                                     at android.view.View.layout(View.java:15685)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                     at android.view.View.layout(View.java:15685)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                                                                     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                                                                     at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                                                                     at android.view.View.layout(View.java:15685)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                     at android.view.View.layout(View.java:15685)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                                                                     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                                                                     at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                                                                     at android.view.View.layout(View.java:15685)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                     at android.view.View.layout(View.java:15685)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2116)
                                                                     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1873)
                                                                     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1091)
                                                                     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5926)
                                                                     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:775)
                                                                     at android.view.Choreographer.doCallbacks(Choreographer.java:588)
                                                                     at android.view.Choreographer.doFrame(Choreographer.java:558)
                                                                     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:761)
                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)

0 个答案:

没有答案