RecyclerView在调用getItemCount时未调用onCreateViewHolder方法

时间:2019-09-19 18:37:34

标签: android android-recyclerview recycler-adapter

我面临一个非常奇怪的问题。我正在使用回收站视图并传递数据的ArrayList。该列表包含我已检查的数据,并且适配器正在调用“ getItemCount()”方法,但未调用“ onCreateViewHolder / onBindViewHolder ”方法。以下是我正在使用的代码

XML:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ContactsListActivity">


<TextView
    android:id="@+id/tv_network_notifier_view"
    android:layout_width="match_parent"
    android:layout_height="@dimen/network_notifier_width"
    android:background="@android:color/black"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:visibility="gone" />

<androidx.appcompat.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="@dimen/tool_bar_height"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/backButton"
            android:layout_width="@dimen/action_icon"
            android:layout_height="@dimen/action_icon"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:background="?selectableItemBackgroundBorderless"
            android:padding="10dp"
            android:src="@drawable/back_ico" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_gravity="center"
            android:paddingLeft="10dp"
            android:text="Contacts"
            android:textColor="@color/white"
            android:textSize="@dimen/text_heading_title" />


    </RelativeLayout>
</androidx.appcompat.widget.Toolbar>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary">

    <androidx.appcompat.widget.SearchView
        android:id="@+id/contactSearchView"
        android:layout_width="match_parent"
        android:layout_height="@dimen/button_height_dialog"
        android:layout_marginLeft="23dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="23dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/button_fill_white"
        app:iconifiedByDefault="false" />
</LinearLayout>

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swipeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/contactsList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/ll_contact_number"
            android:layout_marginTop="5dp" />

        <LinearLayout
            android:id="@+id/nofriendAvailable"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical"
            android:visibility="gone">

            <ImageView
                android:layout_width="@dimen/logo_size"
                android:layout_height="@dimen/logo_size"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="40dp"
                android:src="@drawable/ic_gray_logo" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="@dimen/edittext_height"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:text="No Friends Available"
                android:textAllCaps="false"
                android:textColor="@color/colorGray"
                android:textSize="@dimen/text_very_small" />
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/ll_contact_number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/rl_dialer_pad"
            android:background="@color/white"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/et_phone_number"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/text_main"
                android:clickable="false"
                android:cursorVisible="false"
                android:focusable="false"
                android:gravity="fill_vertical|center"
                android:maxLines="1"
                android:scrollHorizontally="true"
                android:textColor="@android:color/black"
                android:textSize="@dimen/phone_number_text_size"
                android:textStyle="bold"
                android:visibility="gone" />

            <ImageView
                android:id="@+id/img_delete_number"
                android:layout_width="@dimen/delete_number_width"
                android:layout_height="@dimen/delete_number_height"
                android:layout_alignParentRight="true"
                android:layout_marginRight="@dimen/margin_right_for_dialer_number_delete"
                android:layout_marginBottom="@dimen/margin_bottom_for_dialer_number_delete"
                android:onClick="onClick"
                android:padding="@dimen/padding_delete_number"
                android:src="@drawable/back_dial_ico"
                android:visibility="gone" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rl_dialer_pad"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/rl_bottom_dialer_view"
            android:background="@color/white"
            android:paddingLeft="@dimen/dialer_button_margin_left"
            android:paddingRight="@dimen/dialer_button_margin_right"
            android:visibility="gone">

            <LinearLayout
                android:id="@+id/Row1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:gravity="center"
                android:orientation="horizontal"
                android:weightSum="5">
                <!--  Buttons 1 2 3 -->
                <ImageButton
                    android:id="@+id/Button1"
                    style="@style/DialerButton"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:onClick="onClick"
                    android:src="@drawable/num1" />

                <ImageButton
                    android:id="@+id/Button2"
                    style="@style/DialerButton"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:onClick="onClick"
                    android:src="@drawable/num2" />

                <ImageButton
                    android:id="@+id/Button3"
                    style="@style/DialerButton"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:onClick="onClick"
                    android:src="@drawable/num3" />

            </LinearLayout>

            <!-- Buttons 4 5 6 -->
            <LinearLayout
                android:id="@+id/Row2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/Row1"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:gravity="center"
                android:weightSum="5">

                <ImageButton
                    android:id="@+id/Button4"
                    style="@style/DialerButton"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:onClick="onClick"
                    android:src="@drawable/num4" />

                <ImageButton
                    android:id="@+id/Button5"
                    style="@style/DialerButton"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:onClick="onClick"
                    android:src="@drawable/num5" />

                <ImageButton
                    android:id="@+id/Button6"
                    style="@style/DialerButton"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:onClick="onClick"
                    android:src="@drawable/num6" />

            </LinearLayout>

            <!-- Buttons 7 8 9 -->
            <LinearLayout
                android:id="@+id/Row3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/Row2"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:gravity="center"
                android:weightSum="5">

                <ImageButton
                    android:id="@+id/Button7"
                    style="@style/DialerButton"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:onClick="onClick"
                    android:src="@drawable/num7" />

                <ImageButton
                    android:id="@+id/Button8"
                    style="@style/DialerButton"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:onClick="onClick"
                    android:src="@drawable/num8" />

                <ImageButton
                    android:id="@+id/Button9"
                    style="@style/DialerButton"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:onClick="onClick"
                    android:src="@drawable/num9" />
            </LinearLayout>

            <!-- Buttons * 0 # -->
            <LinearLayout
                android:id="@+id/Row4"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/Row3"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:gravity="center"
                android:weightSum="5">

                <ImageButton
                    android:id="@+id/ButtonStar"
                    style="@style/DialerButton"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:onClick="onClick"
                    android:src="@drawable/num0l" />

                <ImageButton
                    android:id="@+id/Button0"
                    style="@style/DialerButton"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:onClick="onClick"
                    android:src="@drawable/num0" />

                <ImageButton
                    android:id="@+id/ButtonHash"
                    style="@style/DialerButton"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:onClick="onClick"
                    android:src="@drawable/num0r" />
            </LinearLayout>

        </RelativeLayout>

        <RelativeLayout

            android:id="@+id/rl_bottom_dialer_view"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dialer_button_height"
            android:layout_alignParentBottom="true"
            android:background="@color/white">

            <ImageView
                android:id="@+id/iv_dialer_opener"
                android:layout_width="@dimen/dialer_button_width"
                android:layout_height="@dimen/dialer_button_height"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:src="@drawable/dialer_opener"
                android:tag="dialer" />

            <ImageView
                android:id="@+id/iv_dialer_closer"
                android:layout_width="@dimen/dialer_button_width"
                android:layout_height="@dimen/dialer_button_height"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:src="@drawable/dial_ico"
                android:tag="dialer"
                android:visibility="gone" />
        </RelativeLayout>

    </RelativeLayout>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

