【问题标题】:RecyclerView is not showingRecyclerView 未显示
【发布时间】:2017-09-06 05:59:59
【问题描述】:

我的活动中有SpinnerEditTextRecyclerView。一切都很顺利,但有时我的 recyclerView 是不可见的,直到我在我的 Activity 中单击 EditText。我调试我的代码,一切都在适配器中传递

 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 显示数据时

在清单中我添加了:

 <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 响应中调用此方法:

private void getProductList(String str) 抛出 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);
            }
        });
    }

}

【问题讨论】:

  • 为什么你有这行 et_productname.requestFocus();尝试删除它
  • 我后来添加了它,因为当我点击 edittext 时我的 recyclerview 正在显示,所以我认为通过请求焦点可以完成
  • 您添加了 android:focusableInTouchMode="true" 和 android:descendantFocusability="beforeDescendants" 也是因为它?
  • 只是为了测试你可以删除 android:focusableInTouchMode="true", android:descendantFocusability="beforeDescendants" 这两个属性和 et_productname.requestFocus();在代码中并尝试
  • 现在上面试过了,删除了 android:focusableInTouchMode="true", android:descendantFocusability="beforeDescendants" et_productname.requestFocus();但仍然遇到同样的问题

标签: android android-recyclerview


【解决方案1】:

您应该删除rcycler_productlist.smoothScrollToPosition(0); 并重试。

貌似placeOrderAdapter.notifyDataSetChanged();placeOrderAdapter.notifyItemInserted(productNLCModels.size());这两个语句只有一个意思,一次是通知数据集,然后通知插入的数据,而是只需要写placeOrderAdapter.notifyItemInserted(productNLCModels.size());,去掉notifyDataSetChanged();

【讨论】:

  • rcycler_productlist.smoothScrollToPosition(0) 我添加了这一行,因为它总是关注最后一项
  • 我认为 rcycler_productlist.smoothScrollToPosition(0) 导致了这个问题,我删除了它并且它可以工作,但我不明白为什么它会导致这个问题
  • 其实recyclerview不能用scrollToPosition(pos),但是滚动recyclerview的实际方法是用LinearLayoutManager.scrollToPositionWithOffset(int,int)。试着做对。
【解决方案2】:

这可能是由于您的回收站视图中的平滑滚动问题而发生的,因为它改变了您的回收站视图的焦点。尝试消除或删除

rcycler_productlist.smoothScrollToPosition(0); 

【讨论】:

    猜你喜欢
    • 2021-12-20
    • 2022-01-26
    • 2017-11-18
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多