- android listview different views for every item
- 2 Answers 2
- Not the answer you’re looking for? Browse other questions tagged android listview or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Best way to dynamically render different views in Android?
- 5 Answers 5
- Not the answer you’re looking for? Browse other questions tagged java android or ask your own question.
- Related
- Hot Network Questions
- Subscribe to RSS
- Introduction to Android Views and ViewGroups
- Views
- XML syntax for creating a View
- Most commonly used Android View classes
- Programmatic and Declarative Approach
- Create different view types in RecyclerView Android
- Learn how to create different views with RecyclerView using two different layouts
- Step 1
- Step 2
- Step 3
- Step 4
- Summary
- Know your author
android listview different views for every item
Below is the piece of code which is used to get view for the list item. I want to use different view for every row of listview , how can I do it? Any good tutorials?
2 Answers 2
You will need a generally more complicated Adapter . In particular, you need to override getViewTypeCount() and getItemViewType() . You can take a look at my SackOfViewsAdapter as a way of doing this for short lists, or my MergeAdapter for blending individual views with the results of database or Web service queries.
I know this has already been answered. but I wanted to give a more complete example.
In my example, the list activity that will display our custom list view is called OptionsActivity, because in my project this activity is going to display the different options my user can set to control my app. There are two list item types, one list item type just has a TextView and the second list item type just has a Button. You can put any widgets you like inside each list item type, but I kept this example simple.
The getItemView method checks to see which list items should be type 1 or type 2. According to my static ints I defined up top, the first 5 list items will be list item type 1, and the last 5 list items will be list item type 2. So if you compile and run this, you will have a ListView that has five items that just contain a button, and then five items that just contain a TextView.
Below is the activity code, the activity xml file, and an xml file for each list item type.
Not the answer you’re looking for? Browse other questions tagged android listview or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
Best way to dynamically render different views in Android?
I am relatively new to Android development and I am confused on what would be the best way to dynamically render different views in Android.
I have an activity that searches through a database of information and displays the results to the user with an expandable list view. As it currently stands, the xml layout file just has an expandable list view. This element is initially blank but is then populated when the user performs a search.
My issue is I would like to show a message to the user(Instructions) if the expandable list view has not yet been populated, and I am not sure what the best way to do this is? Should I dynamically add another view at runtime or is there a better way to accomplish this?
5 Answers 5
The simplest way to accomplish this is with 2 separate views in your layout. First the an explanation text will appear and once you get data you can hide the view and show the list.
Below is the core of what you can do:
Layout:
Java:
I thing what you can do is create a view ( ImageView, Button, ProgressBar, whatever you want to use to display a message for user ) in your xml file an show it before you get the data from the Data Base . Once you get data hide the View.
One option is to use a so-called «empty view». Empty views are views that get shown to the user in place of a list/grid when there’s no data loaded into them. They’re a nice, user-friendly way of telling the user that there’s no content to show. In your use case, they can also serve as place for instructions to the user about how to get data loaded.
There are a number of ways of implementing this (it’s hard to know which one is easiest or best for you without seeing more of your code), but the general concept is the same: Add an «empty view» to your layout XML file. It can be anything you want, but to give you an idea of what most look like, check out what the Material Design guidelines have to say about this topic. Give the empty view an initial visibility of View.VISIBLE , then set it to View.GONE or View.VISIBLE each time you update your list’s ExpandableListAdapter . If the adapter is empty, set the empty view to View.VISIBLE , otherwise set it to View.GONE . If you’d like more specific guidance (e.g. some actual code), we’ll have to see more of your code that shows how data is being loaded.
You can take frame layout as the root layout and add a layout you would like to show when the list is empty , and then you list as the second child . And you need to manage their visibity on differnt events.
Note :- While using frame layouts the last child is shown above the other children if these have a same layout_gravity.In Frame Layout you can align view by setting its layout_gravity attribute.
The correct way is to use Fragments together with LoaderManager.LoaderCallbacks. While the request is being made, and if it does not return any data, the EmptyView will be shown, in the case that you dont have an Internet connection, the EmptyView will also be shown. When the LoaderManager has some data, it will automatically replace the EmptyView with the Listview:
The CategoryAdapter class could be something like this:
The Fragment class could be something like this:
The Adapter class could be something like this:
Not the answer you’re looking for? Browse other questions tagged java android or ask your own question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
Introduction to Android Views and ViewGroups
All the interaction of a user with the Android application is through the user interface(UI), hence it is very important to understand the basics about the User Interface of an android application. Here in this tutorial, we are going to cover about various Views and ViewGroups and will try to explain how they can be used to design the User Interface of an android application.
Views
View is the basic building block of UI(User Interface) in android. View refers to the android.view.View class, which is the super class for all the GUI components like TextView , ImageView , Button etc.
View class extends Object class and implements Drawable.Callback , KeyEvent.Callback and AccessibilityEventSource .
View can be considered as a rectangle on the screen that shows some type of content. It can be an image, a piece of text, a button or anything that an android application can display. The rectangle here is actually invisible, but every view occupies a rectangle shape.
The question that might be bothering you would be , what can be the size of this rectangle?
The answer is either we can set it manually, by specifying the exact size(with proper units) or by using some predefined values. These predefined values are match_parent and wrap_content .
match_parent means it will occupy the complete space available on the display of the device. Whereas, wrap_content means it will occupy only that much space as required for its content to display.
A View is also known as Widget in Android. Any visual(that we can see on screen) and interactive(with which user can interact with) is called a Widget.
XML syntax for creating a View
Now, as we have explained earlier as well, to draw anything in your android application, you will have to sepcify it in the design XML files. And to add functionality we will create Java files.
Every view in XML has the following format:
- It always start with an angle bracket, followed by the View name. We will introduce you to various types of Views very soon.
- Then we write attributes that will define how that view will look on the screen of the application along with a value for the attribute. Each view has its own attributes which we will discuss in the next few tutorials which will cover various typs of views.
- In the end, it is closed by/>
So, every View subclass needs to follow this format so that it can appear on the screen of the app. And this format is nothing but default XML style. Right!
There are two attributes that are necessary for every View . These are: android:layout_height and android:layout_width .
These attributes define the size of the invisible rectangle that a view makes. Using these attributes we can easily control the size for every view in our android application.
Apart from the above mentioned attributes, attributes like gravity , layout_gravity , padding and margin are some other commonly used attributes.
Most commonly used Android View classes
Here we have some of the most commonly used android View classes:
Programmatic and Declarative Approach
To create/define a View or a ViewGroup in your android application, there are two possible ways:
- The Programmatic Approach: In this we define/create our Views in the Java source file. We will learn about this approach in details later, as of now here is a sample code to add a Button to our view.
So addView() is the function used to add any View to the UI and setLayoutParams() function is used to set the various attributes.
Источник
Create different view types in RecyclerView Android
Learn how to create different views with RecyclerView using two different layouts
Nov 25, 2019 · 4 min read
RecyclerView is a ViewGroup added to Android in the v7 support library. Using RecyclerView , you can design any complex layout as a list with the help of XML.
RecyclerView is the successor of the ListView and GridView and it is an improvement of both ListView and GridView . It gives us the freedom to create any type of view and performs amazingly when items in the lists are vast.
Step 1
We are going to create the following screen which will show an image as a banner embedder in the GridView using RecyclerView .
First of a l l, we will create two layout files, image_with_title.xml and banner_image.xml .
The image_with_title.xml file will show the title below the image.
banner_image.xml will only display a single image that will occupy the full width of the screen as shown in the above screenshot.
Step 2
Now I have created a model class that has three instance variables, two static variables, and one constructor to initialize the instance variables.
Two static and final variables, TYPE_BANNER and TYPE_IMAGE_WITH_TEXT , will be used to check which layout needs to be inflated.
Step 3
In this step, we will create our adapter class which will extend the RecyclerView.ViewHolder class and will override its necessary methods.
I have created two static classes and both of them extend the Recyclerview.ViewHolder class.
The BannerTypeViewHolder class has been created to access the banner image from the banner_image.xml file and, similarly, the ImageTypeViewHolder class gets the IDs of the title and the image from image_with_title.xml .
Now, create the constructor of the MultiViewTypeAdapter class and initialize the array list of type MultiViewModel .
It’s time to implement the onCreateViewHolder method which inflates the layout files on the basis of TYPE .
The following lines of code are used to fill the entire width of the screen so that our banner image can fit in.
Now, the full implementation of onCreateViewHolder .
When 0 is passed into the MultiViewModel constructor then the banner image will be shown. Otherwise, the title image will be shown.
The getItemViewType() method tells the onBindViewHolder method which view it has to display.
The onBindViewHolder() method:
Now the final code of our adapter:
Step 4
This is the final step in which we will simply create the activity to pass the data into the adapter and an XML file which will include RecyclerView .
Inside your onCreate() method, paste the following code:
The prepareData() method prepares the data to be set in the RecyclerView adapter.
Note: i==6 means we want to show the banner after the sixth item.
Summary
- We’ve discussed what RecyclerView is and when to use it.
- We have created two XML files to show our views as a list in RecyclerView .
- Created a model class to hold our data.
- Created an adapter to create and bind views.
- Finally, we prepared our data and passed it to the adapter class.
Subscribe my mailing list to get the early access of my articles directly in your inbox or follow my own publication on Medium The Code Monster to polish your technical knowledge.
Know your author
Himanshu Verma has graduated from the Dr. APJ Abdul Kalam University, Lucknow (India). He is an Android & IOS Developer and Machine Learning & Data Science Learner, a Financial Advisor, and a Blogger.
Источник