如何为Category和CategoryItem设置android xml布局 [英] How can I make an android xml layout for Category and CategoryItem

查看:71
本文介绍了如何为Category和CategoryItem设置android xml布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用Java和Android Studio的新手,我想知道如何在1个活动中进行这样的布局. myImage

I'am new in using java and android studio and i was wondering how can i make a layout like this in 1 activity. myImage

推荐答案

您正在寻找的是Listview适配器

What you are looking for is a Listview adapter

public class ExampleAdapter extends BaseAdapter {
    List<Product> products;
    private Activity context;

    @Override
    public int getCount() {
        return products.size();
    }

    public ExampleAdapter(List<Product> products, Activity context) {
        this.products = products;
        this.context = context;
    }

    @Override
    public Object getItem(int position) {
        return products.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Product product = products.get(position);

        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.row_shopping_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.getProductTextView().setText(product.getName());
        holder.getProductImageView(). setImageResource(product.getPicture());

        return convertView;
    }

    private class ViewHolder {
        private final TextView productTextView;
        private final ImageView productImageView;

        private ViewHolder(View wrapperView) {
            productTextView = (TextView) wrapperView.findViewById(R.id.tvName);
            productImageView = (TextView) wrapperView.findViewById(R.id.ivPicture);

        }

        public TextView getProductTextView() {
            return productTextView;
        }

        public ImageView getProductImageView() {
            return productImageView;
        }

    }
}

此适配器需要一个布局来定义行的外观.这将是这样的

This adapter needs a layout to define what the rows will look like. This will be something like this

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

    <ImageView
        android:layout_gravity="center_vertical"
        android:layout_width="50dp"
        android:id="@+id/ivPicture"
        android:layout_height="50dp" />

    <TextView
        android:id="@+id/tvName"
        android:text="Product 1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

在活动中最后一个要显示概述的地方,您将需要使用列表视图并设置适配器

Lastly in your activity where you want to display the overview you will need to use a listview and set the adapter

final ExampleAdapter adapter = new ExampleAdapter(list, this);
listview.setAdapter(adapter);

这篇关于如何为Category和CategoryItem设置android xml布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