RecyclerView is not showing

时光毁灭记忆、已成空白 提交于 2019-12-11 16:08:39

问题


I have Spinner, EditText and a RecyclerView in my Activity. Everything is going good but sometimes my recyclerView is not Visible till I click on EditText in my Activity. I debug my code everything is passed in adapter

 private void createRecycler(ArrayList<ProductNLCModel> productNLCModels) {
    rcycler_productlist.setVisibility(View.VISIBLE);
    rcycler_productlist.setHasFixedSize(true);
    SnapHelper snapHelper = new PagerSnapHelper();
    rcycler_productlist.setOnFlingListener(null);
    snapHelper.attachToRecyclerView(rcycler_productlist);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    rcycler_productlist.setLayoutManager(layoutManager);
    product_name = et_productname.getText().toString();
    PlaceOrderAdapter placeOrderAdapter = new PlaceOrderAdapter(productNLCModels, PlaceOrder.this, this, PlaceOrder.this, main_category, product_name);
    rcycler_productlist.setAdapter(placeOrderAdapter);
    placeOrderAdapter.notifyDataSetChanged();
    placeOrderAdapter.notifyItemInserted(productNLCModels.size());
    rcycler_productlist.smoothScrollToPosition(0);
    placeOrderAdapter.setClickListener(this);
    et_productname.requestFocus();

}

above is my method to passing data to recyclerView. it loads data to my Recyclerview but I don't know due to some reasons it is not showing but when I click on my editText RecyclerView showing data

in manifest i have added:

 <activity
            android:name=".PlaceOrder"
            android:configChanges="orientation"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateAlwaysHidden"
            android:launchMode="singleTask"/>

below is my XML:

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="@dimen/margin_10">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:text="Search Product via(Category or Compatibility)"
            android:textColor="@color/black"
            android:textSize="@dimen/margin_16"

            />

        <Spinner
            android:id="@+id/spinner_category"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/spinner_background" />

        <android.support.design.widget.TextInputLayout
            android:id="@+id/txtinput_productname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/shadow_logo"
            android:theme="@style/TextLabel">

            <EditText
                android:id="@+id/et_productname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Product Name"
                android:drawableRight="@drawable/searchdrawable"
                android:inputType="text"
                android:textSize="@dimen/margin_18" />
        </android.support.design.widget.TextInputLayout>

        <!--<Button-->
        <!--android:id="@+id/btn_search"-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_gravity="end"-->
        <!--android:layout_marginRight="@dimen/margin_10"-->
        <!--android:layout_marginTop="@dimen/margin_30"-->
        <!--android:background="@color/colorPrimaryDarker"-->
        <!--android:textColor="@color/white"-->
        <!--android:textSize="13sp"-->
        <!--android:padding="@dimen/margin_10"-->
        <!--android:text="Search"/>-->

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rcycler_productlist"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/margin_3"
            android:focusableInTouchMode="true"
            android:descendantFocusability="beforeDescendants"
            android:windowSoftInputMode="stateHidden|adjustPan"
            android:orientation="horizontal"/>


    </LinearLayout>

I am calling this method from WebApi response:

private void getProductList(String str) throws JSONException { ProductNLCModel productNLCModel = null;

JSONArray jsonArray = new JSONArray(str);
for (int i = 0; i < jsonArray.length(); i++) {
    productNLCModel = new ProductNLCModel();
    JSONObject productJson = jsonArray.optJSONObject(i);
    productNLCModel.setCategory(productJson.optString(WebKeys.Key_Category));
    productNLCModel.setS_no(productJson.optString(WebKeys.Sno));
    productNLCModel.setProductId(productJson.optString(WebKeys.ProductId));
    productNLCModel.setImagePath(productJson.optString(WebKeys.ImagePath));


    productNLCModel.setProductName(productJson.optString(WebKeys.Key_ProductName));
        productNLCModel.setCompatible(productJson.optString(WebKeys.Compatible));
        productNLCModel.setSpecification(productJson.optString(WebKeys.Specification));
        productNLCModel.setMRP(productJson.optString(WebKeys.MRP));
        productNLCModel.setDP(productJson.optString(WebKeys.DP));
        productNLCModel.setNLC(productJson.optString(WebKeys.NLC));
        if (i == 0) {
            JSONArray categoryDetailArray = productJson.optJSONArray(WebKeys.GetCategoryDetail);
            for (int j = 0; j < categoryDetailArray.length(); j++) {
                JSONObject discountstructure = categoryDetailArray.optJSONObject(j);
                str_categorydetail = str_categorydetail + discountstructure.optString(WebKeys.Turnover) + "@" + discountstructure.optString(WebKeys.Discount) + "%,";
            }

        }

        productNLCModels.add(productNLCModel);
    }

    if(jsonArray.length()>0){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    category_detail.setSelected(true);
                    str_categorydetail = str_categorydetail.substring(0, str_categorydetail.length() - 1);
                    category_detail.setText(str_categorydetail);
                } catch (Exception e) {
                    e.printStackTrace();

                }
                createRecycler(productNLCModels);
            }
        });
    }

}

回答1:


This may happen because of smoothscrolling issue in your recycler view because it changes the Focus of you recyclerview. Try to eliminate or remove

rcycler_productlist.smoothScrollToPosition(0); 



回答2:


You should remove the rcycler_productlist.smoothScrollToPosition(0); and try again.

Looks like placeOrderAdapter.notifyDataSetChanged(); and placeOrderAdapter.notifyItemInserted(productNLCModels.size()); these two statements has only one meaning, at a time you are notifying the data set and then you notify the inserted data, Instead you only need to write the placeOrderAdapter.notifyItemInserted(productNLCModels.size()); only and remove the notifyDataSetChanged();



来源:https://stackoverflow.com/questions/46067831/recyclerview-is-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!