RecyclerView项目匹配父项

时间:2016-02-15 11:27:18

标签: android android-recyclerview

我在linearLayout中有多个Recyclerview,我只需显示一行而不是所有行。 我试过匹配父母,但它不起作用,所以任何人都可以发牢骚。

在这个例子中,我有3个以编程方式创建的recycleView。

enter image description here

这是我的卡片视图

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_launcher" />
        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/thumbnail"
            android:maxLines="3"
            android:padding="8dp"
            android:text="test"
            android:textColor="#222"
            android:textSize="15dp" />
    </LinearLayout>
</android.support.v7.widget.CardView>

Formule.xml

我的Formule.xml

<FrameLayout
    android:id="@+id/mainmenu"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"

    android:foreground="@drawable/foreground_shape" >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/top"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@color/transparentWhite"
    >
    <TextView
    android:id="@+id/textViewCategoryParent"
    android:layout_width="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:layout_height="wrap_content" />          
</LinearLayout>

<LinearLayout
    android:id="@+id/middle"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
  />
<LinearLayout
    android:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="1"
    android:background="@color/transparentWhite"
    >
    <LinearLayout
        android:id="@+id/CategoriesGallery"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
  </LinearLayout>
   </LinearLayout>

    </FrameLayout>

Java代码

public void fillFormules(List<Category> objectsTextToShow)
{
    MyRecyclerAdapter adapter;
    final LinearLayout layoutItemDetail = (LinearLayout) view.findViewById(R.id.middle);
    LinearLayout relativeLayout = (LinearLayout) view.findViewById(R.id.titles);


    // Initialize recycler view
    for (int j = 0; j <objectsTextToShow.size() ; j++) {
        LinearLayout parentLayout = new LinearLayout(getActivity());
        parentLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, (float) 1.0));
        parentLayout.setOrientation(LinearLayout.HORIZONTAL);
        RecyclerView mRecyclerView = new RecyclerView(getActivity()); //(RecyclerView) view.findViewById(R.id.recycler_view);
        List<Item> items = new ArrayList<Item>();
        TextView textCat = new TextView(getActivity());
        textCat.setText(Check.Languages(objectsTextToShow.get(j).getName(), LANGUAGE_ID));
        textCat.setTextSize(28);
        textCat.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f));
        textCat.setTextColor(colorUtils.TITLE);
        textCat.setGravity(Gravity.CENTER);

        relativeLayout.addView(textCat);
        textCat.setBackgroundColor(colorUtils.backgroundColor);

        for (int i = 0; i <objectsTextToShow.get(j).getItems().size() ; i++) {
            items.add(objectsTextToShow.get(j).getItems().get(i));
        }
        final LinearLayoutManager manager = new LinearLayoutManager(getActivity());

        adapter = new MyRecyclerAdapter(getActivity(), items);
        mRecyclerView.setAdapter(adapter);
        parentLayout.addView(mRecyclerView);
        layoutItemDetail.addView(parentLayout);

        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                final int Height = layoutItemDetail.getHeight();
                if(dy > 0){
                    Log.e("RecyclerView scrolled: ", "scroll up!"+Height);
                //  recyclerView.scrollBy(0, Height);
                }
                else{
                //  recyclerView.scrollBy(0, -Height);
                    }

                Log.e("RecyclerView scrolled: ", "scroll down!"+-Height);}

        });

    }

}

1 个答案:

答案 0 :(得分:0)

如果您只想在recyclerView中首先显示一个项目。然后在适配器的onCreateViewHolder中添加以下代码。这会使recyclerView将项目显示为MATCH_PARENT高度

 @Override
    public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.list_item, parent, false);
        ListViewHolder holder = new ListViewHolder(view);
//code to MATCH_PARENT item height

    if (view.getLayoutParams ().height == RecyclerView.LayoutParams.MATCH_PARENT)
        view.getLayoutParams ().height = parent.getHeight();


        return holder;
    }
相关问题