How do I place the image next to the listview items?

Asked

Viewed 709 times

0

I have the following code for listview:

ListView listview = (ListView) findViewById(R.id.listview1);
String[] dados = new String[] { "Produção Geral", "Perdas", "Contagem Por Máquina", "OEE", "Paradas de Máquina" };



ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dados);
listview.setAdapter(adapter);

How do I add image next to items?

2 answers

0

You just need to create an xml of your list item.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginStart="8dp"
        android:scaleType="fitXY"
        android:src="@mipmap/ic_launcher_round"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="Descrição"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/image"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

And then add it to your Recyclerview

0

To add an image, you need to create a custom cell, use the concept of Recyclerview + Viewholder:

In your Activity create Adapter by passing your list:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView rvMain = view.findViewById(R.id.rvMain);

        List<String> dados = Arrays.asList( "Produção Geral", "Perdas", "Contagem Por Máquina", "OEE", "Paradas de Máquina");
        final MainRecyclerAdapter adapter = new MainRecyclerAdapter(dados, context, this);
        rvMain.setAdapter(adapter);
        rvMain.setHasFixedSize(true);
        rvMain.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
    }

Mainrecycleradapter

   public class MainRecyclerAdapter extends RecyclerView.Adapter<MainViewHolder> {
    List<String> dados;
    private Context context;


    public MainRecyclerAdapter(List<String> dados, Context context) {
        this.dados = dados;
        this.context = context;
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_main_item, parent, false);
        return new MainViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull final MainViewHolder holder, int position) {

        holder.setText(dados.get(position));
        holder.setImage(R.mipmap.ic_launcher);
    }

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

    @Override
    public int getItemViewType(int position) {
        return position;
    }
}

Mainviewholder

public class MainViewHolder extends RecyclerView.ViewHolder {

    TextView textView;
    ImageView imageView;

    public MainViewHolder(View itemView) {
        super(itemView);
        textView = itemView.findViewById(R.id.textView);
        imageView = itemView.findViewById(R.id.imageView);
    }

    public void setText(String text) {
        textView.setText(text);
    }

    public void setImage(int image) {
        imageView.setImageResource(image);
    }
}

Add a Recyclerview to your Mainactivity xml and create an xml related to the Recyclerview item (R.layout.recycler_main_item) of your preference.

Browser other questions tagged

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