当我尝试绑定数据时,模拟器中只显示进度。但是在Logcat中,每件事都很好
我的基地FirebaseRecyclerAdapter
:
public abstract class FirestoreAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> implements EventListener<QuerySnapshot> {
private static final String TAG = "Firestore Adapter";
private Query mQuery;
private ListenerRegistration mRegistration;
protected ArrayList<DocumentSnapshot> mSnapshots = new ArrayList<>();
public FirestoreAdapter(Query query) {
mQuery = query;
}
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.w(TAG, "onEvent:error", e);
onError(e);
return;
}
// Dispatch the event
Log.d(TAG, "onEvent:numChanges:" + documentSnapshots.getDocumentChanges().size());
for (DocumentChange change : documentSnapshots.getDocumentChanges()) {
switch (change.getType()) {
case ADDED:
onDocumentAdded(change);
break;
case MODIFIED:
onDocumentModified(change);
break;
case REMOVED:
onDocumentRemoved(change);
break;
}
}
onDataChanged();
}
public void startListening() {
if (mQuery != null && mRegistration == null) {
mRegistration = mQuery.addSnapshotListener(this);
}
}
public void stopListening() {
if (mRegistration != null) {
mRegistration.remove();
mRegistration = null;
}
mSnapshots.clear();
notifyDataSetChanged();
}
public void setQuery(Query query) {
// Stop listening
stopListening();
// Clear existing data
mSnapshots.clear();
notifyDataSetChanged();
// Listen to new query
mQuery = query;
startListening();
}
@Override
public int getItemCount() {
return mSnapshots.size();
}
public DocumentSnapshot getSnapshot(int index) {
return mSnapshots.get(index);
}
protected void onDocumentAdded(DocumentChange change) {
mSnapshots.add(change.getNewIndex(), change.getDocument());
notifyItemInserted(change.getNewIndex());
}
protected void onDocumentModified(DocumentChange change) {
if (change.getOldIndex() == change.getNewIndex()) {
// Item changed but remained in same position
mSnapshots.set(change.getOldIndex(), change.getDocument());
notifyItemChanged(change.getOldIndex());
} else {
// Item changed and changed position
mSnapshots.remove(change.getOldIndex());
mSnapshots.add(change.getNewIndex(), change.getDocument());
notifyItemMoved(change.getOldIndex(), change.getNewIndex());
}
}
protected void onDocumentRemoved(DocumentChange change) {
mSnapshots.remove(change.getOldIndex());
notifyItemRemoved(change.getOldIndex());
}
protected void onError(FirebaseFirestoreException e) {
}
protected void onDataChanged() {
setQuery(mQuery);
}
}
用于查询的基本Firestore适配器:
CustomRecycler适配器:
public class Bidhaa_Recycler_Adapter extends FirestoreAdapter{
private static final int BIDHAA_VIEW_TYPE = 0;
private static final int NATIVE_EXPRESS_AD_VIEW_TYPE = 1;
public Bidhaa_Recycler_Adapter(Query query) {
super(query);
setQuery(query);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType){
case NATIVE_EXPRESS_AD_VIEW_TYPE:
View native_Ad_View = LayoutInflater.from(parent.getContext())
.inflate(R.layout.native_express_ad_container, parent, false);
return new Native_Ad_ViewHolder(native_Ad_View);
case BIDHAA_VIEW_TYPE:
default:
View bidhaa_View = LayoutInflater.from(parent.getContext())
.inflate(R.layout.bidhaa_layout, parent, false);
return new Bidhaa_Holder(bidhaa_View);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int itemViewType = getItemViewType(position);
switch (itemViewType){
case NATIVE_EXPRESS_AD_VIEW_TYPE:
Native_Ad_ViewHolder nativeExpressHolder = (Native_Ad_ViewHolder) holder;
NativeExpressAdView adView = (NativeExpressAdView) mSnapshots.get(position);
ViewGroup adCardView = (ViewGroup) nativeExpressHolder.itemView;
adCardView.removeAllViews();
if (adView.getParent() != null){
((ViewGroup) adView.getParent()).removeView(adView);
}
adCardView.addView(adView);
break;
case BIDHAA_VIEW_TYPE:
default:
Bidhaa_Holder bidhaaHolder = (Bidhaa_Holder) holder;
//Bidhaa_Data bidhaaData = (Bidhaa_Data) mSnapshots.get(position);
bidhaaHolder.BindData(getSnapshot(itemViewType));
//((Bidhaa_Holder) holder).BindData(getSnapshot(position));
}
}
@Override
public int getItemCount() {
return mSnapshots.size();
}
@Override
public int getItemViewType(int position) {
return (position % 2 == 0) ? NATIVE_EXPRESS_AD_VIEW_TYPE : BIDHAA_VIEW_TYPE;
}
@Override
public void onEvent(Object o, FirebaseFirestoreException e) {
}
}
First View Holder
Bidhaa
ViewHolder:
class Bidhaa_Holder extends RecyclerView.ViewHolder {
private ImageView picha;
private CircularProgressBar testing;
private TextView bidhaa, bei, muuzaji, namba;
public Context pichaContext;
Bidhaa_Holder(View bidhaa_view) {
super(bidhaa_view);
picha = bidhaa_view.findViewById(R.id.picha_bidhaa);
bidhaa = bidhaa_view.findViewById(R.id.maelezo_bidhaa);
bei = bidhaa_view.findViewById(R.id.bei_bidhaa);
muuzaji = bidhaa_view.findViewById(R.id.muuzaji_bidhaa);
namba = bidhaa_view.findViewById(R.id.simu_bidhaa);
testing= bidhaa_view.findViewById(R.id.tester);
}
public void BindData(DocumentSnapshot docSnapshot) {
Bidhaa_Data bidhaaData = docSnapshot.toObject(Bidhaa_Data.class);
pichaContext = itemView.getContext();
namba.setText(bidhaaData.getSimu());
muuzaji.setText(bidhaaData.getMuuzaji());
bidhaa.setText(bidhaaData.getBidhaa());
bei.setText(bidhaaData.getBei());
testing.progressiveStop();
String Url = bidhaaData.getPicha();
Picasso.with(pichaContext)
.load(Url)
.centerInside()
.fit()
.into(picha, new Callback() {
@Override
public void onSuccess() {
testing.progressiveStop();
}
@Override
public void onError() {
testing.progressiveStop();
}
});
我的主要活动:
public class Main extends AppCompatActivity {
private RecyclerView recyclerView;
private Bidhaa_Recycler_Adapter bidhaaRecyclerAdapter;
FirebaseFirestore firestoreBidhaa;
SmoothProgressBar loading_Progress;
List <Object> objectList = new ArrayList<>();
static {
FirebaseFirestore.setLoggingEnabled(true);
}
public static final int ITEMS_PER_AD = 2;
private static final String AD_UNIT_ID = "************";
private static final String TAG = "Bidhaa_Activity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firestoreBidhaa = FirebaseFirestore.getInstance();
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setPersistenceEnabled(true)
.build();
firestoreBidhaa.setFirestoreSettings(settings);
recyclerView = findViewById(R.id.bidhaa_Recycler);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
loading_Progress = findViewById(R.id.progress);
loading_Progress.applyStyle(R.style.PocketProgressBar);
loading_Progress.progressiveStop();
CollectionReference bidhaaCollection = firestoreBidhaa.collection("BIDHAA");//.orderBy("Muuzaji");
Query query = bidhaaCollection.orderBy("Muuzaji");
bidhaaRecyclerAdapter = new Bidhaa_Recycler_Adapter(query) {
@Override
protected void onDataChanged() {
Log.d(TAG, "Data Change");
recyclerView.scrollToPosition(0);
loading_Progress.progressiveStop();
}
@Override
protected void onError(FirebaseFirestoreException e) {
handleFirebaseException(e);
}
};
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
recyclerView.setAdapter(new Bidhaa_Recycler_Adapter(query));
recyclerView.setAdapter(bidhaaRecyclerAdapter);
addNativeExpressAds();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (bidhaaRecyclerAdapter != null) {
bidhaaRecyclerAdapter.startListening();
}
}
private void addNativeExpressAds() {
for (int i = 0; i <= objectList.size(); i += ITEMS_PER_AD) {
NativeExpressAdView adView = new NativeExpressAdView(Main.this);
adView.setAdUnitId(AD_UNIT_ID);
objectList.add(i, adView);
}
recyclerView.post(new Runnable() {
@Override
public void run() {
final float density = Main.this.getResources().getDisplayMetrics().density;
AdSize adSize = new AdSize((int)(recyclerView.getWidth() / density), 150);
for (int i = 0; i <=objectList.size(); i += 2){
NativeExpressAdView adViewSize = (NativeExpressAdView) objectList.get(i);
adViewSize.setAdSize(adSize);
adViewSize.loadAd(new AdRequest.Builder().build());
}
}
});
}
@Override
public void onStart() {
super.onStart();
bidhaaRecyclerAdapter.startListening();
loading_Progress.progressiveStart();
}
@Override
public void onStop() {
super.onStop();
bidhaaRecyclerAdapter.stopListening();
loading_Progress.progressiveStop();
}
private void handleFirebaseException(FirebaseFirestoreException e) {
if (e.getCode() == FirebaseFirestoreException.Code.PERMISSION_DENIED) {
loading_Progress.progressiveStop();
Alerter.create(this)
.enableSwipeToDismiss()
.setText("Permission Denied")
.setTitle("Error")
.show();
}
}
}
原生广告持有人:
package com.ngara_k.bidhaa;
import android.support.v7.widget.RecyclerView;
import android.view.View;
/**
* Created by Ngara-K on 11/2/2017.
*/
class Native_Ad_ViewHolder extends RecyclerView.ViewHolder {
public Native_Ad_ViewHolder(View itemView) {
super(itemView);
}
}
这是我的logcat:
11-02 16:34:31.532 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzeep]: Starting transaction: Apply remote event
11-02 16:34:31.542 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream received: # com.google.android.gms.internal.zzepe@33e4dc84
11-02 16:34:31.542 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzeep]: Starting transaction: Apply remote event
11-02 16:35:23.091 19435-19435/com.ngara_k.bidhaa I/Ads: Ad is not visible. Not refreshing ad.
11-02 16:35:23.091 19435-19435/com.ngara_k.bidhaa I/Ads: Scheduling ad refresh 60000 milliseconds from now.
11-02 16:35:31.601 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream received: # com.google.android.gms.internal.zzepe@da43d34f
11-02 16:35:31.601 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzeep]: Starting transaction: Apply remote event
11-02 16:36:23.110 19435-19435/com.ngara_k.bidhaa I/Ads: Ad is not visible. Not refreshing ad.
11-02 16:36:23.110 19435-19435/com.ngara_k.bidhaa I/Ads: Scheduling ad refresh 60000 milliseconds from now.
11-02 16:36:31.740 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream received: # com.google.android.gms.internal.zzepe@65567c39
11-02 16:36:31.740 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzeep]: Starting transaction: Apply remote event
11-02 16:37:23.120 19435-19435/com.ngara_k.bidhaa I/Ads: Ad is not visible. Not refreshing ad.
11-02 16:37:23.120 19435-19435/com.ngara_k.bidhaa I/Ads: Scheduling ad refresh 60000 milliseconds from now.
11-02 16:37:31.709 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream received: # com.google.android.gms.internal.zzepe@f499aecf
11-02 16:37:31.709 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzeep]: Starting transaction: Apply remote event
11-02 16:38:23.129 19435-19435/com.ngara_k.bidhaa I/Ads: Ad is not visible. Not refreshing ad.
11-02 16:38:23.129 19435-19435/com.ngara_k.bidhaa I/Ads: Scheduling ad refresh 60000 milliseconds from now.
11-02 16:38:31.829 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream received: # com.google.android.gms.internal.zzepe@d1555759
11-02 16:38:31.829 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzeep]: Starting transaction: Apply remote event
11-02 16:39:23.128 19435-19435/com.ngara_k.bidhaa I/Ads: Ad is not visible. Not refreshing ad.
11-02 16:39:23.128 19435-19435/com.ngara_k.bidhaa I/Ads: Scheduling ad refresh 60000 milliseconds from now.
11-02 16:39:31.418 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream closed with status: zzcq{code=INTERNAL, description=Received unexpected EOS on DATA frame from server., cause=null}.
11-02 16:39:31.418 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream sending: # com.google.android.gms.internal.zzepc@9820cb7d
11-02 16:39:31.418 19435-19435/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzebn]: Firebase Auth is not available, getToken is returning no token.
11-02 16:39:31.418 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [FirestoreCallCredentials]: Successfully fetched token.
11-02 16:39:31.418 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream is ready
11-02 16:39:33.348 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream received headers: {date=Thu, 02 Nov 2017 13:39:34 GMT}
11-02 16:39:33.348 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream received: # com.google.android.gms.internal.zzepe@72b77077
11-02 16:39:33.348 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream received: # com.google.android.gms.internal.zzepe@c415b40d
11-02 16:39:33.478 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream received: # com.google.android.gms.internal.zzepe@1a60a68
11-02 16:39:33.478 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream received: # com.google.android.gms.internal.zzepe@aa6fb0f7
11-02 16:39:33.478 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream received: # com.google.android.gms.internal.zzepe@117ebd5c
11-02 16:39:33.478 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzeep]: Starting transaction: Apply remote event
11-02 16:39:33.488 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream received: # com.google.android.gms.internal.zzepe@f4f6907d
11-02 16:39:33.488 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzeep]: Starting transaction: Apply remote event
11-02 16:40:23.127 19435-19435/com.ngara_k.bidhaa I/Ads: Ad is not visible. Not refreshing ad.
11-02 16:40:23.127 19435-19435/com.ngara_k.bidhaa I/Ads: Scheduling ad refresh 60000 milliseconds from now.
11-02 16:40:33.537 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream received: # com.google.android.gms.internal.zzepe@9cd50d7a
11-02 16:40:33.547 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzeep]: Starting transaction: Apply remote event
11-02 16:41:23.127 19435-19435/com.ngara_k.bidhaa I/Ads: Ad is not visible. Not refreshing ad.
11-02 16:41:23.127 19435-19435/com.ngara_k.bidhaa I/Ads: Scheduling ad refresh 60000 milliseconds from now.
11-02 16:41:33.586 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream received: # com.google.android.gms.internal.zzepe@721bf245
11-02 16:41:33.586 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzeep]: Starting transaction: Apply remote event
11-02 16:42:23.126 19435-19435/com.ngara_k.bidhaa I/Ads: Ad is not visible. Not refreshing ad.
11-02 16:42:23.126 19435-19435/com.ngara_k.bidhaa I/Ads: Scheduling ad refresh 60000 milliseconds from now.
11-02 16:42:33.736 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzejk]: (4a7ea5f4) Stream received: # com.google.android.gms.internal.zzepe@1d4d8242
11-02 16:42:33.736 19435-19450/com.ngara_k.bidhaa I/Firestore: (0.6.6-dev) [zzeep]: Starting transaction: Apply remote event