I want to make a first child item of grid view as "Add" button like below the image Link. Data should be appear on from position 1, not a position 0. Don't suggest the headerview, I tried it. please give me some great ideas to do that.
Click here to view
In your adapter's getView method, do this check:
#NonNull
#Override
public View getView(final int position, View convertView, #NonNull ViewGroup parent) {
// ...
// ...
if (position == 0) {
yourImageView.setImageResource(mContext.getResources().getDrawable(R.drawable.your_add_img));
}
return convertView;
}
It should be quite easy. If you have an adapter, just check if you're in the first indexing position and then add the button. If you're not you add your images.
Related
I'm in the process of making a grocery list app and I wrote the code to where when I click an item, it'll mark it off.
This is my code for that section:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView text = (TextView) view;
if (!text.getPaint().isStrikeThruText()) {
text.setPaintFlags(text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}else{
text.setPaintFlags(text.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
}
}
});
}
It works exactly like i want but when I add another item after an item is marked off, all of the items that are marked off, the marks disappear.
When I add an item, it's like it resets. it doesn't delete any of my items, just the strike_thru part of it. any help would be greatly appreciated! thanks
Your ListView's Adapter contains a method called getView, which is called when a list view item needs to be displayed in an actual View. The Views in your ListView will be discarded if you scroll too far off screen, or invalidate the whole ListView.
My guess is that adding an item is invalidating the ListView.
Your getView method should set the paint flags on the view that it returns. Assuming your list view is displaying a String[], you will also need a boolean[] to hold whether or not an item is complete. You would need to initialize this to all falses, add a completed[i] = !completed[i] at the beginning of your onItemClick. Then you can check competed[i] instead of isStrikeThruText in your if statement, later in that method. Finally, your getView can look like this
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
}
TextView textView = (TextView) convertView;
textView.setText(items[position]);
if (completed[position]) {
textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
textView.setPaintFlags(textView.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
}
return textView;
}
I have been reading articles and questions about recycling a listItem rather than Inflating a new one which is costly. In particular I read this question and I understood the answers, but my question is: When we scroll down a list some listItems from the top disappear and they should be recycled and be shown again in the bottom of the List.
How does android determine the position of them?
When we use the viewHolder = (ViewHolder)convertView.getTag; the viewHolder obviously holds the row who wants to be recycled and when we set View Elements by using viewHolder.txtName.setText(dataModel.getName()); I understand that we update the Elements in that particular viewHolder so how does it go to the bottom of the List?
I found this article very useful but there is not enough explanation and comments to make recycling clear for me.
Android ListView with Custom Adapter
How does android determine the position of them?
The ListView adapter (ArrayAdapter for example) has ArrayAdapter.getView() and the equivalent adpater for RecyclerViews is RecyclerView.Adapter.onBindViewHolder(). They both have a position parameter in their method signatures:
// ListView
#Override
public View getView(int position, View convertView, ViewGroup parent) {
...
}
// RecyclerView
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
...
}
This is the list row position that has been scrolled into view, and needs to be bound to fill in the data.
Furthermore, RecyclerView.ViewHolder objects have the method getAdapterPosition(), so that you can find ViewHolder's list row position outside of the onCreateViewHolder() and onBindViewHolder() methods, for example in a click event handler.
See also:
http://www.vogella.com/tutorials/AndroidListView/article.html#adapterperformance
http://www.vogella.com/tutorials/AndroidRecyclerView/article.html
I have custom listview and array adapter with ViewHolder. When click listview item, it expands new layout below. Problem is: unfortunately it is opened for every +9th item in listview.. For example: if item 0 is clicked; 0,9,18th elements opens their expand layouts. Any idea without looking code ?
I have no idea, but this sounds familair with an issue I had. I had a listview with TextView objects and multiple were selected and got typed in the same value.
I had a very, very dirty fix for that, in my custom adapter I'd always make a new View, no matter what:
#Override
public View getView (final int position, View convertView, final ViewGroup parent) {
//if (convertView == null) {
// Inflate the view from the converter
final LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
convertView = layoutInflater.inflate(converter.getLayout(), parent, false);
//}
// Populate the view from the converter
converter.populateInflatedView(convertView, getItem(position));
return convertView;
}
Source can be found here if you want to know what the converter is about.
On a side note, I have created a sort of interface type of thing for a TreeView, however this is in Activity form which uses a ScrollView. So this is not really a ListView type of deal, but might help you. The Tree part can be found on the Tree-link, with an implementation in the subject package.
I want to set the the color for a row in my TableView but am having some difficulties. I tried
ListView.getChildAt(0).setBackgroudColor()
but it says I have no children. I do have 5 items but don't know how to say, "Set this item's background color to red" I want to do this programmatically in any method in my code.
Any suggestions?
This is probably more effort than you'd want to do, but you could make your own custom adapter and set your background for that row in the adapter.
Here is a nice tutorial that does it:
Basically in your adapter, you can override getView
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position == 0) {
view.setBackgroundColor(yourColor);
}
return view;
}
One advantage to doing this... you can do further customizations to that row to make it stand out.
I want to set the background color of a specific item in the listview.
My listview is generated by ArrayAdapter using a ArrayList.
I have a specific item in the listview that I plan to change the background color.
I know the item's position in the list.
This is my code for generating the listview.
respondMessageListView = (ListView) findViewById(R.id.respondMessageListView);
respondMessageListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, autoRespondMessages.getMessages()));
Thank you!
[edit]
According to this post, using setSelection makes no effect if is used in onCreate(), the work around is "remove the method onAttachedToWindow in PullToRefreshListView". I am not quite understanding the solution. May I ask how should I accomplish this? I am a subclass of Activity, so I cannot subclass any other class anymore.
You will have to subclass ArrayAdapter and override the getView(...) method. For simplicity's sake you could just call through to the base class implementation and set the background color for the returned View.
Edit:
The following example colors the items' backgrounds alternating black and white.
private class MyAdapter extends ArrayAdapter {
...
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
v.setBackgroundColor(position % 2 == 0 : 0xff000000, 0xffffffff);
}
}
This code is for the when you select the listitem.
Try this code...
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> myAdapter, View myView, int pos, long mylng) {
if( pos == 1) {
// to change the listview background
listview.setBackgroundColor(getResources().getColor(R.color.your_color_id));
// to change the selected item background color
myView.setBackgroundColor(getResources().getColor(R.color.your_color_id));
}
}
});
Good luck.