简体   繁体   English

如何使线性布局出现在gridview中?

[英]How do I make a linear layout appear in a gridview?

Here is the code. 这是代码。

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

The adapter code is: 适配器代码为:

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


}

I want to put a gray colored linear layout in some of the cells. 我想在某些单元格中放置一个灰色的线性布局。 But it doesn't appear. 但是它没有出现。 How do I do it? 我该怎么做? My references seems right but still the gray colored layout doesn't show up. 我的参考文献似乎正确,但是灰色布局仍然没有显示。

The reason is the item of a ListView will be reused. 原因是ListView的项目将被重用。

After this code: 此代码后:

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

You should add the else clause: 您应该添加else子句:

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

There may be another reason: change the height of the Linearlayout to 1dp. 可能还有另一个原因:将Linearlayout的高度更改为1dp。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM