简体   繁体   中英

Android - Add Custom View to ListView

I have a ListView , and I have a custom View , PostView .

How would I go about creating a new PostView() , and adding it to the ListView via .addFooterView() , but still being able to access the object representation of the PostView .

Right now I'm trying:

// the footer
p = new PostView( this );

p.setTargetUri( C.API_CONTENT );
p.setRenderMode( PostView.MODE_TEXT | PostView.MODE_IMAGE );

commentListView.addFooterView( p );

But that is failing, when I try to access the should have been inflated XML components.

Mainly:

public void setRenderMode( int renderMode )
{
  this.renderMode = renderMode;

  // fails
  this.postImagePreview = (ImageView)findViewById( R.id.new_post_image_preview );
}

Am I going about this wrong? I have an onFinishInflate() defined, but maybe I'm prematurely accessing the Views?

为什么不挂在成员变量中的实例上,而不是稍后再调用findViewById?

Is your setRenderMode within PostView or the Activity that uses PostView?

If it is in the activity, you should be doing:

this.postImagePreview = (ImageView)p.findViewById( R.id.new_post_image_preview );

with p being a reference to your PostView that you have created.

对我来说,解决方案是仅拥有一个超级自定义列表适配器,该适配器能够返回不同类型的视图,并在生成的列表内容周围手动添加页眉和页脚视图。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM