Java – listview header without list item separator
I'm currently writing an Android application that uses a listview with a title It works well, but it's not what I want Each item in the listview has 1-2px separators at the top and bottom So is the title – that's the problem It doesn't look very beautiful
Interestingly, system applications (such as "Settings") do not have such problems
Here is my sample adapter:
setlistadapter(new BaseAdapter() { @Override public int getCount() { return 10; } @Override public Object getItem(int i) { return i; } @Override public long getItemId(int i) { return i; } @Override public View getView(int i,View view,ViewGroup viewGroup) { View v = ((LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)) .inflate(i % 3 == 0 ? R.layout.list_header : android.R.layout.simple_list_item_1,viewGroup,false); ((TextView)v.findViewById(android.R.id.text1)).setText("test"); return v; } });
And list the title layout files:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello,World" style="?android:attr/listSeparatorTextViewStyle"> </TextView>
So the question is: how do you get rid of the item separator between the title and the regular item, like, for example, setting up the application?
Editor: after reading the answer, I want to clarify one thing I don't want to delete the delimiter completely I want to delete them only between the title item and the general item In addition, half measures such as "completely remove separators and add them to some items" do not meet my requirements
Solution
It seems that you must use custom project views for separation and some solutions Let me explain how to manage this:
>Do not use the default divider, remove it. > Create a custom layout whose bottom view is a child row of the title. > Use the top view to create a custom layout to get the separator of the item
Then, the two types of dividers will be glued and only one sub line will be made for the head part, so the dividers should have the same height to form a good sub line This will avoid having a separator above the title section, but keep the separator of the item list
So let me show you some code to implement it First, don't forget to avoid the default separator on the listview:
<ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@null" android:dividerHeight="0dp"/>
Create a project layout with the top separator set to 1DP (or other):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- this is the divider for items --> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray"/> <!-- and the rest of item's content in another ViewGroup --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/item_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="15dp" android:textColor="@android:color/white"/> </LinearLayout> </LinearLayout>
Finally, the title layout has a separator at the bottom (the same height as the separator of the project):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/item_header" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" style="?android:attr/listSeparatorTextViewStyle"/> <!-- this is the ("half-")divider for header section --> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray"/> <!-- this view above will be merged with item's dividers --> </LinearLayout>
It gives this result: