让我直截了当。我已经多次搜索过RECYCLE VIEW和CARD VIEW。我已经尝试了很多提示,但我仍然无法找出哪一个是最好的和最新的。
我觉得这个firebase-ui(FirebaseRecyclerAdapter)是最容易在循环视图和卡片视图中实现的。
“implementation'com.firebaseui:firebase-ui-database:4.0.1'”
我发现的是
的错误@Override //It says 'Method does not override from its superclass.
protected void populateViewHolder(HomeViewHolder homeViewHolder, home model, int position){
homeViewHolder.setTitle(model.getTitle());
homeViewHolder.setShortDescription(model.getShortDescription());
}
我认为这个版本不适用于4.0.1的最新更新
我把代码放在Fragment中。
FragmentHome.java
package my.package.sports;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
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.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
/**
* A simple {@link Fragment} subclass.
*/
public class FragmentHome extends Fragment {
private FirebaseRecyclerAdapter firebaseRecyclerAdapterHome;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mainView = inflater.inflate(R.layout.fragment_home, container, false);
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("home");
databaseReference.keepSynced(true);
RecyclerView recyclerView = mainView.findViewById(R.id.recycleView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//FirebaseRecyclerAdapter
firebaseRecyclerAdapterHome = new FirebaseRecyclerAdapter<home, HomeViewHolder>(home.class, R.layout.home_row, HomeViewHolder.class, databaseReference) {
@NonNull
@Override
public HomeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;
}
@Override
protected void onBindViewHolder(HomeViewHolder holder, int position, home model) {
}
@Override //It says 'Method does not override from its superclass.(Only Here the Error)
protected void populateViewHolder(HomeViewHolder homeViewHolder, home model, int position) {
homeViewHolder.setTitle(model.getTitle());
homeViewHolder.setShortDescription(model.getShortDescription());
}
};
recyclerView.setAdapter(firebaseRecyclerAdapterHome);
return mainView;
}
public static class HomeViewHolder extends RecyclerView.ViewHolder {
View viewData;
public HomeViewHolder(View viewItem) {
super(viewItem);
viewData = viewItem;
}
public void setTitle(String title) {
TextView textViewTitle = viewData.findViewById(R.id.home_row_title);
textViewTitle.setText(title);
}
public void setShortDescription(String shortDescription) {
TextView textViewShortDescription = viewData.findViewById(R.id.home_row_short_description);
textViewShortDescription.setText(shortDescription);
}
}
@Override
public void onStart() {
super.onStart();
firebaseRecyclerAdapterHome.startListening();
}
@Override
public void onStop() {
super.onStop();
firebaseRecyclerAdapterHome.stopListening();
}
}
home.class
package my.package.sports;
public class home {
private String title;
private String shortDescription;
home() {
}
public home(String title, String shortDescription) {
this.title = title;
this.shortDescription = shortDescription;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
}
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".FragmentHome">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
home_row.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:elevation="90dp"
android:orientation="vertical"
app:cardCornerRadius="11dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="245dp"
android:background="@color/colorPrimaryDark"
android:orientation="vertical">
<TextView
android:id="@+id/home_row_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:text="Title"
android:textColor="@color/colorText"
android:textSize="20sp" />
<TextView
android:id="@+id/home_row_short_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:text="Short Description" />
</LinearLayout>
</android.support.v7.widget.CardView>
请帮我制作recycleView和cardView for Fragment并申请firebase。
答案 0 :(得分:0)
您应该使用THIS指南详细说明如何使用
您应该使用此结构以使用Firebase-UI填充RecyclerView
FirebaseRecyclerAdapter
将Query
绑定到RecyclerView
。添加,删除或更改数据时,这些更新会自动实时应用于您的UI。
首先,通过构建FirebaseRecyclerOptions
来配置适配器。
FirebaseRecyclerOptions<home> options =
new FirebaseRecyclerOptions.Builder<home>()
.setQuery(query, home.class)
.build();
接下来创建FirebaseRecyclerAdapter
对象。您应该已经有一个ViewHolder
子类来显示每个项目
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<home, HomeViewHolder>(options) {
@Override
public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.message, parent, false);
return new HomeViewHolder(view);
}
@Override
protected void onBindViewHolder(HomeViewHolder holder, int position, home model) {
// Bind the Chat object to the ChatHolder
// ...
}
};
并且不要忘记这个
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
答案 1 :(得分:0)
我已经通过降级firebaseui解决了我的问题。
实施&#39; com.firebaseui:firebase-ui-database:0.4.0&#39;
我使用populateViewHolder,因为它支持0.4.0
FirebaseRecyclerAdapter<home, HomeViewHolder> firebaseRecyclerAdapterHome =
new FirebaseRecyclerAdapter<home, HomeViewHolder>(
home.class,
R.layout.home_row,
HomeViewHolder.class,
databaseReference
) {
@Override
protected void populateViewHolder(HomeViewHolder viewHolder, home model, int position) {
viewHolder.setDetails(getContext(), model.getImage(),model.getTitle(), model.getDate(), model.getShortDescription());
}
};
//setAdapter
recyclerView.setAdapter(firebaseRecyclerAdapterHome);