简体   繁体   中英

Recycleview doesn't appear in a Fragment

I'm trying to get some data from my firebase, so I created recycleview in activity to get the data, and it worked without problem.

But now, I added my recycle to a Fragment to get data, and it managed to get the data but I can't see it, the firenase ( on success ) tell me that it work, but I can't see the recycleview.

I searched more and more without finding any solution, there are another one have the same problem, and he didn't find any solution else, here .

RecyclerView doesn't appear in Fragment

I tried all the solution in this question but, it didn't work, now I spent more than two months without finding out the problem.

FriendsFragment

public class FriendsFragment extends Fragment {  

DatabaseReference databaseReference;
    RecyclerView recyclerView;
    RecyclerView.Adapter adapter ;
    ProgressDialog progressDialog;
    List<ImageUploadInfo> list = new ArrayList<>();
    private Context context;


    public FriendsFragment() {  

    }  

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

        View view = inflater.inflate(com.wanjy.dannie.rivchat.R.layout.fragment_people, container, false);
        context = view.getContext();


        recyclerView = (RecyclerView) view.findViewById(R.id.recycleListFriend);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));

        progressDialog = new ProgressDialog(context);
        progressDialog.setMessage("Loading Images From Firebase.");
        progressDialog.show();

        databaseReference = FirebaseDatabase.getInstance().getReference("All_Image_Uploads_Database");

        databaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    for (DataSnapshot postSnapshot : snapshot.getChildren()) {

                        ImageUploadInfo imageUploadInfo = postSnapshot.getValue(ImageUploadInfo.class);
                        list.add(imageUploadInfo);

                    }

                    adapter = new RecyclerViewAdapter(context, list);
                    recyclerView.setAdapter(adapter);

                    progressDialog.dismiss();

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                    // Hiding the progress dialog.
                    progressDialog.dismiss();

                }
            });



        return inflater.inflate(R.layout.fragment_people, container, false);  

    }
}  

RecyclerViewAdapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    Context context;
    List<ImageUploadInfo> MainImageUploadInfoList;

    public RecyclerViewAdapter(Context context, List<ImageUploadInfo> TempList) {

        this.MainImageUploadInfoList = TempList;

        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ImageUploadInfo UploadInfo = MainImageUploadInfoList.get(position);

        holder.imageNameTextView.setText(UploadInfo.getImageName());

        //Loading image from Glide library.
        Glide.with(context).load(UploadInfo.getImageURL()).into(holder.imageView);
    }

    @Override
    public int getItemCount() {

        return MainImageUploadInfoList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView imageView;
        public TextView imageNameTextView;

        public ViewHolder(View itemView) {
            super(itemView);

            imageView = (ImageView) itemView.findViewById(R.id.imageView);

            imageNameTextView = (TextView) itemView.findViewById(R.id.ImageNameTextView);
        }
    }
}

ImageUploadInfo

public class ImageUploadInfo {

    public String imageName;

    public String imageURL;

    public ImageUploadInfo() {

    }

    public ImageUploadInfo(String name, String url) {

        this.imageName = name;
        this.imageURL= url;
    }

    public String getImageName() {
        return imageName;
    }

    public String getImageURL() {
        return imageURL;
    }

} 

