自定义Listview项目布局

时间:2017-04-26 18:22:39

标签: android layout

我是Android开发的新手。我正在尝试制作自定义列表视图项目布局。我想知道如果我在may布局中有一个ImageView,我将如何提供图像路径。

我填充此ListView的活动文件

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            adapter.getFilter().filter(s);
            return true;
        }
    });

布局文件:

ListView lstItems = (ListView) findViewById(R.id.lstItems);

String[] from = new String[]{"itemname", "productimage"};
int[] to = new int[]{R.id.itemname, R.id.productimage};

List<HashMap<String, Object>> fillMaps = new ArrayList<HashMap<String, Object>>();

for (Item item : objects) {

    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("itemname", item.getName());
    map.put("productimage", item.getImages());

    fillMaps.add(map);

}

SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.shop_listitem_view, from, to);
lstItems.setAdapter(adapter);

1 个答案:

答案 0 :(得分:0)

在这种情况下,SimpleAdapter将不适合您。首先,您必须创建一个代表您的业务逻辑的类:

public class Product {

public String productName;

public String productImage;

}

其次,您必须将Glide库添加到build.gradle(模块应用)文件中。在依赖项块中添加以下行:

dependencies {
  compile 'com.github.bumptech.glide:glide:3.7.0'
  }

之后,您必须创建将由Adapter夸大的布局文件:

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

    <ImageView
        android:id="@+id/your_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/your_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

现在,让我们创建您的Adapter类,它必须扩展BaseAdapter类:

public class YourAdapter  extends BaseAdapter{

    private List<Object> products;
    private Context context;


    public YourAdapter(Context context, List<Product> products){
        this.context = context;
        this.products = products;

    }


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

    @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) {
        LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = li.inflate(R.layout.your_list_layout, null);//set layout for displaying items
        ImageView image = (ImageView) view.findViewById(R.id.your_image_view);//get id for image view
        Glide.with(context).load(products.get(position).productImage).into(image); //set the image in your image view
        TextView text = (TextView) view.findViewById(R.id.your_text_view);
        text.setText(products.get(position).productName);
        return view;
    }
}

最后,在您的活动中,您可以使用它:

//Create a product:
List<Product> p = new ArrayList<>();
Product prod = new Product();
prod.productName = "Name of your product";
prod.productImage = "http://your_image_url";
prod.add(prod);

YourAdapter adapter = new YourAdapter(this, p);

//and finally, set it into your listview:

listview.setAdapter(adater);