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

Asked

Viewed 178 times

0

How to make that when creating multiple imageview , it go to the other side , and when ending the space in the column it pass to the next? I tried to use linear but when it ends the column , he keeps putting in the same column , and decreasing the size so that it fits all

inserir a descrição da imagem aqui

<android.support.v4.widget.NestedScrollView 
xmlns: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>

1 answer

1


You can do it using one RecyclerView.

You will create an Adapter, which you will manage when you add a new image to open 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));

    }
  }

and in your Activity, you will put a RecyclerView where Voce wants to display the images, and 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 should add another item in the list with value null, 'cause he’s the one who’s gonna create the next little square next door.

Item XML

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

Xml item is my Adapter xml.

  • Then I create a separate class and put the recyclerimageadapter right? This recyclercview I replace the linear layout with it ?

  • That friend, the adapter is a separate class. In the case of Recycler instead of linear, here goes its layout. What you should take are all the image Buttons.

  • I put the image button inside the Recycler right

  • Note also that I treated my Adapter for 2 different image views. A call image, and another imageadd. In your case, you can use only 1 and when comparing, show a placeholder if image is null

  • I edited my answer by putting the itemadd, layout that you will use on Adapter

Browser other questions tagged

You are not signed in. Login or sign up in order to post.