Asa*_*sad 4 android header footer divider android-listview
从我的问题的标题,我的问题很清楚.我有一个自定义listView和标题和一些项目.当然,我在所有项目之间添加了分隔线.我唯一不想要的是标题和第一项之间的分隔符.但是,下面的代码不起作用..我也想知道这一行的确切工作
list.setHeaderDividerEnabled(false);
Run Code Online (Sandbox Code Playgroud)
我搜索过并尝试了很多也访问了这个链接,但没有运气..
提前致谢.
更新!
public class ListView extends android.widget.ListView {
private OnScrollListener onScrollListener;
private Onscroll onscrollObj;
public ListView(Context context) {
super(context);
onCreate(context, null, null);
}
public ListView(Context context, AttributeSet attrs) {
super(context, attrs);
onCreate(context, attrs, null);
}
public ListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
onCreate(context, attrs, defStyle);
}
@SuppressWarnings("UnusedParameters")
private void onCreate(Context context, AttributeSet attrs, Integer defStyle) {
setListeners();
}
private void setListeners() {
super.setOnScrollListener(new OnScrollListener() {
private int oldTop;
private int oldFirstVisibleItem;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (onScrollListener != null) {
onScrollListener.onScrollStateChanged(view, scrollState);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (onScrollListener != null) {
onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
}
if (onscrollObj != null) {
onDetectedListScroll(view, firstVisibleItem);
}
}
private void onDetectedListScroll(AbsListView absListView, int firstVisibleItem) {
View view = absListView.getChildAt(0);
int top = (view == null) ? 0 : view.getTop();
if (firstVisibleItem == oldFirstVisibleItem) {
if (top > oldTop) {
onscrollObj.onUpScrolling();
} else if (top < oldTop) {
onscrollObj.onDownScrolling();
}
} else {
if (firstVisibleItem < oldFirstVisibleItem) {
onscrollObj.onUpScrolling();
} else {
onscrollObj.onDownScrolling();
}
}
oldTop = top;
oldFirstVisibleItem = firstVisibleItem;
}
});
}
@Override
public void setOnScrollListener(OnScrollListener onScrollListener) {
this.onScrollListener = onScrollListener;
}
public void setOnDetectScrollListener(Onscroll onDetectScrollListener) {
this.onscrollObj = onDetectScrollListener;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过删除所有分频器android:dividerHeight="0dp" android:divider="@null"现在为你添加一个列表视图中的每一项View与width平等match_parrent,而height平等1dp的底部
小智 7
我在xml"android:headerDividersEnabled ="false""中使用该属性,它工作正常.如果你想自定义标题和第一项之间的分隔符,我想你可以在标题布局的底部做一些事情来假装它是一个分隔符.
我的代码:
main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:headerDividersEnabled="false"
android:dividerHeight="5dp"
android:divider="@color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_list_view"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3px"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
/>
<TextView
android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
/>
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Bind(R.id.my_list_view)
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initviews();
}
private void initviews() {
View view = View.inflate(this,R.layout.headerview,null);
listView.addHeaderView(view);
SimpleAdapter adapter = new SimpleAdapter(this, getData(),
R.layout.list_item, new String[] { "img", "title", "info" },
new int[] { R.id.img, R.id.title, R.id.info });
listView.setAdapter(adapter);
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", R.mipmap.ic_launcher);
map.put("title", "php");
map.put("info", "for server");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.mipmap.ic_launcher);
map.put("title", "java");
map.put("info", "stable");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.mipmap.ic_launcher);
map.put("title", "C++");
map.put("info", "cool and hard");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.mipmap.ic_launcher);
map.put("title", "python");
map.put("info", "pretty clean");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.mipmap.ic_launcher);
map.put("title", "hello");
map.put("info", "every thing");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.mipmap.ic_launcher);
map.put("title", "world");
map.put("info", "hello world");
list.add(map);
return list;
}
}
Run Code Online (Sandbox Code Playgroud)
<ListView
android:headerDividersEnabled="false"
android:dividerHeight="1dp"
/>
or do this,
don't add a listview.headerview(view);
instead, add a header view in the xml design and do like this
<LinearLayout>
<layout header design>
<Listview>
</LinearLayout>
Don't provide space between header and Listview.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4329 次 |
| 最近记录: |