i want to build an application with a listview or whatever.. that looks very attractive and have some images and many more.But i cant find a good way to have that. I exactly want my application's UI like this images:
(source: coenraets.org)
(source: coenraets.org)
i want to display my app like this images please suggest me how can i do this? please if u know some tutorials then give the links.
Yes. Just place one ImageView and TextView in one xml layout. And, inflate this layout into one layout which is having the ListView And, do the process there for getting images from webservice or locally stored
Here i provide some example links that may very useful to you -
Lazy load of images in ListView
ListView with images
How to display a list of images in a ListView in Android?
You need to build the layout you want in a XML file, the same way you would do for an Activity. Then just inflate the XML layout for each row in your ListView and set its values and images.
Example of one ArrayAdapter that I've inflated with my own view (picture, text and checkbox):
private class FriendListAdapter extends ArrayAdapter<User> {
public FriendListAdapter(Activity a, int textViewResourceId, List<User> items) {
super(a, textViewResourceId, items);
}
public class ViewHolder{
public TextView username;
public ImageView image;
public CheckedTextView ctv;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.invite_friend_row, null);
holder = new ViewHolder();
holder.username = (TextView) v.findViewById(R.id.username);
holder.username.setTypeface(tf);
holder.image = (ImageView) v.findViewById(R.id.image);
holder.ctv = (CheckedTextView) v.findViewById(R.id.checked);
v.setTag(holder);
}
else{
holder = (ViewHolder) v.getTag();
}
final User user = getItem(position);
if(user != null){
holder.username.setText(user.getName());
holder.ctv.setChecked(user.isChecked());
holder.image.setImageView(user.getImage());
}
return v;
}
}
You get the idea!
Related
I have a list of words. I want the letters of each word in the list to display as images in a gridview.
I have implemented a working version of this by creating a custom adapter, WordListAdapter, for my list of words. For each word, WordListAdapter.getView, in turn, calls a custom adapter, LetterImageAdapter, to display the letters in the words. Since I'm new to Android programming, I'm wondering if this is a good approach. It sure seems like a lot of work. If it is a good approach, perhaps this will be helpful to someone else.
I've seen a number of other posts about gridviews inside listviews. Many of them touched on issues with scrolling and memory but I didn't see anything specific to this use case.
In my activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
ListView listView = (ListView) findViewById(R.id.wordList);
listView.setAdapter(new WordListAdapter(this, new String[]{"ABC", "DEF"}));
}
WordListAdapter.getView()
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.word_grid, null);
GridView gridView = (GridView) convertView.findViewById(R.id.word_grid_id);
holder = new ViewHolder();
holder.gridView = gridView;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.gridView.setAdapter(new LetterImageAdapter(mContext, words[position]));
return convertView;
}
private static class ViewHolder {
GridView gridView;
}
LetterImageAdapter.getView()
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.letter_item, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.list_item_letter_imageview);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// resource for each letter is in lettersId array
holder.icon.setImageResource(letterIds[position]);
return convertView;
}
private static class ViewHolder {
ImageView icon;
}
// transform word into array of resources for each letter
private void buildResourceArray(String packageName) {
letterIds = new int[theWord.length()];
for (int index = 0; index < theWord.length(); index++) {
letterIds[index] = getResourceId(theWord.substring(index, index + 1).toLowerCase(), "drawable", packageName);
}
}
I'd suggest to use a LinearLayout with ImageViews instead of a GridView to hold the letters.
While technically is possible to have a GridView inside a ListView, GridViews are pretty complex objects, better suited to be a main list than a recyclable child item. You will run into complex issues when recycling the main items, as every GridView will have its own adapter.
So as the number of letters is pretty limited, a LinearLayout (maybe inside a HorizontalScrollView) is a far simpler approach. You can even cache the letter bitmaps, or include them as resources, so they are shared by all ImageViews in the LinearLayouts
To do something related to your intentions, you'd better look at RecyclerView, that is kind of an abstract item list container that you can setup to do all kinds of complex layouts. But being a beginner, I'd suggest to first master the simpler ListView / GridView then move into RecyclerView when you understand how View recycling works.
I have created a ListView and its custom Adapter. But due to some reason I am not allowed to get items from ViewHolder.
In my case ViewHolder has only one variable and that is of LinearLayout. LinearLayout contains the other child views(which is decided and created at run time). When I use ViewHolder and set the tag of holder object, on scroll I am getting the same views again.
Is there any other way to stop adapter to create views while scrolling ?
Or, while scrolling how can we clear the references of views ?
I have find this but I don't think this will work.
setRecyclerListener(new RecyclerListener() {
#Override
public void onMovedToScrapHeap(View view) {
//from here can we use this to clean the memory
}
});
ViewHolder is meant as a holder to contain ids of listitem layout.
It is optimization to avoid calling findViewById everytime new listitem is created for display by going through data container e.g. arrayList.
You cannot stop adapter in between creating item views.
Only items on display are created.
convertView acts as object being recycled for creating subsequent view while scrolling up/down.
You will not be able to use view holder for the purpose you are trying to achieve.
Sample usage as below.
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
ViewHolder viewHolder = null;
if(convertView == null)
{
v = LayoutInflater.from(StockDetailsActivity.this).inflate(R.layout.stock_details_list_item, null);
viewHolder = new ViewHolder();
viewHolder.model_name_tv = (TextView) v.findViewById(R.id.model_name);
viewHolder.model_type_iv = (ImageView) v.findViewById(R.id.model_type_icon);
viewHolder.model_type_tv = (TextView) v.findViewById(R.id.model_type_desc);
viewHolder.model_stock_tv = (TextView) v.findViewById(R.id.model_stock_value);
v.setTag(viewHolder);
}
else
viewHolder = (ViewHolder) v.getTag();
stockCursor.moveToPosition(position);
// logic to update data to views as appropriate goes here
return v;
}
public class ViewHolder{
public TextView model_name_tv;
public ImageView model_type_iv;
public TextView model_type_tv;
public TextView model_stock_tv;
}
I'm trying to create section headers for my ListView in a way that some list rows have headers and some do not. So what i have is three xml layout files. fragment_listview.xml contains the <ListView>
row_listview.xml contains the content for ListView rows
header_listview.xml is for the section headers.
Feeds.java is my model class.
So in my adapter i have something like this:
public class FeedsArrayAdapter extends ArrayAdapter<Feeds> {
private Context context;
private List<Feeds> feeds;
private LayoutInflater vi;
FeedsArrayAdapter(Context c, List<Feed> ff){ //initializes all the class member fields }
public void getView(int position, View convertView, ViewGroup parent){
Feeds f = feeds.get(position);
if(f.headerNeeded()){
convertView = vi.inflate(R.layout.header_listview, null);
TextView textViewHeader = (TextView) convertView.findViewById(R.id.feed_header);
textViewHeader.setText(new SimpleDateFormat("dd/MM/yyyy").format(feed.date));
}
else {
convertView = vi.inflate(R.layout.row_listview, null);
TextView textViewfeedHeading = (TextView) convertView.findViewById(R.id.feed_heading);
TextView textViewfeedSubheading = (TextView) convertView.findViewById(R.id.feed_subheading);
TextView textViewfeedDate = (TextView) convertView.findViewById(R.id.textViewDate);
ImageView imageViewfeedIcon = (ImageView) convertView.findViewById(R.id.feed_icon);
ImageView imageViewfeedBanner = (ImageView) convertView.findViewById(R.id.feed_banner);
textViewfeedHeading.setText(feed.title);
textViewfeedSubheading.setText(feed.subtitle);
textViewfeedDate.setText(new SimpleDateFormat("dd/MM/yyyy").format(feed.date));
}
return convertView;
}
So this gives us something like this:
---header1----
---header2----
___Row 3_____
___Row 4_____
What i want is:
-----header 1------
_____Row 1_______
-----header 2------
_____Row 2_______
_____Row 3_______
_____Row 4_______
If i try to inflate row.listview in if(true) I get an exception.
Any approach how to resolve this.
you can use Sticky Header Library here
yourListView.addHeaderView(yourHeaderView);
See http://developer.android.com/reference/android/widget/ListView.html#addHeaderView%28android.view.View%29
use expandable listview with BaseExpandableAdapter
I have seen similar questions to that, but mine is a little bit different:
I have a listview and each row has an imageview that loads a picture from url. Here is my adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) this.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.bestof_list_row, parent, false);
holder = new ViewHolder();
holder.textviewRowNumber = (TextView) convertView.findViewById(R.id.textview_row_number);
holder.imageviewUserPic = (RemoteImageView) convertView.findViewById(R.id.imageview_user_profile_pic);
holder.textviewUsername = (TextView) convertView.findViewById(R.id.textview_username);
holder.textviewSubtextBold = (TextView) convertView.findViewById(R.id.textview_subtext_bold);
holder.textviewSubtextNotBold = (TextView) convertView.findViewById(R.id.textview_subtext_not_bold);
}else{
holder = (ViewHolder) convertView.getTag();
}
BestOfSubTabListItem item = getItem(position);
holder.textviewRowNumber.setText(String.valueOf(position+1));
holder.imageviewUserPic.setRemoteURI(item.getUserAvatarURL());
holder.imageviewUserPic.loadImage();
holder.textviewUsername.setText(item.getUsername());
if(feedType == SocialFeedType.hero2){
holder.textviewSubtextBold.setVisibility(View.GONE);
holder.textviewSubtextNotBold.setVisibility(View.GONE);
}else{
holder.textviewSubtextBold.setVisibility(View.VISIBLE);
holder.textviewSubtextNotBold.setVisibility(View.VISIBLE);
holder.textviewSubtextBold.setText(item.getSubtextBold());
holder.textviewSubtextNotBold.setText(item.getSubTextNotBold());
}
convertView.setTag(holder);
return convertView;
}
static class ViewHolder{
public TextView textviewRowNumber;
public RemoteImageView imageviewUserPic;
public TextView textviewUsername;
public TextView textviewSubtextBold;
public TextView textviewSubtextNotBold;
}
When i scroll the listview fast, the images in imageviews change, they are put to wrong positions. One solution is removing the
if(convertview==null)
but this time, listview does not scroll smoothly. Can anyone help me to fix this problem?
Thanks
When you're using convertviews, it means that listview will try to recycle views. In your case it means that the same view will be used in different positions in listview, and you're actually trying to load different images into same imageview. To prevent this, try using Picasso or Volley, they have this view reusing thing sorted out.
If you want to do it manually, you have to watch when your view goes off the screen and cancel image pulling request to make that imageview available to handle proper image for that position.
http://square.github.io/picasso/
I'm creating an expandable listview that contains a restaurant menu. When the user clicks on one of the items, he sees the details of the item (for example clicking on the row "avocado roll" shows a new layout that states "avocado, crab, rice").
Here is a screenshot of what a standard view is:
(I can't post photos because I am new but here is what the user would see if he saw the listview:
PRODUCT DESCRIPTION
PRODUCT DESCRIPTION
PRODUCT DETAIL (different format)
PRODUCT DESCRIPTION
PRODUCT DESCRIPTION
The idea is that the default features the name of the food item; and if the user clicks (or swipes, eventually - but let's keep it simple for now) he should see the description (in black).. Problem is, when I scroll the black row switches back to the original default format. (the problem will become more acute when I try to get the interface to "remember" where the user clicked and keep those buttons pressed so teh user can know what he ordered - and it clearly has to do with me not understanding how the holder works!)
I use a viewflipper inside each row and it's working great - fast and reliable. Also only a single row gets selected when I click, which is nice.
My only problem is I can't get the holder to remember the status of the viewflipper when I scroll. So it resets every time I scroll.
For example, in the steak example, if I show the detail view in the row, then scroll down and back up, the view in the row returns to the name "avocado roll". Clearly I'm not implementing the holder correctly so it remembers the viewflipper value but for the life of me I can't figure this out after 24 hours of trying!
Here is my adapter code:
ExpandMenu Adapter so you know what the lists are referring to:
public ExpandMenuAdapter(Context passedContext, ArrayList<ExpandOptionsGroup> passedGroupList){
this.context = passedContext;
this.groupList = passedGroupList;
}
getChildView code: this is where I think the holder issue is but I can't ID it!
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View rowView = null;
String[] child = getChild(groupPosition, childPosition);
if (rowView == null){
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
rowView = infalInflater.inflate(R.layout.item_row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.itemName = (TextView) rowView.findViewById(R.id.tvItem);
viewHolder.itemPrice = (TextView) rowView.findViewById(R.id.tvPrice);
viewHolder.itemDetail = (TextView) rowView.findViewById(R.id.tvDetail);
viewHolder.itemLayout = (LinearLayout) rowView.findViewById(R.id.item_layout);
viewHolder.rowFlipper = (ViewFlipper) rowView.findViewById(R.id.item_row_flipper);
viewHolder.itemLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
viewHolder.rowFlipper.showNext();
Toast.makeText(context, "You clicked", Toast.LENGTH_SHORT).show();
}
});
rowView.setTag(viewHolder);
viewHolder.rowFlipper.setTag(groupList.get(groupPosition));
}else{
rowView = convertView;
((ViewHolder) rowView.getTag()).rowFlipper.setTag(groupList.get(groupPosition));
}
ViewHolder holder = (ViewHolder) rowView.getTag();
//holder.rowFlipper.set(groupList.get(groupPosition)); CLEARLY THIS IS WRONG
holder.itemName.setText(child[MenuXMLGettersSetters.ELEMENT_NAME]);
holder.itemDetail.setText(child[MenuXMLGettersSetters.ELEMENT_DETAIL]);
if(!child[MenuXMLGettersSetters.ELEMENT_PRICE].isEmpty()){
holder.itemPrice.setText("$"+child[MenuXMLGettersSetters.ELEMENT_PRICE]);
}else{
holder.itemPrice.setText("");
}
return rowView;
}
private static class ViewHolder {
public TextView itemName;
public TextView itemPrice;
public TextView itemDetail;
public LinearLayout itemLayout;
public ViewFlipper rowFlipper;
private void getViewHolderDetail(){
itemDetail.isShown();
}
}