Android listview:有一个header的listview,如何给textview元素添加点击事件?
Android listview: there is a header of listview, how can I add a click event to a textview element?
在片段中,有一个listview,我在listview中添加了一个header。
有MainActivity.java,我添加了点击事件。 MyAdapter 是我定义的class。
MyAdapter adapter;
ListView search_history_list;
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
LinearLayout header = (LinearLayout) inflater.inflate(R.layout.search_listview_header, null);
search_history_list.addHeaderView(header,null,false);
View listheader = inflater.inflate(R.layout.search_listview_header,null);
TextView clearAll = (TextView)listheader.findViewById(R.id.clearall);
adapter = new MyAdapter(getActivity(),list);
search_history_list = (ListView)view.findViewById(R.id.search_history_list);
search_history_list.setAdapter(adapter);
clearAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.removeAll(list);
adapter.notifyDataSetChanged();
search_history_list.invalidate();
}
});
clearAll.setEnabled(true);
search_listview_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="热门搜索"
android:textSize="15sp"
android:layout_marginTop="27dp"/>
<com.shijiebang.offlinemap.commom.view.FlowLayout
android:id="@+id/search_flowLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="14dp">
</com.shijiebang.offlinemap.commom.view.FlowLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索历史"
android:textSize="15sp"
android:layout_marginTop="27dp"/>
<TextView
android:id="@+id/clearall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清除搜索历史"
android:textColor="#49A2FC"
android:layout_marginLeft="175dp"
android:layout_marginTop="28dp"
android:textSize="14sp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
</LinearLayout>
但是当我点击文本视图时,它不起作用。请帮忙。
您将 header
添加为列表视图 header。
但是在另一个视图实例中找到视图并设置 onclick listheader
所以你的 setonclick 代码将不起作用。
试试这个:View listheader = header;
希望这有帮助。
检查您的代码后,我看不到视图 "listheader" 的使用位置。所以我不确定如何点击文本视图。我建议为您的列表项(即列表的每一行)创建一个模型和布局,然后在您的自定义适配器中实例化每一行并设置点击监听器。为了提高列表的性能,您可以实现一个视图持有者。看看这个 http://www.vogella.com/tutorials/AndroidListView/article.html#adapterperformance_holder
在片段中,有一个listview,我在listview中添加了一个header。
有MainActivity.java,我添加了点击事件。 MyAdapter 是我定义的class。
MyAdapter adapter;
ListView search_history_list;
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
LinearLayout header = (LinearLayout) inflater.inflate(R.layout.search_listview_header, null);
search_history_list.addHeaderView(header,null,false);
View listheader = inflater.inflate(R.layout.search_listview_header,null);
TextView clearAll = (TextView)listheader.findViewById(R.id.clearall);
adapter = new MyAdapter(getActivity(),list);
search_history_list = (ListView)view.findViewById(R.id.search_history_list);
search_history_list.setAdapter(adapter);
clearAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.removeAll(list);
adapter.notifyDataSetChanged();
search_history_list.invalidate();
}
});
clearAll.setEnabled(true);
search_listview_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="热门搜索"
android:textSize="15sp"
android:layout_marginTop="27dp"/>
<com.shijiebang.offlinemap.commom.view.FlowLayout
android:id="@+id/search_flowLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="14dp">
</com.shijiebang.offlinemap.commom.view.FlowLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索历史"
android:textSize="15sp"
android:layout_marginTop="27dp"/>
<TextView
android:id="@+id/clearall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清除搜索历史"
android:textColor="#49A2FC"
android:layout_marginLeft="175dp"
android:layout_marginTop="28dp"
android:textSize="14sp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
</LinearLayout>
但是当我点击文本视图时,它不起作用。请帮忙。
您将 header
添加为列表视图 header。
但是在另一个视图实例中找到视图并设置 onclick listheader
所以你的 setonclick 代码将不起作用。
试试这个:View listheader = header;
希望这有帮助。
检查您的代码后,我看不到视图 "listheader" 的使用位置。所以我不确定如何点击文本视图。我建议为您的列表项(即列表的每一行)创建一个模型和布局,然后在您的自定义适配器中实例化每一行并设置点击监听器。为了提高列表的性能,您可以实现一个视图持有者。看看这个 http://www.vogella.com/tutorials/AndroidListView/article.html#adapterperformance_holder