RecyclerView没有显示 [英] RecyclerView is not showing

查看:88
本文介绍了RecyclerView没有显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动中有SpinnerEditTextRecyclerView.一切进展顺利,但有时直到我在Activity中单击EditText时,我的recyclerView才可见.我调试我的代码,一切都通过适配器

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();

}

以上是我将数据传递到recyclerView的方法.它将数据加载到我的Recyclerview中,但是由于某些原因我不知道它没有显示,但是当我单击显示数据的editText RecyclerView时

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

我添加的清单中

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

下面是我的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>

我正在从WebApi响应中调用此方法:

I am calling this method from WebApi response:

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

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);
            }
        });
    }

}

推荐答案

之所以会发生这种情况,是因为回收站视图中的平滑滚动问题,因为它改变了回收站视图的焦点.尝试消除或删除

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); 

这篇关于RecyclerView没有显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