user2328381
user2328381

Reputation: 23

LinearLayout and Visibility GONE issue

Alright, so I'm running into a rather odd problem. I think I'm doing this right, but something weird happens when I try to set the visibility to GONE on any of my linear layouts.

Basically, what I'm trying to do is use a spinner onItemSelected listener to determine which layouts are visible. It's basically a table row, spinner, and three linearlayouts inside of one linearlayout inside of a scrollview inside of a RelativeLayout. Here's the first xml of part of it, just not showing the other 2 linearlayouts:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".Catbug" >

<ScrollView
    android:id="@+id/scrollview1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:ignore="UselessParent" >

    <LinearLayout
        android:id="@+id/home_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:orientation="vertical" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center|center_horizontal|center_vertical" >

            <TextView
                android:id="@+id/textView0"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="left|center_vertical|fill_vertical|center_horizontal|center"
                android:layout_weight="1"
                android:text="@string/catbug_quotes"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textSize="36sp" />

            <ImageButton
                android:id="@+id/catbugback"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                android:src="@drawable/backbuttonpress" android:contentDescription="@string/goback"/>

        </TableRow>

        <Spinner
            android:id="@+id/catbugspinner"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
             />

        <LinearLayout
            android:id="@+id/catbug7_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

Now, here's the code side of the part in question:

    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    final   LinearLayout ep7 = (LinearLayout) findViewById(R.id.catbug7_layout);
    final   LinearLayout ep10 = (LinearLayout) findViewById(R.id.catbug10_layout);
    final   LinearLayout ep11 = (LinearLayout) findViewById(R.id.catbug11_layout);
    int position = spinner.getSelectedItemPosition();
    switch(position){
    case 0:
        ep7.setVisibility(View.VISIBLE);
        ep10.setVisibility(View.VISIBLE);
        ep11.setVisibility(View.VISIBLE);
        break;
    case 1:
        ep7.setVisibility(View.VISIBLE);
        ep10.setVisibility(View.GONE);
        ep11.setVisibility(View.GONE);
        break;
    case 2:
        ep7.setVisibility(View.GONE);
        ep10.setVisibility(View.VISIBLE);
        ep11.setVisibility(View.GONE);
        break;
    case 3:
        ep7.setVisibility(View.GONE);
        ep10.setVisibility(View.GONE);
        ep11.setVisibility(View.VISIBLE);
        break;
    }
}

In general, it works. The layouts I don't want for certain spinner choices disappear after they're selected and come to the top of the screen. However, they don't actually disappear until I start to scroll, which presents a problem as the first linearlayout doesn't NEED to scroll as only like 5 buttons are visible. However, an inactive (it's been destroyed, just still visible) linearlayout2 suddenly dissapears if I swipe my finger like I were intending to actually scroll.

Any ideas on what I'm doing wrong? It's bugging the hell out of me because it actually works, it's just the aesthetic of having that weird "ghosting".

Upvotes: 2

Views: 3131

Answers (1)

Sam
Sam

Reputation: 3413

It seems your onItemSelected is only getting call when you clear focus from your spinner to any other space. Try to log and check when it is being called - when you select spinner or when you touch the screen. If it's the latter, you might want to revisit your code.

Alternatively your layout is not getting invalidated. If the onItemSelected is getting called properly, try invalidating the layout after you have hidden the views.

Upvotes: 1

Related Questions