简体   繁体   English

如何创建这样的自定义ListView?

[英]How to create custom ListView like this?

I want to create a Custom listview like this the below image. 我想要创建一个自定义列表视图,如下图所示。 I am creating a chat app, in that app have to pass text, images and video etc. see below image. 我正在创建一个聊天应用程序,该应用程序必须传递文本,图像和视频等。请参见下图。

在此处输入图片说明

In that, when a user send/receive image in chat, if he wants to see that by clicking on the button beside that image, how to move to another activity with that image path (or) url, for showing full image, and when user clickngi on image have to show a Quick view. 这样,当用户在聊天中发送/接收图像时,如果他想通过单击该图像旁边的按钮来查看该图像,则如何移动到具有该图像路径(或URL)的另一个活动,以显示完整图像,以及何时用户点击图片后必须显示一个快速视图。 same way if that is video, i have to get that path (or) url to play video properly. 如果是视频,同样的方法,我必须获取该路径(或)网址才能正确播放视频。 How to differenciate the list item depends on the item type. 如何区分列表项目取决于项目类型。

You can 您可以

  • implement ScrollView and add views into it. 实现ScrollView并向其中添加视图。
  • create adapter that contains all of the possible views and then decide which view to hide or to show 创建包含所有可能视图的适配器,然后决定隐藏或显示哪个视图
  • you can create adapter and dynamically add view into each row (in this case it's too slow). 您可以创建适配器并将视图动态添加到每一行(在这种情况下,它太慢了)。

My choise is #2 - create row view with all possible views and then decide what to hide, in this case you can save time because you won't have to inflate you views each time and you can use even ViewHolder pattern. 我的选择是#2-创建具有所有可能视图的行视图,然后决定隐藏什么,在这种情况下,您可以节省时间,因为您不必每次都使视图膨胀,甚至可以使用ViewHolder模式。

If you want to show different rows for audio, image and text messages, you need to have 3 row layouts, then you will decide which row needs to be returned from your getView() of your CustomAdapter . 如果要显示音频,图像和文本消息的不同行,则需要具有3行布局,然后您将决定需要从CustomAdapter getView()返回哪一行。 There are two methods getViewTypeCount() and getItemViewType() of Adapter which will help your recycling the row to show up in ListView . getViewTypeCount()有两种方法getViewTypeCount()getItemViewType() ,可帮助您回收行以显示在ListView

You will first tell that how many layouts in your ListView will be using getViewTypeCount() which tells the adapter how many row types will be there, next check what kind of data is present at that position in your data model and you return the view type from getItemViewType() , so getView() will receive the relevant recycled view (if there is any). 首先,您将使用getViewTypeCount()告诉ListView中将有多少个布局,它将告诉适配器那里将有多少行类型,接下来检查数据模型中该位置上存在什么数据,然后返回视图类型。从getItemViewType() ,因此getView()将接收相关的回收视图(如果有的话)。

Here is my blog post about using 9 patch images, it demonstrate sender and receiver type of views, same can be applied for image and audio based on the item in your data model at that specific position. 这是我的博客文章,内容涉及使用9个补丁图像,演示了发送者和接收者的视图类型,这些视图可以基于数据模型中特定位置的项目应用于图像和音频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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