Looking 4.2.2apidemos the code, see the following code
in com.example.android.apis.view.List14
/ **
* Make a view to hold each row.
*
* @ See android.widget.ListAdapter # getView (int, android.view.View,
* Android.view.ViewGroup)
* /
public View getView (int position, View convertView, ViewGroup parent) {
/ / A ViewHolder keeps references to children views to avoid unneccessary calls
/ / To findViewById () on each row.
ViewHolder holder;
/ / When convertView is not null, we can reuse it directly, there is no need
/ / To reinflate it. We only inflate a new View when the convertView supplied
/ / By ListView is null.
if (convertView == null) {
convertView = mInflater.inflate (R.layout.list_item_icon_text, null);
/ / Creates a ViewHolder and store references to the two children views
/ / We want to bind data to.
holder = new ViewHolder ();
holder.text = (TextView) convertView.findViewById (R.id.text);
holder.icon = (ImageView) convertView.findViewById (R.id.icon);
convertView.setTag (holder);
} Else {
/ / Get the ViewHolder back to get fast access to the TextView
/ / And the ImageView.
holder = (ViewHolder) convertView.getTag ();
}
/ / Bind the data efficiently with the holder.
holder.text.setText (DATA [position]);
holder.icon.setImageBitmap ((position & 1) == 1 mIcon1: mIcon2?);
return convertView;
}
static class ViewHolder {
TextView text;
ImageView icon;
}
Think there is a question: If I want to write individually to TextView OnClick method should how to obtain an instance of this View it? BaseAdapter in what can be achieved?
Or I could get a direct LinearLayout instance wrapped text and icon for it? Activity in this instance should be a ListItem.<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Direct holder.text.setOnClickListener (***) on the line ah
Reply:
To give an example, if the text on setOnClickListener, then how to obtain the icon in the OnClick method in? That said, I think by clicking on the text, change the icon, so you?
Reply:
Inside this function directly in the getView operation, holder.text and holder.icon instances have
final ImageView iconView = holder.icon;
holder.text.setOnClickListener (new OnClickListener () {
@ Override
public void onClick (View v) {
iconView.setImageResource (R.drawable.xxxxx);
}
});
Landlord try.
Reply:
bingo, you say how I would never have thought it, thanks

No comments:
Post a Comment