【发布时间】:2018-06-16 12:53:38
【问题描述】:
让我直奔主题。我已经多次搜索有关 RECYCLE VIEW 和 CARD VIEW 的信息。我已经尝试了很多技巧,但我仍然无法找出哪个是最好的和最新的。
我感觉这个firebase-ui(FirebaseRecyclerAdapter)是最容易在回收视图和卡片视图中实现的。
“实现'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>
请帮我为 Fragment 制作 recycleView 和 cardView 并应用到 firebase。
【问题讨论】:
标签: android firebase firebase-realtime-database firebaseui