Estoy tratando de mostrar la imagen en una fila horizontal. La imagen aparece pero solo se muestran tres de la imagen en lugar de las cinco imágenes. Debajo está mi fragmento.

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:orientation="horizontal"
            android:paddingLeft="32dp"
            android:paddingRight="32dp">

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="2dp"
                android:src="@drawable/star" />

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="2dp"
                android:src="@drawable/star" />
            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="2dp"
                android:src="@drawable/star" />
            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="2dp"
                android:src="@drawable/star" />
            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="2dp"
                android:src="@drawable/star" />


        </LinearLayout>

Por favor, cómo puede hacer que las imágenes se muestren horizontalmente y si el ancho está lleno, deje que fluya a la siguiente fila

-1
net 28 feb. 2018 a las 12:05

6 respuestas

La mejor respuesta

Use un HorizontalScrollView,

  <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:orientation="horizontal"
            android:paddingLeft="32dp"
            android:paddingRight="32dp">

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="2dp"
                android:src="@drawable/star" />

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="2dp"
                android:src="@drawable/star" />
            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="2dp"
                android:src="@drawable/star" />
            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="2dp"
                android:src="@drawable/star" />
            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="2dp"
                android:src="@drawable/star" />


        </LinearLayout>
      </HorizontalScrollView>
0
Bhavya Gandhi 28 feb. 2018 a las 09:11

Deberías usar recyclerView porque no quieres dibujar todos los widgets de imageView en la memoria ... pero para tu fragmento ... puedes usar esto ...

 <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:orientation="horizontal"
                android:paddingLeft="32dp"
                android:paddingRight="32dp">

                <ImageView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="100dp"
                    android:layout_margin="2dp"
                    android:src="@drawable/star" />

                <ImageView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="100dp"
                    android:layout_margin="2dp"
                    android:src="@drawable/star" />
                <ImageView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="100dp"
                    android:layout_margin="2dp"
                    android:src="@drawable/star" />
                <ImageView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="100dp"
                    android:layout_margin="2dp"
                    android:src="@drawable/star" />
                <ImageView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="100dp"
                    android:layout_margin="2dp"
                    android:src="@drawable/star" />

        </LinearLayout>
0
Vishva Dave 28 feb. 2018 a las 09:11
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    android:orientation="horizontal"
    android:paddingLeft="32dp"
    android:paddingRight="32dp">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="2dp"
            android:src="@drawable/star" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="2dp"
            android:src="@drawable/star" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="2dp"
            android:src="@drawable/star" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="2dp"
            android:src="@drawable/star" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="2dp"
            android:src="@drawable/star" />

</LinearLayout>
</HorizontalScrollView>

Espero que esto funcione y avíseme si funciona o no. Por favor dé un voto positivo si está trabajando.

0
Chetan Kumar Patel 28 feb. 2018 a las 09:39

Puede usar HorizontalScrollView como a continuación

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

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <LinearLayout
            android:id="@+id/mygallery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="2dp"
                android:src="@drawable/tiles" />

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="2dp"
                android:src="@drawable/ic_noti" />
            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="2dp"
                android:src="@drawable/tiles" />
            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="2dp"
                android:src="@drawable/tiles" />
            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="2dp"
                android:src="@drawable/tiles" />
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

Aquí salida

enter image description here

0
vikas singh 28 feb. 2018 a las 09:27

Use Recyclerview y configure LayoutManager como se muestra a continuación

mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));

Le dará desplazamiento horizontal y adaptador con visor para cargar imágenes.

0
akshay 28 feb. 2018 a las 09:16
<android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerViewFrame"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:gravity="center" />


private RecyclerView recyclerViewFrame;
recyclerViewFrame = findViewById(R.id.recyclerViewFrame);
private LinearLayoutManager inearLayoutManagerFrame;
inearLayoutManagerFrame = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerViewFrame.setLayoutManager(linearLayoutManagerFrame);


FramesAdapter frameAdapter = new FramesAdapter(activity, frameSelectedListner);
recyclerViewFrame.setAdapter(frameAdapter);






public class FramesAdapter extends RecyclerView.Adapter<FramesAdapter.ViewHolder> {
    @SuppressLint("StaticFieldLeak")
    public static ImageView mCurrentFilterBtn;
    private static int filterBtnId;
    private final Context context;
    private final FrameSelectedListner frameSelectedListner;
    private int lastPosition;
    private final DisplayImageOptions options;

    public FramesAdapter(Context context, FrameSelectedListner frameSelectedListner) {
        this.context = context;
        this.frameSelectedListner = frameSelectedListner;
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.no_image)
                .showImageForEmptyUri(R.drawable.no_image)
                .showImageOnFail(R.drawable.no_image)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .build();
    }

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(this.context).inflate(R.layout.frames_adapter_item, parent, false));
    }

    public void onBindViewHolder(final ViewHolder holder, @SuppressLint("RecyclerView") int position) {



        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Bitmap bitmapFlag = null;
                try {
                    bitmapFlag = ImageLoader.getInstance().loadImageSync(SERVER_BASE_URL + serverData.getFrDetail().get(holder.getAdapterPosition()));
                } catch (OutOfMemoryError memoryError) {
                    memoryError.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                if (bitmapFlag != null) {
                    if (mCurrentFilterBtn != null)
                        mCurrentFilterBtn.setBackground(context.getResources().getDrawable(R.drawable.stroke_invisible));
                    filterBtnId = view.getId();
                    mCurrentFilterBtn = view.findViewById(filterBtnId);
                    mCurrentFilterBtn.setBackground(context.getResources().getDrawable(R.drawable.frame_stroke_visible));
                }

                frameSelectedListner.onFrameSelected(holder.getAdapterPosition());

            }
        });

        // bind data here
        try {

            String frame_path = SERVER_BASE_URL + serverData.getFrDetail().get(holder.getAdapterPosition());
            ImageLoader.getInstance()
                    .displayImage(frame_path, holder.imageView, options, new SimpleImageLoadingListener() {
                        @Override
                        public void onLoadingStarted(String imageUri, View view) {
                            holder.progress_bar.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                            holder.progress_bar.setVisibility(View.GONE);
                        }

                        @Override
                        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                            holder.progress_bar.setVisibility(View.GONE);
                        }
                    }, new ImageLoadingProgressListener() {
                        @Override
                        public void onProgressUpdate(String imageUri, View view, int current, int total) {
                        }
                    }, true);

        } catch (OutOfMemoryError ignored) {
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public int getItemCount() {

        return serverData.getFrDetail().size();

    }

    class ViewHolder extends RecyclerView.ViewHolder {
        final ImageView imageView;
        final ProgressBar progress_bar;

        ViewHolder(View itemView) {
            super(itemView);
            this.imageView = itemView.findViewById(R.id.imageView);
            this.progress_bar = itemView.findViewById(R.id.progress_bar);
        }
    }

    public interface FrameSelectedListner {
        void onFrameSelected(int position);
    }
}

Frames_adapter_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="@dimen/_40sdp"
    android:layout_height="@dimen/_40sdp"
    android:background="@drawable/stroke_invisible"
    android:layout_marginLeft="@dimen/_2sdp"
    android:layout_marginRight="@dimen/_2sdp"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:contentDescription="@string/todo"
        android:padding="@dimen/_2sdp"
        android:scaleType="fitXY"
        tools:ignore="ContentDescription" />

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
</RelativeLayout>
0
Milan Hirpara 28 feb. 2018 a las 09:15