我实际上在我的应用程序中使用了一个带有两列的GridLayoutManager,我希望根据所使用的视图类型有一列.
这是我片段的方法"onCreateView()"中的代码:
// Recycler view for users
usersList = (RecyclerView) mView.findViewById(R.id.usersList);
// Layout manager (grid of users
layoutManager = new GridLayoutManager(mContext, 2);
usersList.setLayoutManager(layoutManager);
// ArrayList of users
users = new ArrayList<User>();
// Set the adapter for the list of users
mHomeListAdapter = new HomeListAdapter(getActivity(), users);
usersList.setAdapter(mHomeListAdapter);
Run Code Online (Sandbox Code Playgroud)
然后,在我的适配器中,我有这样的代码:
@Override
public int getItemViewType(int position) {
if (position == 5) {
return TYPE_PREMIUM;
} else {
return TYPE_ITEM;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_PREMIUM) { …Run Code Online (Sandbox Code Playgroud) 我的一个适配器出了问题.我有一系列图像,我在网格中显示如下:
当我有一个单一屏幕的物品并且它们都显示正常时,这很有效.当有更多项目向下滚动时,会出现问题.最初当我向下滚动时,项目显示为空白,GridLayout容器在那里,显示图像的代码运行,但它们不会出现在屏幕上,如下所示:
如果我向上滚动,然后再次向下滚动,图像会显示在它们应该的位置:
看起来我需要刷新布局或告诉渲染器某些东西已经发生变化,但我尝试的任何东西似乎都没有做任何事情.我试过打电话requestLayout,invalidate但它似乎没有解决问题.我还注册了一个GlobalOnLayoutListener,但这并没有更好.
这是相关代码:
这是我的onBindViewHolder方法
final GridLayout grid = ((KKAlbumViewHolder) holder).mGridLayout;
grid.removeAllViews();
int width = grid.getWidth();
if(width > 0) {
albumHolder.setPreviewImages(posts);
}else {
albumHolder.itemView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
albumHolder.itemView.removeOnLayoutChangeListener(this);
albumHolder.setPreviewImages(posts);
albumHolder.mGridLayout.invalidate();
}
});
}
Run Code Online (Sandbox Code Playgroud)
这是我用来布局图像的方法.它基本上将图像放在不规则的网格中,因此如果有长图像或高图像,该图像将尝试占据整行或整列,具体取决于空闲空间.我设置参数宽度和高度的原因是毕加索不抱怨该.fit()方法:
public void setPreviewImages(ArrayList<KKPost> posts) {
Picasso picasso = Picasso.with(itemView.getContext());
int space = 0;
int …Run Code Online (Sandbox Code Playgroud) android android-gridlayout gridlayoutmanager android-recyclerview
我正在使用GridLayout(支持)ImageView在我的应用程序中显示s.有3列5行.问题是细胞在GridLayout它们之间自动获得一些空间.我没有为细胞设置任何填充或边距.请参考下图.动态添加所有单元格,以下是添加这些单元格的方法.
获得屏幕宽度和高度:
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
screenWidth = size.x;
screenHeight = size.y;
rowHeight = (int) (screenHeight * 0.2);
Run Code Online (Sandbox Code Playgroud)
将视图添加到GridLayout:
GridLayout.LayoutParams params = new GridLayout.LayoutParams(
getSpec(rowColumn[0]), getSpec(rowColumn[1]));
params.height = rowHeight;
if (rowColumn[1].equalsIgnoreCase("col_full")) {
params.width = (int) (screenWidth);
} else if (rowColumn[1].equalsIgnoreCase("col_two_part")) {
params.width = (int) (screenWidth * 0.6);
} else {
params.width = (int) (screenWidth * 0.3);
}
ImageButton image = (ImageButton) imageHashMap
.get(reOrderedButtonsListCopy.get(i));
image.setLayoutParams(params);
gridLayout.addView(image, params);
Run Code Online (Sandbox Code Playgroud)
XML布局:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.xx.xxx"
android:id="@+id/scrollView1"
android:layout_width="wrap_content" …Run Code Online (Sandbox Code Playgroud) 我Recyclerview这是在Coordinatorlayout> NestedScrollview>ViewPager并ViewPager有3个片段,一个具有正与帮助图片库Recyclerview。每当我尝试向上或向下滚动时,它根本不滚动。我已经设置好了photosRecycler.setNestedScrollingEnabled(false);,一旦我删除这条线,我就会让滚动只向下工作,当我试图向上移动时,它会随着父级向上移动Nestedscrollview。
我的布局管理器也将 StaggeredGrid 显示为 Grid 我需要获得这种布局
这是我的 Nestedscrollview 父布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="?android:attr/colorBackground"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:colorBackground">
<ImageView
android:layout_width="match_parent"
android:layout_height="500dp"
android:src="@color/custom_transparent_colorBlack"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="128dp"
android:layout_height="128dp"
app:layout_constraintBottom_toBottomOf="@+id/coverPhoto"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/coverPhoto"
app:layout_constraintVertical_bias="0.44"
tools:srcCompat="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/up_NestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
app:layout_behavior="@string/appbar_scrolling_view_behavior" …Run Code Online (Sandbox Code Playgroud) java android android-scrollview android-gridlayout android-recyclerview
我正在使用GridLayoutManager(GLM)我的RecyclerView.在RecyclerView,我将填充CardViews,如何设置我在整个屏幕中GLM只渲染一个CardView?
如果我将跨度大小设置为1,则CardView每行渲染一个,而不是整个屏幕.
在Simple中,我想显示1 CardView占用两列和两行.怎么做到这一点?
我想在网格中的项目之间创建一个空格.这些物品都应具有相同的宽度/高度.
我尝试过的:
创建一个GridLayoutOffsetDecorator将偏移量应用于所有网格项:
class GridLayoutOffsetDecorator(var offset: Int) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State?) {
super.getItemOffsets(outRect, view, parent, state)
outRect.set(offset, offset, offset, offset)
}
}
Run Code Online (Sandbox Code Playgroud)
具有偏移量8dp会16dp在项目之间创建空间.所以我们仍然需要8dp在外边缘应用填充:
<android.support.v7.widget.RecyclerView
android:id="@+id/productList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="8dp" />
Run Code Online (Sandbox Code Playgroud)
结果是:
问题:项目大小不相同:
当您看到蓝线时,您会注意到高度略有不同.仅在填充Recyclerview之后才会出现此差异.这个填充似乎稍微调整了一些项目的大小.你们有这方面的经验吗?知道如何解决这个问题吗?
通过从项目中移除图像,高度差消失.这是图像的设置方式:
<SquareImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:imageResource="@{product.image}" />
Run Code Online (Sandbox Code Playgroud)
SquareImageView:
class SquareImageView : ImageView {
constructor(context: Context) : super(context)
constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)
constructor(context: Context, attributeSet: …Run Code Online (Sandbox Code Playgroud) android android-gridlayout gridlayoutmanager android-recyclerview
我想制作一个custom grid我们通常在Android手机中看到的图像

我一直在搜索它几个小时.但没有运气好处,最后我试图在这里提问.有人可以建议我如何实现这种gridview.或者我是否需要遵循任何其他方法?
我正在尝试为我的应用程序中的某些项目创建一个表格/网格,我希望在每个单元格周围有一个边框来分割项目,并将设置与项目保持一致.这个应用程序将用于工业环境中,可能有不熟悉Android的人需要使用它,因此试图让它尽可能简单.
表/格将包含TextView,EditText,Spinner,和Button,并且也将滚动(经由ScrollView亲本).
我读到GridView并发现它(似乎)只能以编程方式获取项目,如果我错了请纠正我.我觉得这是不必要的,因为我知道我想要什么,在哪里.此外,我还没有尝试以编程方式添加项目到布局,所以我想我会先尝试其他选项.此外,如果自动显示边框线,或者您可以完全显示边框线,则GridView文档不会说明这种方式.
我开始使用a TableLayout并且能够获得除边界线之外的所有内容.我试图android:divider获得线条,但这不起作用.我曾经想过要创建一堆TextView带有黑色背景和~2dp宽度/高度的s来制作我自己的边框线.这感觉就像是一个巨大的浪费.然后我还阅读了TableLayout文档并发现:"TableLayout容器不显示其行,列或单元格的边框线."
然后我尝试了,GridLayout并得到了相同的结果TableLayout.我试过padding和margins,既不工作.此外,GridLayout文档指出:"网格由一组无限细线组成,将观察区域分隔成单元格."
我的问题是:
是否存在我错过的TableLayout或者GridLayout通过xml给我边界线的声明?
如果没有,那么GridView我会给我想要的台词吗?
我可以添加我想要的所有以前提到的项目GridView吗?
android android-gridview android-tablelayout android-gridlayout
使用GridLayout,这是一个有效的布局定义.关于'layout_height' attribute should be defined或没有任何警告
'layout_width' attribute should be defined
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView />
</GridLayout>
Run Code Online (Sandbox Code Playgroud)
另一方面,如果我扩展GridLayout,等效布局会同时给出警告'layout_height' attribute should be defined和'layout_width' attribute should be defined
<ExtendedGridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView />
</ExtendedGridLayout>
Run Code Online (Sandbox Code Playgroud)
这就是扩展的gridlayout的样子
package com.github.extendedgridlayout;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.GridLayout;
@SuppressWarnings("unused")
public class ExtendedGridLayout extends GridLayout {
public ExtendedGridLayout(Context context){
super(context);
}
public ExtendedGridLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExtendedGridLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, …Run Code Online (Sandbox Code Playgroud)