android - RecyclerView中的项目变得越来越小

时间:2017-07-11 15:09:25

标签: android

我正在使用带有StaggeredGridLayoutManager和tabLayout的RecyclerView 我使用tabLayout刷新RecyclerView,但问题是什么时候 刷新后,RecyclerView中的项目越来越小 拖着照片清除了情况 第一张照片:

enter image description here

第二张照片:

enter image description here

       gaggeredGridLayoutManager = new StaggeredGridLayoutManager(3, 1);
        mAdapter= new RecycleRowItemAdapter(list2);
        mAdapter.setContext(getContext());
        recyclermat.setLayoutManager(gaggeredGridLayoutManager);
        recyclermat.setItemAnimator(new DefaultItemAnimator());
        int spacingInPixels =   getResources().getDimensionPixelSize(R.dimen.photos_list_spacing);
        recyclermat.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
        recyclermat.setAdapter(mAdapter);

这是我的布局:

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


    <RelativeLayout
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"

        android:background="@color/gold">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="4">

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/button"
                android:text="Filter"
                android:textColor="@android:color/white"
                android:textSize="12sp" />

            <AutoCompleteTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:gravity="center"
                android:text="Enter Search Word"
                android:textColor="@android:color/white"
                android:textSize="12sp" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/button"
                android:text="Invite"
                android:textColor="@android:color/white"
                android:textSize="12sp" />

        </LinearLayout>


    </RelativeLayout>


    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:tabBackground="@android:color/black"
        android:background="@android:color/white"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/gold"
        app:tabTextColor="@android:color/white" />



    <android.support.v7.widget.RecyclerView
        android:id="@+id/mainrecycle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:layout_below="@+id/toolbar">

    </android.support.v7.widget.RecyclerView>



</LinearLayout>

行项目:              

    <LinearLayout
        android:id="@+id/mainBord"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/row_item_border"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:adjustViewBounds="true"
            android:src="@drawable/icon"
            />


        <LinearLayout
            android:id="@+id/buttonsLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:paddingBottom="2dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/priceedt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="5dp"
                android:gravity="center"
                android:layout_weight="1"
                android:textSize="15dp"
                android:text="6$"
                android:textStyle="bold"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_weight="1">
            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:src="@drawable/blank_heart"
                android:id="@+id/like"
                android:adjustViewBounds="true"
                android:layout_weight="1"/>
        </LinearLayout>

        </LinearLayout>

    </LinearLayout>
</android.support.v7.widget.CardView>

任何想法?提前谢谢

2 个答案:

答案 0 :(得分:2)

问题出在项目装饰者身上。

recyclermat.addItemDecoration(new SpacesItemDecoration(spacingInPixels));

仅在初始化Recyclerview时首次设置。

答案 1 :(得分:0)

我认为更新recyclerview时存在问题。您应该初始化一次recyclerviw并仅使用适配器更新列表项。以下是一些参考代码。

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.grievance_list, container, false);
    unbinder = ButterKnife.bind(this, view);

    initRecyclerView();

    //update adapter whenever new data is inserted.
    mmAdapter.updateItems(yourDataList);

    return view;
}

private void initRecyclerView() {
    gaggeredGridLayoutManager = new StaggeredGridLayoutManager(3, 1);
    mAdapter= new RecycleRowItemAdapter(new ArrayList<YourModelClass>);
    mAdapter.setContext(getContext());
    recyclermat.setLayoutManager(gaggeredGridLayoutManager);
    recyclermat.setItemAnimator(new DefaultItemAnimator());
    int spacingInPixels =   getResources().getDimensionPixelSize(R.dimen.photos_list_spacing);
    recyclermat.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
    recyclermat.setAdapter(mAdapter);
}

内部适配器类创建更新回收器视图项的功能

public void updateItems(final List<YourModelClass> newItems) {
    list.clear();
    list.addAll(newItems);
    notifyDataSetChanged();
}