下面是Adater

public class ContactCallAdapter extends RecyclerView.Adapter<ContactCallAdapter.ViewHolder> {

private static final String TAG = ContactCallAdapter.class.getSimpleName();

private List<ContactModel> contactsList;
private List<ContactModel> contactsListCopy = new ArrayList<>();
private Context context;
private OnContactClickListener onContactClickListener;
private OnCallClickListener onCallClickListener;
private boolean isCall = false;
boolean isPending = false;
private int callType = 0;
private OnInviteCLickListener onInviteCLickListener;
private OnCancelClickListener onCancelClickListener;

public interface OnInviteCLickListener {
    void onitemClick(String fromWhere, String customerLoginID);
}

public interface OnCancelClickListener {
    void onClick(ContactModel contactModel);
}


public ContactCallAdapter(OnInviteCLickListener listener, int callType, List<ContactModel> contactsList, OnContactClickListener onContactClickListener,
                          OnCallClickListener onCallClickListener) {

    LogUtility.logInfo(TAG, "Constructor is called");
    this.contactsList = contactsList;
    contactsListCopy.addAll(contactsList);
    this.onContactClickListener = onContactClickListener;
    this.onCallClickListener = onCallClickListener;
    this.callType = callType;
    this.onInviteCLickListener = listener;
    if (callType != 0) isCall = true;
}



@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LogUtility.logInfo(TAG, "onCreateViewHolder is called");
    context = parent.getContext();
    Activity activity = (Activity) context;
    FontsUtility.applyCustomFont(activity);

    return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact_for_call, parent, false));
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
    LogUtility.logInfo(TAG, "onBindViewHolder is called");
    holder.bind(position, onContactClickListener, onCallClickListener);
}