recyclerview_items

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardview1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardElevation="5dp"
    card_view:contentPadding="5dp"
    card_view:cardCornerRadius="5dp"
    card_view:cardMaxElevation="5dp">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:orientation="vertical">

        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:background="#FFFFFF"
            android:padding="1dp">

            <ImageView
                android:layout_height="50dp"
                android:layout_width="50dp"
                android:src="@android:drawable/ic_delete"
                android:scaleType="fitXY"/>

            <LinearLayout
                android:layout_height="match_parent"
                android:layout_width="wrap_content"
                android:orientation="vertical"
                android:background="#FFFFFF"
                android:layout_weight="1.0"
                android:padding="3dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="15dp"
                    android:textColor="#000000"
                    android:text="أخبار الدولة"
                    android:gravity="center"
                    android:id="@+id/myname"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="التاريخ"
                    android:id="@+id/date"
                    android:textColor="#000000"/>

            </LinearLayout>

        </LinearLayout>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="fill_parent"
            android:layout_height="180dp"
            android:src="@mipmap/ic_launcher"
            android:background="#FFFFFF"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:layout_weight="1.0"
            android:textSize="14sp"
            android:textColor="#000000"
            android:layout_gravity="right"
            android:padding="4dp"
            android:gravity="right"
            android:id="@+id/ImageNameTextView"/>

        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:background="#FFFFFF"
            android:padding="2dp">

            <LinearLayout
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:orientation="horizontal"
                android:background="#FFFFFF"
                android:layout_weight="1.0"
                android:gravity="center">

                <ImageView
                    android:layout_height="30dp"
                    android:layout_width="30dp"
                    android:src="@android:drawable/ic_delete"
                    android:scaleType="fitXY"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="0"
                    android:textColor="#000000"
                    android:padding="2dp"/>

            </LinearLayout>

            <LinearLayout
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:orientation="horizontal"
                android:background="#FFFFFF"
                android:layout_weight="1.0"
                android:gravity="center">

                <ImageView
                    android:layout_height="30dp"
                    android:layout_width="30dp"
                    android:src="@android:drawable/ic_delete"
                    android:scaleType="fitXY"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="0"
                    android:textColor="#000000"
                    android:padding="2dp"/>

            </LinearLayout>

            <LinearLayout
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:orientation="horizontal"
                android:background="#FFFFFF"
                android:layout_weight="1.0"
                android:gravity="center">

                <ImageView
                    android:layout_height="30dp"
                    android:layout_width="30dp"
                    android:src="@android:drawable/ic_delete"
                    android:scaleType="fitXY"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="0"
                    android:textColor="#000000"
                    android:padding="2dp"/>

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</android.support.v7.widget.CardView>

fragment_people

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycleListFriend"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="15dp" />
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

My build gradle

compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    compile 'com.google.firebase:firebase-core:9.8.0'
    compile 'com.google.firebase:firebase-database:9.8.0'
    compile 'com.google.firebase:firebase-auth:9.8.0'
    testCompile 'junit:junit:4.12'
    compile 'de.hdodenhof:circleimageview:2.1.0'
    compile 'com.yarolegovich:lovely-dialog:1.0.4'
    compile 'com.android.support:cardview-v7:25.0.1'
    compile 'com.android.support:percent:25.0.1'
    compile 'com.github.bumptech.glide:glide:3.7.0'
  • Note My recycle work in Activity without problem, but it doesn't appear in my fragment .

I hope someone find the problem in my code.

Can you make any view show in the fragment? I noticed your MyFragment class is initializing fragment_people.xml but your xml class is named Fragment.xml unless that is a typo. Verify you can show any type of view in your fragment before thinking it is a recycler view issue.

Also, in your adapter, you are using an item layout with name recyclerview_items.xml but you have the class named Recycle item unless that is a typo as well.

It work now, I just changed the fragment to this

public class FriendsFragment extends Fragment {  

DatabaseReference databaseReference;
    RecyclerView recyclerView;
    RecyclerView.Adapter adapter ;
    ProgressDialog progressDialog;
    List<ImageUploadInfo> list = new ArrayList<>();
    private Context context;


    public FriendsFragment() {  

    }  

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

        View view = inflater.inflate(com.wanjy.dannie.rivchat.R.layout.fragment_people, container, false);
        context = view.getContext();










        return inflater.inflate(R.layout.fragment_people, container, false);  

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {

        recyclerView = (RecyclerView) view.findViewById(R.id.recycleListFriend);



        progressDialog = new ProgressDialog(context);
        progressDialog.setMessage("Loading Images From Firebase.");
        progressDialog.show();

        databaseReference = FirebaseDatabase.getInstance().getReference("All_Image_Uploads_Database");

        databaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    for (DataSnapshot postSnapshot : snapshot.getChildren()) {

                        ImageUploadInfo imageUploadInfo = postSnapshot.getValue(ImageUploadInfo.class);
                        list.add(imageUploadInfo);

                    }

                    adapter = new RecyclerViewAdapter(context, list);
                    recyclerView.setAdapter(adapter);
                    recyclerView.setHasFixedSize(true);
                    recyclerView.setLayoutManager(new LinearLayoutManager(context));


                    progressDialog.dismiss();

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                    // Hiding the progress dialog.
                    progressDialog.dismiss();

                }
            });




        super.onViewCreated(view, savedInstanceState);
    }




}  

I don't what was the problem but, I goggled for an example, and I found someone use method oncreateview So I did like him , and I moved this line

recyclerView.setLayoutManager(new LinearLayoutManager(context));

to under this line

recyclerView.setAdapter(adapter);

Now it work without problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM