4

我想實現一個水平滾動RecyclerView裏面的另一個RecyclerView這意味着父RecyclerView中的父RecyclerView(垂直滾動)和子RecyclerView(水平滾動)。我提到了有關這個要求的幾個問題,並從這個問題How to have a ListView/RecyclerView inside a parent RecyclerView?找到了一個更好的解決方案,並且成功實施。 在子適配器的佈局文件編輯後,子RecyclerView的滾動功能被禁用。Android RecyclerView滾動不工作後佈局編輯

這是我的代碼, 這是父母的片段。

public class MarketFragment extends Fragment { 

private String log = "abcpappaHomeFragment"; 
private Button loadMore ; 
private ArrayList<ProductCategoryBean> productCategoryList=null; 
private ProductCategoryService productCategoryService; 
private RecyclerView.Adapter mAdapter; 
private RecyclerView mRecyclerView; 
private LinearLayoutManager mLayoutManager; 

public MarketFragment() { 
    // Required empty public constructor 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 

    productCategoryService = new ProductCategoryService(); 
    productCategoryList =(ArrayList<ProductCategoryBean>)productCategoryService.getAllCategories(); 
    View rootView = inflater.inflate(R.layout.market_fragment, container, false); 
    mRecyclerView = (RecyclerView)rootView.findViewById(R.id.market_recycle_list); 
    mRecyclerView.setHasFixedSize(true); 
    mLayoutManager = new LinearLayoutManager(getContext()); 
    mRecyclerView.setLayoutManager(mLayoutManager); 
    mAdapter = new MarketRecyclerAdapter(getContext(),productCategoryList,this); 
    mRecyclerView.setAdapter(mAdapter); 
    return rootView; 

} 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
} 

@Override 
public void onDetach() { 
    super.onDetach(); 
} 
} 

這是LayoutFile

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:tools="http://schemas.android.com/tools" 
tools:context="bodhiinfo.abcpappa.activity.MarketFragment">> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/market_recycle_list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="4dp" /> 

父RecyclerView適配器類

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

private Context context; 
private List<ProductCategoryBean> productCategoryList=null; 
private List<ProductBean> productDetailList=null; 
private RecyclerView.Adapter mAdapter; 
public MarketFragment marketFragment; 
// private marketList 


public MarketRecyclerAdapter(Context context,List<ProductCategoryBean> productCategoryList,MarketFragment marketFragment){ 
    this.context = context; 
    this.productCategoryList =(ArrayList<ProductCategoryBean>) productCategoryList; 
    this.marketFragment = marketFragment; 
    } 

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.market_fragment_row, parent, false); 
    ViewHolder viewHolder = new ViewHolder(v,context); 
    return viewHolder; 
    } 

@Override 
public void onBindViewHolder(ViewHolder viewHolder, int position) { 

    ProductCategoryBean productCategoryBean =(ProductCategoryBean)productCategoryList.get(position); 

    viewHolder.product_category_name_id.setText(productCategoryBean.getUtxt_product_category()); 
    ProductService productService = new ProductService(); 
    productDetailList = (ArrayList<ProductBean>)productService.getProductDetailsByCategoryId(productCategoryBean.getPki_product_category_id()+""); 
    viewHolder.product_recycle_list.setHasFixedSize(false); 
    viewHolder.mLayoutManager = new ChildCustomLinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false); 
    viewHolder.product_recycle_list.setLayoutManager(viewHolder.mLayoutManager); 
    mAdapter = new MarketRowRecyclerAdapter(context,productDetailList); 
    viewHolder.product_recycle_list.setAdapter(mAdapter); 

    } 

@Override 
public int getItemCount() { 
    return productCategoryList.size(); 
    } 

public static class ViewHolder extends RecyclerView.ViewHolder{ 

public TextView product_category_name_id; 
public RecyclerView product_recycle_list; 
public ChildCustomLinearLayoutManager mLayoutManager; 
public Context context; 

public View view; 
public ClipData.Item currentItem; 
public ViewHolder(final View itemView,final Context context) { 
    super(itemView); 
    this.context = context; 
    product_category_name_id = (TextView)itemView.findViewById(R.id.product_category_name_id); 
    product_recycle_list =(RecyclerView)itemView.findViewById(R.id.product_recycle_list); 
    itemView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

     } 
    }); 
} 
} 
} 

父RecyclerView適配器的佈局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="bodhiinfo.abcpappa.activity.MarketFragment"> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <!-- linear layout for market heading row--> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/market_heading_raw" 
     android:layout_marginTop="25dp"> 
     <TextView 
      android:id="@+id/product_category_name_id" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/market_heading" 
      android:layout_weight="0.7" 
      android:layout_gravity="left" 
      android:gravity="left"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="View More.." 
      android:layout_weight="0.3" 
      android:layout_gravity="right" 
      android:gravity="right" 
      /> 
    </LinearLayout> 
    <android.support.v7.widget.RecyclerView 
     android:id="@+id/product_recycle_list" 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 
</RelativeLayout> 

孩子RecyclerView適配器

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

private List<ProductBean> productList; 
private Context context; 

MarketRowRecyclerAdapter(Context context,List<ProductBean> productList){ 

    this.productList =(ArrayList<ProductBean>)productList; 
    this.context = context; 

} 


@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.market_product_image_view, parent, false); 
    ViewHolder viewHolder = new ViewHolder(v,context); 
    return viewHolder; 
} 

@Override 
public void onBindViewHolder(ViewHolder viewHolder, int position) { 

    ProductBean productBean=(ProductBean)productList.get(position); 
    ImageCacheManager imageCacheManager= ImageCacheManager.INSTANCE; 
    ImageLoader imageLoader=imageCacheManager.getImageLoader(); 
    viewHolder.market_product_image_id.setImageUrl("http://www.ps4home.com/wp-content/uploads/2013/10/Mad-Catz-F.R.E.Q.5-Headset-for-PC-and-Mac-Black.jpg", imageLoader); 
    viewHolder.product_text_id.setText(productBean.getTxt_product_name()); 
    viewHolder.product_actual_price_id.setText("Rs :"+productBean.getTxt_product_actual_price()); 
    viewHolder.product_actual_price_id.setPaintFlags(viewHolder.product_actual_price_id.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
    viewHolder.product_app_price_id.setText("Rs :" + productBean.getTxt_product_price_in_app()); 

} 

@Override 
public int getItemCount() { 
    return productList.size(); 
} 

public static class ViewHolder extends RecyclerView.ViewHolder{ 

    public TextView product_text_id; 
    public TextView product_actual_price_id; 
    public TextView product_app_price_id; 
    public NetworkImageView market_product_image_id; 
    public Context context; 

    public View view; 
    public ViewHolder(final View itemView,final Context context) { 
     super(itemView); 
     this.context = context; 
     product_text_id = (TextView)itemView.findViewById(R.id.product_text_id); 
     product_actual_price_id= (TextView)itemView.findViewById(R.id.product_actual_price_id); 
     product_app_price_id= (TextView)itemView.findViewById(R.id.product_app_price_id); 
     market_product_image_id = (NetworkImageView)itemView.findViewById(R.id.market_product_image_id); 

     itemView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

      } 
     }); 
    } 
    } 
    } 

兒童適配佈局文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:orientation="vertical" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:paddingRight="10dp" 
android:paddingLeft="10dp" 
tools:context="bodhiinfo.abcpappa.activity.MarketFragment"> 
    <FrameLayout 
     android:id="@+id/market_product1_image_frame_id" 
     android:layout_width="150dp" 
     android:layout_height="150dp"> 
     <com.android.volley.toolbox.NetworkImageView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/market_product_image_id"/> 
     <TextView 
      android:id="@+id/product_text_id" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="@color/text_light" 
      android:layout_gravity="bottom" 
      android:text="@string/product_one" 
      android:background="@drawable/textbackgrounds"/> 
    </FrameLayout> 
    <TextView 
     android:id="@+id/product_actual_price_id" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@color/text_dark_black" 
     android:text="@string/product_one" 
     android:layout_below="@+id/market_product1_image_frame_id"/> 
    <TextView 
     android:id="@+id/product_app_price_id" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/product_one" 
     android:textColor="@color/text_dark_black" 
     android:layout_below="@+id/product_actual_price_id"/> 
</LinearLayout> 

ChildCustomLinearLayoutManager是通過pptang在上述問題提供的同一類。 其實我是android的新手,如果你曾經交叉過這樣的問題,讓我知道我犯的錯誤。謝謝你們。

回答

2

如果wrap_content和沒有明確的最小高度設置,RecyclerView根據其子視圖大小自己。 在你的孩子RecyclerView你必須考慮如下改變

<android.support.v7.widget.RecyclerView 
    android:id="@+id/product_recycle_list" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 

你使用橫向recyclerView所以設置

,如果你想了解更多有關此問題的此次訪問將工作https://code.google.com/p/android/issues/detail?id=74772 它會幫助你。

+0

謝謝asnad你救了我的一天。其作品 –