How to create custom ListView like this? - android

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. 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.

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. There are two methods getViewTypeCount() and getItemViewType() of Adapter which will help your recycling the row to show up in 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).
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.

You can
implement ScrollView and add views into it.
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.

Related

Add a different view in between RecyclerView items without using viewholder

After doing a pretty long research on SO and other sites and reaching deadends, I am posting a new question. Here goes:
I have implemented a RecyclerView that uses only 1 list item type (i don't need to use multiple layout items in the recycler view based on position). The layout as shown below: box 1 in image
what i want to do is, based on some event(click or a push notification), add a different view between two items of the RecyclerView (basically anywhere within the RecyclerView list). The view as shown below. box 2 in image
The following picture depicts what I want to achieve.
the image with the list item type and view to be added
The contents of the view to be added comes from a different source that the data source passed to the RecyclerViewAdapter. so notifyDataSetChanged() or adapter.notifyItemInserted() should not be called (in my opinion).
Is there any way I can get the above mentioned result?

Android: What’s the best way to implement the BaseAdapter if item content in ListView is dynamic?

In most case, we need to implement the BaseAdapter’s getItemViewType() and getViewTypeCount() for dynamic item content of the listview, as this post says. But I think this solution is only suitable for finite number and knowing beforehand, such as listview item with sending layout and receving layout.
What about the case that the listview with its item content impossible knowing beforehand?
For example, I need to show a contact list from server, the contact list size is about several thousand. For each item, I need to show, for example, the hobby “list”. It is a small range of 0 to tens of string. So in this case:
The item types is relatively bigger than normal case using “getItemViewType”
Though each item may be different, but similar to a certain degree: the item content is different in the number of views, but common in view type. Item A is different from item B only because it have more TextViews.
For each time in the getView, the convertview is hard to simply reuse because they are different, but if we create new TextView and added to the convertView, it will impact the scrolling of the listview. I don't think it's appropriate such way. What should i do in such case?
Unfortunately you cannot easily change the number of item view types on the fly. The getViewTypeCount() method is only invoked when the adapter is set on the ListView. That means, were you to dynamically change it, then you would have to call setAdapter() again. This is a huge performance hit as the ListView will toss out all the recycled views and re-generate everything from scratch again.
Honestly, it looks like you should be using an ExpandableListView instead. It allows you to displays lists of items under groups. The only difference is the groups are placed on top of the list. So where you have A, B, C, D on the left side in your picture...in the ExpandableListView it'll sit on top.
The ExpandableListView can easily handle your situation where a given grouping could have any variable number of items within it. You mentioned needing to store a contact list. I'd suggest taking a look at a RolodexArrayAdapter for use with the ExpandableListView. It may be of help.

Which is the better method in Android for creating a dynamic list?

If you are creating a very dynamic list, say, where every row can have a different set of input types plus optional buttons, and the list length is based on another dynamic value, is it better to do this in a list adapter or creating a custom view in a scroll window?
After struggling with list adapters for quite a while now something finally occurred to me- this seems dumb. It seems like I am going through a lot of work keeping track of what spinner is set to what value, which row was clicked and so forth.
For example, say you are showing something like a contacts screen with various details that can be entered about a contact. Some rows will have text inputs (name, address etc), some will have spinners (ie. state, group), some will have checkboxes (like 'favorite' or something). Also, there is an 'add' button that allows you to add another field to edit. Is it worth making this in a list adapter or is it better to populate a custom view, and if the "add" button is clicked, we re-create the custom view, adding a view of the type they want to add?
I hope this is clear.
ListViews (and List Adapters) are meant for data that is to be displayed in mainly similar views. For your example, it is much easier and more natural to have a predefined layout file with the screen and use view visibility so select which views are to be shown. If you need to add views to the screen you can do this dynamically by using findViewById on the layout and then using it's addView method.
Let me know if you need more clarification or sample code...

Android display multiple lists values

I am doing android application. In That I want to display a List of podcast urls like shown in the Image. In this I also share this url into FB, twitter and etc and also the user clicks the arrow symbol I want to forward to that podcast url.
I am having those values in separate arraylist (i.e. "4353,3424" as a arraylist and "567567, 234234" as a another arraylist likewise). So how can I display these values like shown in the attached image. Can anyone help me how to proceed to display like this?
I'd recommend you had a look at some of the tutorials on how to implement your own custom ListView.
An example can be found here: http://www.thepcwizard.in/2012/09/android-creating-custom-listview-for.html
Also I'd recommend you create a custom class for holding the different informations in every row of the ListView. When doing it like this you can have one single List<MyCustomObject> holding all informations and then when a row is clicked, you simple get the item from the List<MyCustomObject> and get the specific property of the custom object and act accordingly.
EDIT: Small example of how to add onClickListener to a sub-view of the row:
Inside the getView method of the custom Adapter you can use setOnClickListener to the views, you'd like to respond to clicks. For instance:
myImageView.setOnClickListener(this);
Then let your custom Adapter implement the interface OnClickListener and act accordingly to the clicks.
Another way would also be to add a Share Intent to the different images, like in this example:
http://sudarmuthu.com/blog/sharing-content-in-android-using-action_send-intent
You would need to create a custom ArrayAdapter to populate a ListView from this objects the way you want.
The advantage of this technic is that you gain a Views recycle mechanism that will recycle the Views inside you ListView in order to spend less memory.
In Short you would have to:
1. Create an object that represents your data for a single row.
2. Create an ArrayList of those objects.
3. Create a layout that contains a ListView or add a ListView to you main layout using code.
4. Create a layout of a single row.
5. Create a ViewHolder that will represent the visual aspect of you data row from the stand point of Views.
6. Create a custom ArrayAdapter that will populate the rows according to you needs.
7. Finally assign this ArrayAdapter to your ListView in onCreate.
You can get an Idea of how to implement this by reading this blog post I wrote:
Create a Custom ArrayAdapter

Change ImageView content within a GridView in Android

I am new to Android and Java. I am building an app that allows users to push a button which launches an image chooser where they can select from images on the sd card. The app loads with a grid view with 2 cells. One cell has an image view that has a default image. The other is the button. Once, the image is chosen, the image view needs to be displayed in the Image View of the Grid View.
I am using a string path that is being decoded from the images uri to create a bitmap. I then am calling imageView.setImageBitmap(bitmap). This is doing nothing. I have tried updating the image resource with a different image in the drawable folders and still nothing. I added a seperate image view just under the grid in my activity, and that image is updating fine which leads me to believe that this has to do with the grid view (this is my first grid view).
Any help is very much appreciated.
Thank you.
GridView is maybe not the right choice if you've only got two cells. You will probably have an easier time with a simple LinearLayout that has the two items you're dealing with.
But, here's how you do it with a GridView:
Create your adapter by extending BaseAdapter. The most important method is getView(), but it's where you should be doing the least amount of work.
When the GridView goes to redraw itself, it'll go through for each visible position and call getView() for each one. You'll be supplied a View. If the view is null, inflate a new one from your XML resources, populate it, and return it. If the view is NOT null, populate the existing one with the appropriate data. The widget is being efficient by recycling views.
The trick you need is that when you want the image to change, you need to call notifyDataSetChanged() on your adapter in order to trigger the redraw (which is when you'll populate the new image in the view for the appropriate cell).
That's the two-minute version. At Google I/O a year or two back, they did a talk on The World of ListView and posted it on YouTube. It was a good talk and pretty much everything there applies to GridView as well.

Categories

Resources