无法在片段上实施RecyclerView

时间:2018-05-24 18:14:24

标签: android android-recyclerview

我正在尝试在片段上使用FirebaseRecyclerAdapter显示一个recycleview。此片段在选项卡式活动上膨胀。我无法做到这一点。

我的片段:FragOne 我在片段上添加了一个id为mRecyclerView的recyclelerView 我在另一个xml menu_row

上创建了一个cardview

我创建了一个模型类MenuItems 我创建了一个viewholder MenuListHolder

请查找我写的代码:

的MenuItems:

public class MenuItems {
private String title;
private String type;
private String desc;
private String imageurl;

public MenuItems(){
//Empty constructor
}
public MenuItems(String title, String type, String desc, String imageurl) {
    this.title = title;
    this.type = type;
    this.desc = desc;
    this.imageurl = imageurl;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

public String getImageurl() {
    return imageurl;
}

public void setImageurl(String imageurl) {
    this.imageurl = imageurl;
}
}

MenuListHoder:

public class MenuListHolder extends RecyclerView.ViewHolder {
private TextView title;
private ImageView itemImage;
private TextView type;
private TextView desc;



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

    title = (TextView)itemView.findViewById(R.id.title);
    itemImage = (ImageView)itemView.findViewById(R.id.itemImage);
    type = (TextView) itemView.findViewById(R.id.type);
    desc = (TextView)itemView.findViewById(R.id.desc);
}

public void setTitle(String ti) {
    title.setText(ti);
}

public void setItemImage(String ii, Context context) {
    StorageReference ref = FirebaseStorage.getInstance().getReference().child(ii);
    Glide.with(context).load(ref).into(itemImage);
}

public void setType(TextView type) {
    this.type = type;
}

public void setDesc(TextView desc) {
    this.desc = desc;
}
}

FragOne:

public class FragOne extends Fragment {
private RecyclerView mRecyclerView;
FirebaseRecyclerAdapter adapter;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.frag_one, container, false);

    mRecyclerView = (RecyclerView) view.findViewById(R.id.mRecyclerView);
    mRecyclerView.setHasFixedSize(true);


    mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));


    Query query = FirebaseDatabase.getInstance()
            .getReference()
            .child("Menu")
            .limitToLast(50);

    FirebaseRecyclerOptions<MenuItems> options =
            new FirebaseRecyclerOptions.Builder<MenuItems>()
                    .setQuery(query, MenuItems.class)
                    .build();

    adapter = new FirebaseRecyclerAdapter<MenuItems,MenuListHolder>(options){

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

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


            return new MenuListHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull MenuListHolder holder, int position, @NonNull MenuItems model) {

        }
    };

    mRecyclerView.setAdapter(adapter);
    adapter.startListening();

    return view;
    //return inflater.inflate(R.layout.frag_one, container, false);

}



}

请帮忙......

1 个答案:

答案 0 :(得分:0)

最后我能够破解代码。在这里,您可以看到最终的工作代码:

的MenuItems:

package in.urchef.urchef_p;

public class MenuItems {

private String title;
private String type;
private String desc;
private String imageurl;

public MenuItems(){
//Empty constructor
}
public MenuItems(String title, String type, String desc, String imageurl) {
    this.title = title;
    this.type = type;
    this.desc = desc;
    this.imageurl = imageurl;
}

public String getTitle(){
    return title;
}

public String getType(){
    return type;
}

public String getDesc(){
    return desc;
}

public String getImageurl(){
    return imageurl;
}
}

MenuListHolder:

package in.urchef.urchef_p;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.squareup.picasso.Picasso;

public class MenuListHolder extends RecyclerView.ViewHolder {

private TextView title;
private ImageView itemImage;
private TextView type;
private TextView desc;



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

    title = (TextView)itemView.findViewById(R.id.title);
    itemImage = (ImageView)itemView.findViewById(R.id.itemImage);
    type = (TextView) itemView.findViewById(R.id.type);
    desc = (TextView)itemView.findViewById(R.id.desc);
}

public void setTitle(String ti) {
    title.setText(ti);
}

public void setItemImage(String ii, Context context) {
    StorageReference ref = FirebaseStorage.getInstance().getReference().child(ii);
    Picasso.with(context).load(ii).fit().centerCrop().into(itemImage);
    //Glide.with(context).load(ref).into(itemImage);
}

public void setType(String ty) {
    type.setText(ty);
}

public void setDesc(String de) {
    desc.setText(de);
}
}

FragOne:

package in.urchef.urchef_p;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
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 com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import static com.facebook.FacebookSdk.getApplicationContext;

public class FragOne extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.frag_one, container, false);

    RecyclerView mRecyclerView = (RecyclerView) 
   view.findViewById(R.id.mRecyclerView);
    mRecyclerView.setLayoutManager(new 
LinearLayoutManager(getApplicationContext()));
    DatabaseReference mDatabase = 
FirebaseDatabase.getInstance().getReference().child("Menu");
    mDatabase.keepSynced(true);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    Query query = FirebaseDatabase.getInstance()
            .getReference()
            .child("Menu").orderByKey();

    FirebaseRecyclerOptions<MenuItems> options =
            new FirebaseRecyclerOptions.Builder<MenuItems>()
                    .setQuery(query, MenuItems.class)
                    .build();

    FirebaseRecyclerAdapter adapter = new 
FirebaseRecyclerAdapter<MenuItems,MenuListHolder>(options){
        @NonNull
        @Override
        public MenuListHolder onCreateViewHolder(@NonNull ViewGroup parent, 
int viewType) {
            View view =LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.menu_row,parent,false);


            return new MenuListHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull MenuListHolder holder, int 
position, @NonNull MenuItems model) {
            holder.setTitle(model.getTitle());
            holder.setType(model.getType());
            holder.setDesc(model.getDesc());

holder.setItemImage(model.getImageurl(),getApplicationContext());

        }
    };

    mRecyclerView.setAdapter(adapter);
    adapter.startListening();

    return view;

}

}