如何在网格视图中显示线性布局?

时间:2014-09-15 08:55:44

标签: java android xml android-layout

这是代码。

<?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;
    }


}

我想在一些单元格中添加灰色线性布局。但它没有出现。我该怎么做?我的推荐似乎没错,但仍然没有出现灰色的布局。

1 个答案:

答案 0 :(得分:1)

原因是ListView的项目将被重用。

在此代码之后:

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

你应该添加else子句:

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

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

相关问题