I have many navigation drawers (mostly in Google Apps) where few of the items are having no List item seperators, while some list items are having list seperators.
I want to achieve the same functionality for my app.
can anybody please help me understand the implementation?
How can i hide list seperator for few of the list items while others have it?
regards,
Rajan
For each list item row use this layout:
<LinearLayout>
(...)
<View
android:id="#+id/viewSeparator1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#646464"/>
</LinearLayout>
Then on your adapter use this
public class DrawerListAdapter extends BaseAdapter{
(...)
public View getView(int position, View convertView, ViewGroup parent){
(...)
View mViewSeparator = convertView.findViewByID(R.id.viewSeparator1);
//I dont know when you want to show a separator so replace this line with the apropriate check: for example: if(position == 0) etc
if(hasSeparator)
mViewSeparator.setVisibility(View.VISIBLE);
else
mViewSeparator.setVisibility(View.GONE);
return convertView;
}
}
Related
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
I have a listview and I want to change the dividerHeight for specific Item.
For example I want it look like that:
Item 1
Item 2
Item 3
// Bigger space here
Item 4
Item 5
I know how to change space between each Items but how can I specify a space depending on the item position in the list.
The other possibility is to use a different drawer for some items.
Thanks a lot
PS: Sorry for my bad english, I'm french
EDIT: Here is the code where my Items are displayed on my Listview:
drawerItemsList = getResources().getStringArray(R.array.items);
myDrawer = (ListView) findViewById(R.id.my_drawer);
myDrawer.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_item, drawerItemsList));
You can customize your adapter by extending ArrayAdapter class and then do what you want in its getView method, A simple solution for your problem might be the following snippet code:
#Override
protected View getView(int position, View convertView, AdapterView parent) {
View view = convertView;
// Check 'view' value and inflate it if needed
if(position == 3) { // Gap index in the list
// Here you can add some additional bottom margin or padding to the
// 'view'.
// Or you can left this view empty like a free space
}
return view;
}
Hi I'm new to Android development and I have to create a navigation drawer with different items on it.
The first row should contain a
Button
. The next three rows should contain
TexViews
, and the last row should contain an
EditText
I'm gone as far as creating the layout but I haven't the idea of how to call that inside the adapter.
The Navigation Drawer isn't limited to a ListView. Maybe this is the reason why you want to choose one. You can use any View / ViewGroup to build your Drawer. Try a LinearLayout with some child Views of your choice. Something like this:
<LinearLayout
android:id="#+id/drawer_view"
android:layout_width="240dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start">
<Button
.../>
<TextView
.../>
</LinearLayout>
It's just an example, to show that you don't have to stick to a ListView as your Drawer.
In getView, you need to check what type the item from getItem(position) is and inflate the proper layout based on that.
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
NavDrawerItem item = getItem(position);
if (item.getType() == BUTTON_ITEM_TYPE)
{
convertView = inflater.inflate(R.layout.navdrawer_button, parent, false);
//get references to individual views, etc.
}
}
with an item interface like
public interface NavDrawerItem
{
public static final int BUTTON_ITEM_TYPE = 1;
public static final int TEXTVIEW_ITEM_TYPE = 2;
public int getId();
public String getLabel();
public int getType();
}
Is there a way to insert another View above the List in a ListView? For example, I want a Title Bar (TextView) that sits on top of the List, and scrolls out of view as the List scrolls. I can think of two ways so far of doing this, but both are hacks.
Idea #1 - Use a LinearLayout that pretends to be a ListView. But the problem is you are unable to take advantage of the "smart" loading/unloading of views that an adapter provides.
<ScrollView>
<LinearLayout>
<TextView/>
<LinearLayout
android:orientation="vertical" /> # add ListItems here
</LinearLayout>
</ScrollView>
Idea #2 - Use a hacky ArrayAdapter, with a getView method similar to this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(position == 0)
View view = inflater.inflate(R.layout.special_list_item, null);
else
View view = inflater.inflate(R.layout.regular_list_item, null);
...
return vi;
}
Simple. Just add a header view to your ListView
TextView textView = new TextView(context);
textView.setText("Hello. I'm a header view");
listView.addHeaderView(textView);
because I need to display something more than just a list in a Fragment.
So I choose Fragment rather than ListFragment, and my layout is something looks like
<linearlayout...>
<TextView...>...<TextView/>
<Button...>...<Button/>
<ListView android:id = "#"+id/mylist" ...></ListView>
</linearylayout>
And I implemnt "MyAdapter" extend BaseAdapter, which has getView like following
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if(position == 0)
{
return categroyView("Team leader");
}
else if (position == 2)
{
return categroyView("Team memebers");
}
else
{
LayoutInflater inflater = context.getLayoutInflater();
View v = inflater.inflate(R.layout.row_group, null, false);
return v;
}
}
protected View categroyView(String text)
{
TextView txtView = new TextView(context);
txtView.setText(text);
return txtView;
}
It turns out that I can receive onItemClick when its position is 0 or 2 (which as you can see I dynamically generate textView.
Meanwhile I can't receive onItemClick when its position is not 0 or 2 (which I return inflate view from XML)
I've seen some discussion about if customized row layout has some clickable item (like button), this situation will happen, but even my row layout has only one textView, it still failed to receive onItemClick.
p.s.
Also, I select Fragment rather than Activity for other other design issue.
I know I can alternatively add v.setOnClickListene in getView to help this issue, but then still the item won't highlight if I pressed on it.
What is in the position two view? If that thing might be able to take focus sit will do it instead of the list item if you don't want the inards to be clickabke then disable that it's click and it will the be passed to the item
Also are these long lists? You will run into trouble if you inflate a lot