我正在使用Recyclerview,其层为StaggeredGridLayoutManager。问题是在大多数手机中会显示项目,但在荣耀 6x 手机中不会显示项目但可以单击它们。
我将项目 rotationY:180 设置为从右到左显示。
我什至使用了GridLayoutManager但它仍然有同样的问题。
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);
}