gpt4 book ai didi

java - 如何使线性布局出现在 GridView 中?

转载 作者:行者123 更新时间:2023-12-01 12:32:18 25 4
gpt4 key购买 nike

这是代码。

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

<LinearLayout
android:id="@+id/grid_cell_color"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80A9A9A9"
android:orientation="vertical" >
</LinearLayout>

<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/grid_cell_border" />

<RelativeLayout
android:id="@+id/grid_cell_background"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</RelativeLayout>

</FrameLayout>

适配器代码是:

    @Override
public int getCount() {
return titles.length;
}

@Override
public Object getItem(int position) {
return titles[position];
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View row, ViewGroup parent) {

if (inflater == null)
inflater = (LayoutInflater) view.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);

row = inflater.inflate(R.layout.grid_view_single_cell, parent, false);

LinearLayout gridCell1= (LinearLayout) row.findViewById(R.id.grid_cell_color);

if(position == 0 || position == 1 || position == 5){
gridCell1.setVisibility(View.GONE);
Log.i("GRID", "ENTERED");
}

RelativeLayout gridCell = (RelativeLayout) row.findViewById(R.id.grid_cell_background);

gridCell.setBackgroundResource(titles[position]);
return row;
}


}

我想在某些单元格中放置灰色线性布局。但它没有出现。我该怎么做?我的引用似乎是正确的,但灰色布局仍然没有显示。

最佳答案

原因是ListView的item会被重用。

在此代码之后:

if(position == 0 || position == 1 || position == 5){
gridCell1.setVisibility(View.GONE);
Log.i("GRID", "ENTERED");
}

您应该添加 else 子句:

else{
gridCell1.setVisibility(View.VISIBLE);
}

可能还有另一个原因:将Linearlayout的高度更改为1dp。

关于java - 如何使线性布局出现在 GridView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25844204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com