簡體   English   中英

RecyclerView不會調用onCreateViewHolder和其他方法

[英]RecyclerView doesn't call onCreateViewHolder and other methods

片段具有ViewPager ,在ViewPager我們在兩個fragments之間滑動(我們將其命名為子片段)。 第一和第二sub-fragment具有RecyclerView

我不知道發生了什么,因為我在其他片段中完全以相同的方式創建了RecyclerView ,而且效果很好。

recycler_view_item :(我確定這個項目是適當的,並且其ID與Adapter類兼容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorHeaderText"
android:weightSum="100"
android:layout_marginBottom="4dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="20"
    android:weightSum="50">

    <TextView
        android:text="Client"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:id="@+id/textView8"
        android:layout_weight="15"
        style="@style/ItemPaymentsHeaderTextView"
        />

    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/clientinfo_item_client"
        android:layout_weight="35"
        style="@style/ItemPaymentsInfoTextView"/>

</LinearLayout>


<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="25"
    android:weightSum="50">

    <TextView
        android:text="Date"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:id="@+id/textView13"
        android:layout_weight="15"
        style="@style/ItemPaymentsHeaderTextView"
        />

    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/clientinfo_item_date"
        android:layout_weight="35"
        style="@style/ItemPaymentsInfoTextView"/>
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="27"
    android:weightSum="50">

    <TextView
        android:text="Payment amount"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:id="@+id/textView9"
        android:layout_weight="15"
        style="@style/ItemPaymentsHeaderTextView"
        />


    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/clientinfo_item_totalamount"
        android:layout_weight="35"
        style="@style/ItemPaymentsInfoTextView"/>


</LinearLayout>
<!--TODO jak bedzie po wszystkim to wyrzucic TYP ( określony w TabLayout)-->
<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="20"
    android:weightSum="50">


    <TextView
        android:text="Type"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:id="@+id/textView10"
        android:layout_weight="15"
        style="@style/ItemPaymentsHeaderTextView"
        />

    <TextView
        android:text="Purchase"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/clientinfo_item_type"
        android:layout_weight="35"
        style="@style/ItemPaymentsInfoTextView"/>

</LinearLayout>

<ImageButton
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/arrow_expand_24"
    android:id="@+id/clientinfo_item_downarrow"
    android:background="@color/icon_transparent"
    android:layout_weight="5"
    android:scaleType="fitCenter"
    android:elevation="0dp"
    android:layout_gravity="center_vertical|clip_horizontal"/>

包含ViewPager和TabLayout的FrameLayout

<?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="com.example.android.debtors.Fragments.FragmentSingleClientInfo">

    <android.support.design.widget.TabLayout
         android:id="@+id/clientsinfo_tabs"
         style="@style/CategoryTab"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" />

    <android.support.v4.view.ViewPager
        android:id="@+id/clientsinfo_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>  
</FrameLayout>

onCreateView-創建RecyclerView的方法

我傳遞給AdapterClientInfo的listOfPayments不為null。

 @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    listOfPayments = getPaymentsByClientId(clientsID);

    for( Payment p : listOfPayments ){
        Log.i(TAG, "onCreateView: payment : " + p.toString(true));
    }

    View rootView = inflater.inflate(R.layout.recycler_view_with_viewpager,container, false);
    AdapterClientInfo adapterClientInfo = new AdapterClientInfo(getContext(),listOfPayments);
    RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view_with_viewpager);
    setupRecyclerView(recyclerView);
    recyclerView.setAdapter(adapterClientInfo);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    return rootView;
}

在以上方法中,我為layout.recycler_view_with_viewpager充氣:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="55dp"
android:id="@+id/recycler_view_with_viewpager"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

在AdapterClientInfo中,創建實例時僅調用構造函數,而其他任何方法均無效,則不會調用任何方法。

AdapterClientInfo:

public class AdapterClientInfo  extends RecyclerView.Adapter<AdapterClientInfo.MyViewHolder> {

private static final String TAG = AdapterClientInfo.class.getSimpleName();
List<Payment> listOfPayments = new ArrayList<>();
Context context;

public AdapterClientInfo(Context context, List<Payment> list) {
    Log.i(TAG, "AdapterClientInfo: ");
    this.context = context;
    this.listOfPayments = list;
    Log.i(TAG, "AdapterClientInfo: size of list : " + list.size());
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Log.i(TAG, "onCreateViewHolder: ");
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_clients_info_payments, parent, false);
    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Log.i(TAG, "onBindViewHolder: START");
    Payment payment = listOfPayments.get(position);
    Log.i(TAG, "onBindViewHolder: payment " + payment.toString());

    String clientName = getClientByID(payment.getPaymentClientID()).getClientName();
    String[] dateArray = payment.getPaymentDate().split(" ");
    String dateString = dateArray[0];

    holder.textViewClient.setText(clientName);
    holder.textViewDate.setText(dateString);
    holder.textViewPaymentAmount.setText(String.valueOf(payment.getPaymentAmount()));
    if(payment.isPaymentGotOrGiven())//if tru
        holder.textViewType.setText("Received");
    else
        holder.textViewType.setText("Given");
    Log.i(TAG, "onBindViewHolder: END");
}

@Override
public int getItemCount() {
    Log.i(TAG, "getItemCount: " + listOfPayments.size());
    return listOfPayments.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {

    TextView textViewClient, textViewPaymentAmount, textViewDate, textViewType;


    public MyViewHolder(View itemView) {
        super(itemView);
        Log.i(TAG, "MyViewHolder: ");
        textViewClient = (TextView) itemView.findViewById(R.id.clientinfo_item_client);
        textViewPaymentAmount = (TextView) itemView.findViewById(R.id.clientinfo_item_totalamount);
        textViewDate = (TextView) itemView.findViewById(R.id.clientinfo_item_date);
        textViewType = (TextView) itemView.findViewById(R.id.clientinfo_item_type);

    }
}



private Client getClientByID(long ID){
    DatabaseClients dbClients = new DatabaseClients(context);

    Client client = dbClients.getClientByID(ID);

    return client;
}
}

我確定listOfPayments的大小大於0,所以getItemCount返回的大於零

日志顯示只有構造函數正在調用

我認為這是一個簡單的錯誤,但我無法注意到...

問題是由ViewPagerAdapter中的方法引起的。 我不知道它是否是自動生成的,是我不小心復制了它還是..我不知道,但是刪除此方法很有幫助。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM