2016-07-30 147 views
0

下午好,android.support.v4.widget.NestedScrollView不能转换为android.support.v7.widget.RecyclerView

我试图创建一个包括项RecyclerView片段。

以上这些项目,我需要把一些标题与一些过滤器。

这里是片段的XML (fragment_products_list)

<?xml version="1.0" encoding="utf-8"?> 

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginTop="12sp" 
      android:id="@+id/filters_frame"> 
      <include layout="@layout/expandable_filters"/> 
     </RelativeLayout> 

     <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/recyclerview" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

    </LinearLayout> 
</android.support.v4.widget.NestedScrollView> 

这里是OnCreateView在我的片段Java文件(ListFragment.java)

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    RecyclerView rv = (RecyclerView) inflater.inflate(
      R.layout.fragment_products_list, container, false); 

    //Product filters 
    expandFiltersButton = (ImageButton) getView().findViewById(R.id.expand_filters); 
    expandableZone = (LinearLayout) getView().findViewById(R.id.expandable_zone); 

    expandFiltersButton.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      if(expandableZone.isShown()){ 
       expandableZone.setVisibility(View.GONE); 
       expandFiltersButton.setImageResource(R.drawable.ic_more_24dp); 
      }else{ 
       expandableZone.setVisibility(View.VISIBLE); 
       expandFiltersButton.setImageResource(R.drawable.ic_less_24dp); 
      } 
     } 
    }); 

    setupRecyclerView(rv); 

    mRecyclerView = rv; 
    loadUpdates(); 
    return rv; 
} 

我有这样的错误代码当我试图运行该应用程序:

android.support.v4.widget.NestedScrollView cannot be cast to android.support.v7.widget.RecyclerView 

什么问题,我该如何解决? 感谢您的帮助!

回答

1

试试这个:

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(
      R.layout.fragment_products_list, container, false); 


    //Product filters 
    RecyclerView rv = (RecyclerView) view.findViewById(R.id.recyclerview); 
    expandFiltersButton = (ImageButton) view.findViewById(R.id.expand_filters); 
    expandableZone = (LinearLayout) view.findViewById(R.id.expandable_zone); 

    expandFiltersButton.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      if(expandableZone.isShown()){ 
       expandableZone.setVisibility(View.GONE); 
       expandFiltersButton.setImageResource(R.drawable.ic_more_24dp); 
      }else{ 
       expandableZone.setVisibility(View.VISIBLE); 
       expandFiltersButton.setImageResource(R.drawable.ic_less_24dp); 
      } 
     } 
    }); 

    setupRecyclerView(rv); 

    mRecyclerView = rv; 
    loadUpdates(); 
    return view; 
} 
+0

你找到最终的解决方案,非常感谢你! –

+0

很高兴帮助! –