带有PagerAdapter的ViewPager不显示任何内容

时间:2017-04-20 23:23:52

标签: android firebase firebase-realtime-database android-viewpager android-pageradapter

我的ViewPager没有显示任何内容。而且我也不知道为什么它是空的。你有想法解决这个问题吗?这是我的代码:

CustomPageAdapter.java

public class CustomPageAdapter extends PagerAdapter implements ValueEventListener {

Context mContext;
private Context context;
private HashMap<String, Araba> arabalar;
private DatabaseReference databaseReference;
private String userId;
private LayoutInflater inflater;
private List<String> arabaPlakalari;


public CustomPageAdapter(@NonNull Context context,
                    @NonNull List<String> arabaPlakalar,
                    final DatabaseReference databaseReference,
                    final String userId) {

    this.context = context;
    this.arabaPlakalari = arabaPlakalar;
    arabalar = new HashMap<>();
    this.databaseReference = databaseReference;
    this.userId = userId;
    databaseReference.child("Arabalar")
            .child(userId)
            .addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    String plaka = dataSnapshot.getKey();
                    if(arabalar.get(plaka) == null){
                        databaseReference.child("Arabalar")
                                .child(userId)
                                .child(plaka).addValueEventListener(CustomPageAdapter.this);
                    }

                    notifyDataSetChanged();
                }

                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {
                    String plaka = dataSnapshot.getKey();
                    if(arabalar.get(plaka) != null){
                        arabaPlakalari.remove(plaka);
                        arabalar.remove(plaka);
                        databaseReference.child("Arabalar")
                                .child(userId)
                                .child(plaka)
                                .removeEventListener(CustomPageAdapter.this);
                    }

                    notifyDataSetChanged();
                }

                @Override
                public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
}



@Override
public Object instantiateItem(ViewGroup container, int position)
{
    String plaka = arabaPlakalari.get(position);
    Araba arabaBilgisi = arabalar.get(plaka);
    TextView muayeneTarihiTextView;
    TextView emisyonTarihiTextView;
    TextView sigortaTarihiTextView;
    TextView kaskoTarihiTextView;

    View viewLayout = LayoutInflater.from(context).inflate(R.layout.list_item_custom, container, false);
    muayeneTarihiTextView = (TextView) viewLayout.findViewById(R.id.muayeneTarihiTextView);
    kaskoTarihiTextView = (TextView) viewLayout.findViewById(R.id.kaskoTarihiTextView);
    emisyonTarihiTextView = (TextView) viewLayout.findViewById(R.id.emisyonTarihiTextView);
    sigortaTarihiTextView = (TextView) viewLayout.findViewById(R.id.sigortaTarihiTextView);

    muayeneTarihiTextView.setText(arabaBilgisi.getEditTextEmisyonTarihi());
    kaskoTarihiTextView.setText(arabaBilgisi.getEditTextKaskoTarihi());
    sigortaTarihiTextView.setText(arabaBilgisi.getEditTextSigortaTarihi());
    emisyonTarihiTextView.setText(arabaBilgisi.getEditTextEmisyonTarihi());
    container.addView(viewLayout);
    return viewLayout;
}



@Override
public int getCount() {
    return arabalar.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return false;
}

@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
    Araba araba = dataSnapshot.getValue(Araba.class);
    String plaka = dataSnapshot.getKey();
    araba.setEditTextPlaka(plaka);
    arabalar.put(plaka, araba);
    if(!arabaPlakalari.contains(plaka)){
        arabaPlakalari.add(plaka);
    }

    notifyDataSetChanged();
}


@Override
public void onCancelled(DatabaseError databaseError) {

}

YaklasanlarFragment.java(它的片段)

public class YaklasanlarFragment extends Fragment {


String userId;
FirebaseAuth firebaseAuth;
DatabaseReference dref;
ListView listview;
ArrayList<String> list=new ArrayList<>();
public ViewPager viewPager = null;
PagerAdapter adapter;
public  YaklasanlarFragment(){}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view= inflater.inflate(R.layout.activity_yaklasanlar_fragment, container, false);
    firebaseAuth=FirebaseAuth.getInstance();
    userId=firebaseAuth.getCurrentUser().getUid().toString();
    dref = FirebaseDatabase.getInstance().getReference();
    viewPager = (ViewPager) view.findViewById(R.id.viewPager);
    adapter=new CustomPageAdapter(getActivity(), new ArrayList<String>(),  dref, userId);
    viewPager.setAdapter(adapter);
    return view;
}

}

yaklasanlar_fragment.xml

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="wrap_content"
    android:layout_height="545dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true">
</android.support.v4.view.ViewPager>

list_item_custom.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_yaklasanlar_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.yunus.ototakip.YaklasanlarFragment">


<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="wrap_content"
    android:layout_height="545dp"
    android:layout_alignParentStart="true"
    android:layout_alignTop="@+id/linearLayout"
    android:layout_alignParentBottom="true">
</android.support.v4.view.ViewPager>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginTop="49dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:id="@+id/linearLayout">

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/divide1">

            <TextView
                android:id="@+id/muayeneTarihiTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:text="Muayene Tarihi bitimine"
                android:textColor="@color/beyaz"
                android:textSize="17sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="75dp"
                android:gravity="center"
                android:text="GÜN KALDI"
                android:textColor="@color/beyaz"
                android:textSize="17sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/muayeneFark"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="230dp"
                android:text="0"
                android:textAlignment="center"
                android:textColor="@color/beyaz"
                android:textSize="36sp"
                android:textStyle="bold" />
        </RelativeLayout>

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/divide2">

            <TextView
                android:id="@+id/sigortaTarihiTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:text="Sigorta Tarihi bitimine"
                android:textColor="@color/beyaz"
                android:textSize="17sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/sigortaFark"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="75dp"
                android:gravity="center"
                android:text="GÜN KALDI"
                android:textColor="@color/beyaz"
                android:textSize="17sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView8"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="230dp"
                android:text="0"
                android:textAlignment="center"
                android:textColor="@color/beyaz"
                android:textSize="36sp"
                android:textStyle="bold" />
        </RelativeLayout>
    </LinearLayout>


    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/divide1">

            <TextView
                android:id="@+id/kaskoTarihiTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:text="Kasko Tarihi bitimine"
                android:textColor="@color/beyaz"
                android:textSize="17sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView10"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="75dp"
                android:gravity="center"
                android:text="GÜN KALDI"
                android:textColor="@color/beyaz"
                android:textSize="17sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/kaskoFark"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="230dp"
                android:text="0"
                android:textAlignment="center"
                android:textColor="@color/beyaz"
                android:textSize="36sp"
                android:textStyle="bold" />
        </RelativeLayout>

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/divide3">

            <TextView
                android:id="@+id/emisyonTarihiTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:text="Emisyon Değişim Tarihine"
                android:textColor="@color/beyaz"
                android:textSize="17sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView13"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="75dp"
                android:gravity="center"
                android:text="GÜN KALDI"
                android:textColor="@color/beyaz"
                android:textSize="17sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/emisyonFark"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="230dp"
                android:text="0"
                android:textAlignment="center"
                android:textColor="@color/beyaz"
                android:textSize="36sp"
                android:textStyle="bold" />
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

问题是isViewFromObject方法。我忘了在方法里面写。如果您要使用ViewPager,您还必须添加destroyItem()方法。

@Override
public boolean isViewFromObject(View view, Object object) 
{
    return view==object;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object)
    {
        ((ViewPager) container).removeView((View) object);
    }