栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

单击时创建具有可选行的ListView /更改ListView行的背景颜色

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

单击时创建具有可选行的ListView /更改ListView行的背景颜色

解决这个问题的方法非常简单。我们需要添加一个

OnItemClickListener
ListView
监听点击并做出相应的响应。

因此,在该

onCreate()
方法中,一旦确保数据集不为空,就将要重写该
onItemClick()
方法以侦听单击并更改颜色。您还将要跟踪后面的步骤中选择了哪个项目,因此请
publicint selectionId =-1;
在课程顶部添加。此外,您需要
ListAdapter
通过致电告知您已更改了某些内容
((SimpleAdapter)getListAdapter()).notifyDataSetChanged()

if(receiverList.size() != 0) {    listView.setonItemClickListener(new AdapterView.onItemClickListener() {        @Override        public void onItemClick(AdapterView<?> adapterView, View view, int index, long id) { view.setBackgroundColor(Color.RED); TextView receiverIdTextView = (TextView) view.findViewById(R.id.receiverId); selectionId = Integer.valueOf(receiverIdTextView.getText().toString()); ((SimpleAdapter) getListAdapter()).notifyDataSetChanged();         }    });    SimpleAdapter adapter = getNewAdapter();    setListAdapter(adapter);}

大!现在,我们有了一个可以更改您点击的行的颜色的工作系统。但是我们还没有完成。我们需要确保先前的选择变回正常颜色。

对于这一点,我们将使用替代的

SimpleAdapter
getView()
方法,这就是所谓的每次
ListView
进入画显示在它的项目。

它实际上只显示需要的项目-
您可以看到的项目。它不会渲染屏幕上方或下方的内容。因此,如果您在中有200个项目,则一次

ListView
只能渲染5个或6个项目,具体取决于屏幕的大小和项目的大小。

要覆盖此

getView()
方法,请转到初始化的位置,
adapter
然后将代码更改为此:

SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,receiverList, R.layout.receiver_entry, new String[] { "receiverId","receiverName", "fullPath"}, new int[] {R.id.receiverId, R.id.receiverName, R.id.fullPath}) {    @Override    public View getView (int position, View convertView, ViewGroup parent) {        View view = super.getView(position, convertView, parent);        TextView receiverIdTextView = (TextView) view.findViewById(R.id.receiverId);        if(receiverIdTextView.getText().toString().equals(String.valueOf(selectionId))) { view.setBackgroundColor(Color.RED);        } else { view.setBackgroundColor(Color.WHITE);        }        return view;    }};

每次绘制其中一行时,由于

getView()
都会调用,因此
ListView
它将检查当前
view
行是否具有所选行的ID。如果没有,它将背景色更改为白色。如果是这样,它将背景色更改为红色。

瞧!而已!现在,当您单击中的项目时,将背景色设置为红色

ListView

最终密码

MainActivity.java:

public class MainActivity extends ListActivity {    DBTools dbTools = new DBTools(this);    ArrayList<HashMap<String, String>> receiverList;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        getActionBar().hide();        setContentView(R.layout.activity_main);        receiverList = dbTools.getAllReceivers();        dbTools.close();        ListView listView = getListView();        if(receiverList.size() != 0) { listView.setonItemClickListener(new AdapterView.onItemClickListener() {     @Override     public void onItemClick(AdapterView<?> adapterView, View view, int index, long id) {         view.setBackgroundColor(Color.RED);         TextView receiverIdTextView = (TextView) view.findViewById(R.id.receiverId);         selectionId = Integer.valueOf(receiverIdTextView.getText().toString());         ((SimpleAdapter) getListAdapter()).notifyDataSetChanged();      } }); SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,receiverList, R.layout.receiver_entry, new String[] { "receiverId","receiverName", "fullPath"}, new int[] {R.id.receiverId, R.id.receiverName, R.id.fullPath}) {     @Override     public View getView (int position, View convertView, ViewGroup parent) {         View view = super.getView(position, convertView, parent);         TextView receiverIdTextView = (TextView) view.findViewById(R.id.receiverId);         if(receiverIdTextView.getText().toString().equals(String.valueOf(selectionId))) {  view.setBackgroundColor(Color.RED);         } else {  view.setBackgroundColor(Color.WHITE);         }         return view;     } }; setListAdapter(adapter);        }    }}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TableRow        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@android:color/black" >        <TextView android:id="@+id/titleTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="My List" />    </TableRow>        <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" android:id="@android:id/list" /></TableLayout>

receiver_entry.xml

<?xml version="1.0" encoding="utf-8"?><TableRow xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:id="@+id/tableRow" >    <TextView        android:id="@+id/receiverId"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:visibility="gone" />    <TextView        android:id="@+id/receiverName"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Robotronics" />    <TextView        android:id="@+id/fullPath"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="123.45.678.910:8088/robtrox/find" /></TableRow>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/486758.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号