使用嵌套gridView的RecyclewView调整高度

时间:2017-06-06 09:59:14

标签: android gridview android-recyclerview

我正在尝试创建此视图:

enter image description here

Rerecyclew每件商品Gridview。 因此,当您按加号项时,我会向Gridview添加新项目并调整RecyclewView项目高度。

但是我无法用wrap_content来表达,我不知道为什么。

活动布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_scenes"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

</LinearLayout>

RecyclewView项目

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <GridView
        android:id="@+id/gv_scene_items"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3"
        android:gravity="center"
        android:columnWidth="50dp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#eaeaea" />

</LinearLayout>

GridView项目(仅举例)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="@color/liloColorBlue">



</LinearLayout>

你能帮我看一下示例视图吗?

谢谢。

3 个答案:

答案 0 :(得分:0)

你可以很容易地做到这一点。 将xml中的recyclerView高度更改为:

  public class Fragment : SupportFragment

        {
            public override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                // Create your fragment here
            }


            public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                RecyclerView recyclerView = inflater.Inflate(Resource.Layout.Fragment, container, false) as RecyclerView;

                SetUpRecyclerView(recyclerView);

                return recyclerView;
            }

            private void SetUpRecyclerView(RecyclerView recyclerView)
            {
                recyclerView.SetLayoutManager(new LinearLayoutManager(recyclerView.Context));

                var Adapter = new EventAdapter(Collection.GetData());

                recyclerView.SetAdapter(Adapter);



            }

            public class Adapter : RecyclerView.Adapter
            {

                private JavaList<MyData> AllData;

                public EventAdapter(JavaList<MyData> AllData)
                {
                    this.AllData = AllData;

                }


                public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
                {
                    MyHolder hold = holder as MyHolder;
                    hold.Name.Text = AllData[position].Name;



   }


                public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
                {
                    View v = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.ListItem, parent, false);
                    MyHolder holder = new MyHolder(v);

                    return holder;
                }

                public override int ItemCount
                {

                    get { return AllData.Size(); }

                }

            }
        }




    public class MyHolder : RecyclerView.ViewHolder
    {

        public TextView Name;


        public EventHolder(View itemView) : base(itemView)
        {
            Name = itemView.FindViewById<TextView>(Resource.Id.textView1);




        }


    }

这将自动调整布局。

希望这会有所帮助。

答案 1 :(得分:0)

从MAIN THREAD调用notifyDataSetChanged()的{​​{1}}或notifyItemChanged(int position)方法。

我希望这可以帮到你

答案 2 :(得分:0)

我发现解决方案创建了我自己的gridview,如下所示:

public class ScenesGridView  extends GridView {

        public ScenesGridView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public ScenesGridView(Context context) {
            super(context);
        }

        public ScenesGridView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

        @Override
        public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                    MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);
        }


}