Lea*_*ics 6 android android-view
public void setTag(final Object tag) {
mTag = tag;
}
public Object getTag() {
return mTag;
}
Run Code Online (Sandbox Code Playgroud)
这些是Android中View Class的两种方法.以下是这两种方法的官方文档.
/**
* Returns this view's tag.
*
* @return the Object stored in this view as a tag
*
* @see #setTag(Object)
* @see #getTag(int)
*/
/**
* Sets the tag associated with this view. A tag can be used to mark
* a view in its hierarchy and does not have to be unique within the
* hierarchy. Tags can also be used to store data within a view without
* resorting to another data structure.
*
* @param tag an Object to tag the view with
*
* @see #getTag()
* @see #setTag(int, Object)
*/
Run Code Online (Sandbox Code Playgroud)
标记函数广泛用于baseadapter实现,但我无法理解它的用途以及如何使用它们.能否请您解释一下这个好例子,以帮助我理解这些功能的作用
小智 12
可以把它想象成尽可能简单 - 一个"标签".
杂货店产品标签告诉你什么?基本上很多东西,比如名字,价格,原产国,折扣等等.
想象一下,您正在使用ImageView制作一个在网格中显示此类产品的应用程序.如何从ImageView轻松确定产品功能?标记它是可能的解决方案之一.
class Product {
String mName;
String mManufacturer;
String mOriginCountry;
double mPrice;
int mDiscount;
int mImageId; // A reference to a drawable item id
}
[...]
class My Activity {
@Override
protected void onCreate() {
[...] // Some code
// Grab the imageView reference and grab (or create) the tag for it
ImageView someItemView = (ImageView) findViewById(R.id.some_product_view);
Product someProductTag = new Product( ... some data ...);
// Add the tag to the ImageView
someItemView.addTag(someProductTag);
}
private void displayItemInfo(ImageView iv) {
// Grab the tag and display the info.
String productName = ((Product)iv.getTag()).mName();
Toast.show(mContext, "This is " + productName, Toast.LONG).show();
}
}
Run Code Online (Sandbox Code Playgroud)
当然这非常简单.理想情况下,您将拥有一个适配器,可以根据您提供的标签创建视图,或者自动创建标签.
我希望你明白这个主意
归档时间: |
|
查看次数: |
23777 次 |
最近记录: |