-->

Gallery space at beginning and end

2020-08-26 10:49发布

问题:

I got the following problem , i made a form with a gallery, the gallery instead of containing images contains items from one of my classes, everything inside each item of the gallery displays perfectly. I removed the space between images using:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
        <Gallery android:id="@+id/galleryid"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:spacing="0dip"
                android:padding="0dip"
                android:layout_weight="1" />

the items of the gallery:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="75dip" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" android:background="#ffffff"> 

    <TextView android:id="@+id/frame_number" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="-" android:textSize="12dip" android:textColor="#ffffff" android:background="#000000" android:gravity="center" />
    <LinearLayout android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" android:orientation="horizontal" android:gravity="center_horizontal">
                <TextView  android:id="@+id/frame_shot1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:textSize="18dip" android:textColor="#000000" />
                <TextView  android:id="@+id/frame_shot2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:textSize="18dip" android:textColor="#000000" />
    </LinearLayout> 
    <TextView android:id="@+id/frame_total" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="---" android:textSize="38dip" android:textColor="#000000"/>
</LinearLayout>

buuuuuuuuuuuuuuuuut , i got a problem, there is some blank space at the beginning and end of the gallery with no items. the thing is that my gallery has many items on it that u can actually scroll horizontally, but i wanna get rid of those spaces so the very first thing on the very left of the gallery is the first item, and the very last when u scroll to the very right is the right item.


Edit 08/16 Still with the same problem back in the project, here i leave an image of exactly what is what I'm trying to get rid of that is the black space at the beginning (also is at the end of the gallery at the other side)

回答1:

The reason you get the blank space on the left is simply because there are no more items to display to the left of the first item (centred in the view). I would assume that the same problem arises to the right of the last item as well.

To properly solve this one will have to decide on what to display when there are no more items, but I will give some ideas:

(1) Increasing the width of the gallery items to the same width as the gallery will only show one item at a time, thus the user will never be able to scroll to the empty space.

(2) To display a solid color or an image in the blank space I would assume setting the android:background attribute of the gallery.

(3) Create a buffer in the beginning and end of your adapter, consisting of the last/first items respectively, and when the user scrolls far enough in the buffer, jump to the matching item at the other end of the gallery for a infinite "carousel" gallery.



回答2:

Try:

int position=1;

void android.widget.AbsSpinner.setSelection(int position)


回答3:

One thing you can try is to check if there's more than one item. If there is, set the selection on the gallery to be the second item.



回答4:

Your probably talking about the fading edge length.

http://developer.android.com/reference/android/view/View.html#setFadingEdgeLength(int)

Try

setFadingEdgeLength(0);

Or

 setHorizontalFadingEdgeEnabled(false);

Try changing your layout for the gallery width to fill parent as well instead of wrap content:

   <Gallery android:id="@+id/galleryid"
            android:layout_width="fill_parent" 


回答5:

set the margin of the gallery to -(metrics.widthPixels)+itemWidth, the blank space is hide.

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(-(metrics.widthPixels)+itemWidth,0,0,0); 
gallery.setLayoutParams(params);


回答6:

I've just spent with this issue 2 hours and finally found a solution for myself. I have a Gallery, with custom LinearLayout elements. I want to display just 1 element a time without any padding on the start nor on the end.

After many tries I found that, if I set the minWidth parameter of every Item to a big number (android:minWidth="1000dp") my problem is solved. Don't know if it is suitable for your issue. Good Luck

Peter B.