@Override
public int getItemCount() {
    LogUtility.logInfo(TAG, "getItemCount and count is " + contactsList.size() + " hashcode is " + contactsList.hashCode());
    return contactsList.size();
}

下面是活动代码

public class ContactsListActivity extends BaseActivity implements ContactCallAdapter.OnInviteCLickListener, OnContactClickListener {

private final String TAG = ContactsListActivity.class.getSimpleName();
private static final byte ZER0 = 0;

private TextView tvNetworkStatusNotifier;
private boolean shouldShowDialer;

public static synchronized ContactsListActivity getInstance() {
    return mInstance;
}

static ContactsListActivity mInstance = null;

private RecyclerView recyclerView;
private androidx.appcompat.widget.SearchView contactSearchView;
private ImageButton imgBtn0, imgBtn1, imgBtn2, imgBtn3, imgBtn4, imgBtn5, imgBtn6, imgBtn7, imgBtn8, imgBtn9, imgBtnAsteric, imgBtnHash;
private ImageView imgDeleteNumber, backButton, ivDialerPadOpener, ivDialerCloser;
private RelativeLayout rlDialerPad;
private EditText etPhoneNumber;
private RelativeLayout rlContactNumber, rlBottomViewDialer;

ContactCallAdapter adapter;
Context context;
AppSharedPref pref;
Dialog progressDialog;
protected IHubProxy hub = null;
ArrayList<ContactModel> arrayListContact;
ArrayList<RequestModel> arrayListRequest;
ArrayList<RequestModel> arrayListRequestCount;
ArrayList<RequestModel> arrayListRequestOtherContact;

int callType = 0;
ArrayList<ContactModel> arrayPendingListContact;
TextView tvNotificationcount;
Integer whichOneUpdate = null;
SwipeRefreshLayout swipeLayout;

Dialog dialog;

boolean update = false;
LinearLayout nofriendAvailable;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts_list);

    FontsUtility.applyCustomFont(this);
    context = this;
    mInstance = this;
    if (getResources().getBoolean(R.bool.portrait_only)) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else {

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
    }

    pref = AppSharedPref.getInstance();
    progressDialog = Utils.progressDialog(context);
    recyclerView = findViewById(R.id.contactsList);
    nofriendAvailable = findViewById(R.id.nofriendAvailable);


    imgDeleteNumber = findViewById(R.id.img_delete_number);
    ivDialerPadOpener = findViewById(R.id.iv_dialer_opener);
    ivDialerCloser = findViewById(R.id.iv_dialer_closer);

    rlDialerPad = findViewById(R.id.rl_dialer_pad);
    rlBottomViewDialer = findViewById(R.id.rl_bottom_dialer_view);

    etPhoneNumber = findViewById(R.id.et_phone_number);

    tvNetworkStatusNotifier = findViewById(R.id.tv_network_notifier_view);
    tvNotificationcount = findViewById(R.id.tvNotificationcount);
    contactSearchView = findViewById(R.id.contactSearchView);

    rlContactNumber = findViewById(R.id.ll_contact_number);

    contactSearchView.setQueryHint("Search");
    EditText searchEditText =              

searchEditText.setHintTextColor(getResources().getColor(R.color.colorGray));
    ImageView icon = contactSearchView.findViewById(R.id.search_button);
    icon.setImageResource(R.drawable.search_gray_ico_la);
    icon.setColorFilter(Color.BLACK);
    ImageView iconClose = 
contactSearchView.findViewById(R.id.search_close_btn);
    iconClose.setColorFilter(getResources().getColor(R.color.black2));
    iconClose.setImageResource(R.drawable.close_gray_ico_la);
    arrayListContact = new ArrayList<>();
    arrayListRequest = new ArrayList<>();
    arrayListRequestCount = new ArrayList<>();
    arrayPendingListContact = new ArrayList<>();
    arrayListRequestOtherContact = new ArrayList<>();


    whichOneUpdate = 1;
    contactSearchView.setVisibility(View.VISIBLE);
    Intent intent = getIntent();
    if (intent.hasExtra(Constants.PARAM_TYPE)) {
        callType = intent.getIntExtra(Constants.PARAM_TYPE, 0);

        //from call
    }

    if (intent.hasExtra(Constants.PARAM_SHOULD_SHOW_DIALER)) {
        shouldShowDialer = intent.getBooleanExtra(Constants.PARAM_SHOULD_SHOW_DIALER, false);
        if (shouldShowDialer)
            rlBottomViewDialer.setVisibility(View.VISIBLE);
        else
            rlBottomViewDialer.setVisibility(View.GONE);
    }

    swipeLayout.setOnRefreshListener(() -> {
        contactSearchView.setQuery("", false);
        swipeLayout.setRefreshing(true);
        //getContacts(true);
        getAllContactsAgain();

        swipeLayout.setRefreshing(false);
    });

    contactSearchView.setOnQueryTextListener(new androidx.appcompat.widget.SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String newText) {
            if (adapter != null)
                adapter.filter(newText);

            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if (adapter != null)
                adapter.filter(newText);

            return true;
        }
    });

    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    getAllContactsAgain();
}

}

public void getAllContactsAgain() {
    arrayListContact.clear();
    arrayListRequestOtherContact.clear();
    arrayPendingListContact.clear();
    if (Utils.checkPermissionContact(context))
        if (shouldShowDialer)
            arrayListContact = getContactsList();
    getContacts(false);
}

void getContacts(final boolean addtoRecyler) {
    if (HubUtility.reconnectHubIfRequired(this, TAG))
        return;

    showDialog();

    ArrayList<String> settingsList = new ArrayList<>();
    settingsList.add("" + pref.getUserId());

    hub = CallReceivingService.hub;
    hub.Invoke(Constants.HUB_CONTACTS, settingsList,
            new HubInvokeCallback() {

                @Override
                public void OnResult(boolean status, String response) {

                    dismissDialog();
                    swipeLayout.setRefreshing(false);

                    try {
                        ContactPojo contactPojo = new 
Gson().fromJson(response, ContactPojo.class);
                        if (contactPojo != null) {
                            if (contactPojo.isStatus()) {
                                arrayPendingListContact.clear();
                                //arrayListContact.clear();

arrayPendingListContact.addAll(contactPojo.getUserList());

                                for (int a = 0; a < 
arrayPendingListContact.size(); a++) {
                                    if 
(arrayPendingListContact.get(a).getCustomerContactType() == 
Constants.ACCEPTED 
||

arrayPendingListContact.get(a).getCustomerContactType() == 
Constants.ACCEPTED_BY) {

                                        arrayListContact.add(0, 
contactPojo.getUserList().get(a));
                                    }
                                }

                                Utils.e("Hub Size", "" + 
arrayListContact.size());
                                adapter = new 
ContactCallAdapter(ContactsListActivity.this::onitemClick, callType, 
arrayListContact,
                                        ContactsListActivity.this,
                                        contactModel -> {
                                            //allowed call on behalf of 
statuses
                                            if (contactModel.isPhone()) {
                                                Intent inte = getIntent();

inte.putExtra(Constants.PARAM_SIP_CALL, contactModel.getNumber());

 inte.putExtra(Constants.PARAM_SIP_CALLER_NAME, contactModel.getName());
                                                setResult(RESULT_OK, inte);
                                                finish();
                                            } else {
                                                if 
(contactModel.getCustomerCommunicationStatus() == Constants.ONLINE &&

(contactModel.getCustomerContactType() == Constants.ACCEPTED ||

contactModel.getCustomerContactType() == Constants.ACCEPTED_BY)) {
                                                    Intent inte = 
getIntent();

inte.putExtra(Constants.PARAM_CONTACT, contactModel);
                                                    setResult(RESULT_OK, 
inte);
                                                    finish();
                                                } else {
                                                    Utils.showMessage("User 
is not available.");
                                                }
                                            }
                                        });


                                recyclerView.setAdapter(adapter);
                                recyclerView.requestFocus();
                            }
                        }
                        isEmpty();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                }

                @Override
                public void OnError(Exception e) {

                }
            });
}

任何帮助将不胜感激。尽管已经有人问过这个问题。我检查了那些。这些在我的情况下无效

  1. 从getIemCount方法返回零
  2. 未通过布局管理器

1 个答案:

答案 0 :(得分:0)

根据“刷卡刷新”布局的官方文档 “此布局应成为视图的父级,该视图将由于手势而刷新,并且只能支持一个直接子级。” 因此,请考虑重新布局您的布局,以便RecyclerView是“刷新刷新”布局下的唯一子级。