How to do that when creating multiple imageview, it go next to the other side?

0

How to make when creating multiple imageview, it go next to the other, and when finishing the space in the column it move to the next? I tried to use the linear but when the column ends, it continues putting in the same column, and decreasing the size so that everything fits

<android.support.v4.widget.NestedScrollViewxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.paivadeveloper.lolvoices.AhriActivity"
tools:showIn="@layout/activity_ahri"
android:background="@color/corFundo">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingTop="30dp">

    <ImageButton
        android:id="@+id/imageButton6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:srcCompat="@drawable/ahriicon" />

    <ImageButton
        android:id="@+id/imageButton5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:srcCompat="@drawable/ahriicon" />

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:srcCompat="@drawable/ahriicon" />

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:srcCompat="@drawable/ahriicon" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:srcCompat="@drawable/ahriicon" />


</LinearLayout>

</android.support.v4.widget.NestedScrollView>
    
asked by anonymous 26.05.2017 / 20:11

1 answer

1

You can do this using RecyclerView .

You will create an adapter, which will manage when you add a new image so that it opens a new square on the side. It would look something like this:

RecycleImageAdapter

public class RecycleImageAdapter extends RecyclerView.Adapter<RecycleImageAdapter.ViewHolder>  {

private Context context;
private LayoutInflater inflater;
private List<Uri> itens;
boolean view;

public RecycleImageAdapter(Context context, List<Uri> itens, boolean view){
    this.context = context;
    this.itens = itens;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.view = view;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View v = inflater.inflate(R.layout.item_add, viewGroup, false);
    RecycleImageAdapter.ViewHolder mvh = new RecycleImageAdapter.ViewHolder(v);
    return mvh;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
    Uri imagem = itens.get(position);
    prepare(viewHolder, imagem, position);

}

private  void  prepare(final ViewHolder viewHolder, final Uri imagem, int position){
    if(imagem != null) {

   // Aqui tu vai exibir a sua imagem utilizando Picasso, Fresco ou afins

    }else{
        viewHolder.imageadd.setVisibility(View.VISIBLE);
    }

}

@Override
public int getItemCount() {
    return itens.size();
}


public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{

    ImageView image;
    ImageView imageadd;


    public ViewHolder(View itemView) {
        super(itemView);
        image = ((ImageView) itemView.findViewById(R.id.image));
        imageadd = ((ImageView) itemView.findViewById(R.id.imageadd));

    }
  }

In your Activity, you will put a RecyclerView where you want to display the images, and it would look like this:

XML

     <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="15dp"
        android:id="@+id/lista">
    </android.support.v7.widget.RecyclerView>

Activity

        RecyclerView lista = (RecyclerView) findViewById(R.id.lista);
        lista.setHasFixedSize(true);

    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    //Aqui voce vai setar quantos itens por coluna quer exibir
    lista.setLayoutManager(new GridLayoutManager(this, 3));

    imagens.add(null);

    imageAdapter = new RecycleImageAdapter(this, imagens,false);
    lista.setAdapter(imageAdapter);

As you can see, when you add a new image, you must add another item to the list with value null , because it is the one that will create the next corner of the side.

XML Item

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginRight="10dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageadd"
        android:scaleType="centerCrop"
        android:src="@drawable/placeholder"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/image"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

Item xml is my adapter.

    
29.05.2017 / 16:27