0

我正在使用Recyclerview,其层为StaggeredGridLayoutManager。问题是在大多数手机中会显示项目,但在荣耀 6x 手机中不会显示项目但可以单击它们。

我将项目 rotationY:180 设置为从右到左显示。

我什至使用了GridLayoutManager但它仍然有同样的问题。

所有手机上的屏幕截图 在此处输入图像描述

在荣耀 6x 手机上 在此处输入图像描述

Recyclerview 小部件

    public class AutoFitGridRecyclerView extends RecyclerView {
    private StaggeredGridLayoutManager manager;
    private int columnWidth = -1;

    public AutoFitGridRecyclerView(Context context) {
        super(context);
        init(context, null);
    }

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

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

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            // list the attributes we want to fetch
            // columnWidth is what GridView uses, so we use it too
            int[] attrsArray = {
                    android.R.attr.columnWidth
            };

            TypedArray array = context.obtainStyledAttributes(attrs, attrsArray);

            //retrieve the value of the 0 index, which is columnWidth
            columnWidth = array.getDimensionPixelSize(0, -1);
            array.recycle();
        }

        manager = new StaggeredGridLayoutManager(AppController.NumberColumnAutoFitForRecyclerViewForGridList(120), 1);
        setLayoutManager(manager);
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        super.onMeasure(widthSpec, heightSpec);

        if (columnWidth > 0) {
            //The spanCount will always be at least 1
            int spanCount = Math.max(1, getMeasuredWidth() / columnWidth);
            manager.setSpanCount(spanCount);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }
}

在活动布局中

<com.mrsanad.Application.Widgets.AutoFitGridRecyclerView
            android:id="@+id/gridMenu"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="10dp"
            android:rotationY="180" />

在项目布局中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bRow"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_marginTop="5dp"
    android:background="@android:color/transparent"
    android:clickable="true"
    android:focusable="true"
    android:gravity="center"
    android:orientation="vertical"
    android:rotationY="180">

    <com.balysv.materialripple.MaterialRippleLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:focusable="true"
        app:mrl_rippleAlpha="0.2"
        app:mrl_rippleColor="@color/colorPrimary"
        app:mrl_rippleDuration="350"
        app:mrl_rippleOverlay="true"
        app:mrl_rippleRoundedCorners="100dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@+id/layoutTitle">

                <ImageView
                    android:id="@+id/imgIcon"
                    android:layout_width="65dp"
                    android:layout_height="65dp"
                    android:layout_centerInParent="true"
                    android:padding="12dp" />

                <com.mrsanad.Application.Widgets.BadgeTextView
                    android:id="@+id/tvCount"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_centerInParent="true"
                    android:gravity="center"
                    android:padding="0.5dp"
                    android:textColor="@color/White"
                    android:textSize="@dimen/SmallTextSize" />

            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/layoutTitle"
                android:layout_width="match_parent"
                android:layout_height="39dp"
                android:layout_alignParentBottom="true">

                <com.mrsanad.Application.Widgets.PersianTextViewBold
                    android:id="@+id/tvTitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_marginLeft="2dp"
                    android:layout_marginRight="2dp"
                    android:gravity="center"
                    android:maxLines="2"
                    android:textColor="@color/Gray"
                    android:textSize="@dimen/SmallTextSize" />

            </RelativeLayout>

        </RelativeLayout>

    </com.balysv.materialripple.MaterialRippleLayout>
</LinearLayout>

在适配器 java

package Adapters.ActMain;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mrsanad.Application.Utility.Variable;
import com.mrsanad.Network.Models.Response.Home.GetHome.Menu;
import com.mrsanad.R;
import com.mrsanad.Application.Utility.AppController;
import com.mrsanad.Application.Widgets.BadgeTextView;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

public class AdpMenu extends RecyclerView.Adapter<AdpMenu.ViewMenu> {

    private List<Menu> Items = new ArrayList<>();

    public AdpMenu() {
    }

    public void AddItems(List<Menu> items) {
        Items.addAll(items);
        notifyDataSetChanged();
    }

    public Menu GetItem(int pos) {
        return Items.get(pos);
    }

    @NonNull
    @Override
    public ViewMenu onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(AppController.Context)
                .inflate(R.layout.item_home_menu, parent, false);
        return new ViewMenu(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewMenu holder, int position) {
        Menu item = GetItem(position);

        holder.tvTitle.setText(item.NameFa);
        AppController.SetPicUrl(holder.imgIcon, item.ImagePath, 0, "");
        holder.tvCount.setVisibility(View.GONE);
    }

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

    public void Clear() {
        Items.clear();
        notifyDataSetChanged();
    }

    public class ViewMenu extends RecyclerView.ViewHolder {

        @BindView(R.id.imgIcon)
        ImageView imgIcon;
        @BindView(R.id.tvTitle)
        TextView tvTitle;
        @BindView(R.id.layoutTitle)
        RelativeLayout layoutTitle;
        @BindView(R.id.tvCount)
        BadgeTextView tvCount;

        public ViewMenu(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            tvCount.setStrokeWidth(1);
            tvCount.setStrokeColor(AppController.Context.getString(R.color.White));
            tvCount.setSolidColor(AppController.Context.getString(R.color.Orange));
        }
    }
}

在 Java 活动中

@BindView(R.id.gridMenu)
    RecyclerView gridMenu;

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_main);

        AdpMenu AdpMenu = new AdpMenu();
    gridMenu.setAdapter(AdpMenu);

       AdpMenu.AddItems(\\All items list);

    }
4

2 回答 2

0

你应该切换到ConstraintLayout.

ConstraintLayout 允许您创建具有平面视图层次结构(无嵌套视图组)的大型复杂布局。它与 RelativeLayout 类似,所有视图都根据兄弟视图和父布局之间的关系进行布局,但它比 RelativeLayout 更灵活,更易于与 Android Studio 的布局编辑器一起使用。

您可以使用SDP代替dp。此尺寸单位随屏幕尺寸缩放。它可以帮助Android开发者支持多屏。

dependencies {
  implementation 'com.intuit.sdp:sdp-android:1.0.6'
}

你的价值将是

    android:layout_width="@dimen/_100sdp"
    android:layout_height="@dimen/_100sdp"
    android:layout_marginTop="@dimen/_5sdp"
    

供参考

你可以使用Glide。它是由bumptech开发的Android Image Loader Library,是谷歌推荐的库。

Glide 是一个快速高效的 Android 开源媒体管理和图像加载框架,它将媒体解码、内存和磁盘缓存以及资源池封装到一个简单易用的界面中。

RequestManager requestManager = Glide.with(contextObj);
RequestBuilder requestBuilder = requestManager.load(item.ImagePath);
requestBuilder.into(holder.imgIcon);

摇篮

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.11.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
}
于 2020-11-05T05:45:56.513 回答
0

我假设您正在从 drawable 加载图像资源。在这种情况下,您应该检查您的荣耀 6x 当前运行的是哪个版本?并检查您正在加载的资源是否也在适当的屏幕密度大小文件夹中。

于 2020-11-11T11:31:03.797 回答