i like to implement a layout for a listview like this:
Any idea how i can implement this?
Thanks
You can do it in a couple of ways, but I think the most convenient way is to create two 9-patch background images so that you can define which sides should scale in what way. (Check here for information about 9-patch)
Make sure that the background image has a transparent part, where you want the one list item to overlap with the other, and set the layout_marginTop of the second item negative on an amount that gives you the desired effect.
This Approach must Help,Using two Different View and Displaying One at one Time based on Position
To implement listView elements as the screenshot, you need to have a CustomListViewAdapter (extends ArrayAdapter ) and in your CustomListViewAdapter class you need to specify in the method getView() the background to use in each element as below:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
if ((position % 2) == 0) {
rowView.setBackgroundResource(R.drawable.trapeze_down);
} else {
rowView.setBackgroundResource(R.drawable.trapeze_up);
}
rowView.refreshDrawableState();
return rowView;
}
Note:
* rowLayout represent the layout of an element in the listView
* trapeze_down and trapeze_up are two png images representing the trapeze form you want to have in background
* when you create your list view, don't forget to specify :
android:divider="#null"
android:dividerHeight="0dp"
*And finally you just set the adapter to your listView
Related
I know how to add a header or a footer in JAVA, but I was wondering if I could add it straight in the XML.
I wouldn't want to simulate this, but really add it as footer or header!
No, I don't think that it is possible. Based on ListView source code there are only overScrollHeader/overScrollFooter are available from XML attributes. But these attributes accept only drawables.
If you don't want to use tricks with layouts above/below ListView. You can extend ListView and implement your own footer and header support in customized View. It is not so hard because of footer and header are already implemented. You only have to add XML attributes parsing in your customized View's constructor.
I was just trying to achieve the same thing (to keep my code cleaner and use XML for markup & source code for logic), but the only solution I found is to define the header view with XML somewhere in your layout and then detach it and put into ListView as header.
For example, having this XML:
<ListView android:id="#+id/myListView">
</ListView>
<LinearLayout android:id="#+id/myHeader">
....
</LinearLayout>
You can do this in your code:
ListView myListView = (ListView) findViewById(R.id.myListView);
LinearLayout myHeader = (LinearLayout) findViewById(R.id.myHeader);
// Let's remove the myHeader view from it's current child...
((ViewGroup) myHeader.getParent()).removeView(myHeader);
// ... and put it inside ListView.
myListView.addFooterView(myHeader);
Basically, what we do here is just detach the inflated LinearLayout from its parent and set it as ListView header child.
This is not an ideal solution, but it is still easier than creating/inflating header manually. Also this utilizes the power of XML inflation & view reusing if you're using this inside some "holder" pattern.
Hope this helps somebody.
This is how it worked for me, in my Adapter class which extends the BaseAdapter. I am targeting API 23:
#Override
public View getView(int position, View view, ViewGroup parent) {
if (position == 0) {
if (view == null) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
view = layoutInflater.inflate(R.layout.test_results_header, parent, false);
}
} else {
if (view == null) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
view = layoutInflater.inflate(R.layout.test_result_item, parent, false);
}
}
Pretty simple, I inflate a header XML for position 0 and the content XML for the rest. If you know the position where you want a header or any other XML, in your logic you would need to check the position, and inflate the respective XML for that position.
I created an xml resource same as my adapter rows xml (so the title is fit) and added it to the listview after addind the adapter:
listView.setAdapter(myRowsAdapter);
listView.addHeaderView(View.inflate(getContext(), R.layout.title_row, null));
I'm trying to change the background color of my GridView. This works perfectly with this code:
GridView gv = (GridView) findViewById(R.id.gvSpeelveld);
gv.setBackgroundColor(Color.RED);
But now I want to change the color of different cells. For example: Row 2 cell 2 should be blue.
What method should I use to get the GridView item on a specific position to change the color?
I tried with these methods, but it didn't work out well
//Attempt 1
gv.getChildAt(1).setBackgroundColor(Color.BLUE);
//Attempt 2 (returns data, not the whole object)
gv.getItemAtPosition(5);
So how can I the content of different cells?
It is not a good practice to set the background of a particular list/grid item after execution of the adapter. The best way is to set it in the adapter itself by identifying position.e.g.
class YourAdapter extends BaseAdapter<T>{ ....
getView(int position, View convertView, ViewGroup parent){
View v = convertView;
......
if(position == your_postion){
v.setbackground(Color.parseColor("#FF0000");
}
}
}
My ListView is in the background (using a relative layout).
I have a textbox which is used as a view in ListView, which is set to different background color for different lines. This is done using the following code in adapter.
public View getView(int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
if (rowView == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
rowView = inflater.inflate(R.layout.listview_background, null);
rowView.setTag(new ViewHolder((TextView) rowView.findViewById(R.id.listBackgroundTV)));
}
((ViewHolder)rowView.getTag()).tv.setHeight((position != 0)?((CompareTextBox)((Activity)context).findViewById(R.id.ctb1)).getLineHeight():((CompareTextBox)((Activity)context).findViewById(R.id.ctb1)).getLineHeight() + (int) (context.getResources().getDisplayMetrics().density + 0.5f));
((ViewHolder)rowView.getTag()).tv.setBackgroundColor((b[position])?0xff000000:0xffffffff);
return rowView;
}
So you see the textbox background color is set using the position.
Now I use ListView.scrollTo(x,y) function to scroll the ListView upwards.
Now, as the background of the textbox is calculated from the position, when the ListView is scrolled upwards, the correct color of the textbox should be displayed for the new rows displayed. But this is not happening. All the items in ListView exposed due to scrolling upwards have same black background.
Any idea, how the ListView view can be refreshed/redrawn, or any issue spotted in the above code?
You can refresh the ListView with a call to the notifyDataSetChanged() method of your adapter. You could try to call it after scrollTo, even though that should usually be called if the underlying dataset changed to refresh the view items.
How to do my own custom list? I mean, that each element of list will be looking like I want.
Create a custom list item row layout
You have to create a custom list row item in the layout folder, just like you define the usual activity layouts. There you place your icons, TextViews etc and place them properly.
Override the specific adapter you need
You then need to override the specific adapter you need in order to associate the data from your curso / object list with your layout xml element. This is usually done by overriding the getViewor bindView method of the adapter of your choice (ResourceCursorAdapter, ArrayAdapter,..).
#Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = mInflater.inflate(R.layout.row_item, parent, false);
}
TextView someTextViewOnMyRowLayout = (TextView)findViewById(...);
someTextViewOnMyRowLayout.setText(...);
return convertView;
}
You can create an xml file which acts as an element that looks like you want..
and assign that to the list using inflators and adapters..
Try this..
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/ ,
http://www.androidpeople.com/android-custom-listview-tutorial-example/
I have a ListView powered by a custom adapter that dynamically changes the background color of each row based on its contents. The getView looks like this:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
Power power = powers.get(position);
if(row == null)
{
row = getLayoutInflater().inflate(android.R.layout.simple_list_item_2, parent, false);
}
TextView text1 = (TextView)row.findViewById(android.R.id.text1);
TextView text2 = (TextView)row.findViewById(android.R.id.text2);
text1.setText(power.name);
text2.setText(power.getAttackLine());
row.setBackgroundColor(0xFF0000FF);
return row;
}
All that works peachy, except that whenever I select an item from the list, the standard red/orange highlight only shows up behind the row, only visible for a few pixels on each side of it. Is there a way to get the selection to show up on top of the background?
Try android:drawSelectorOnTop="true" in the <ListView> element in your layout.
I think the problem is that you're using simple_list_item_2, which has some padding/margin built into it. I recommend making your own version of simple_list_item_2, wherein you have both the height and the width set to fill_parent. You can see the latest simple_list_item_2 here.