机器人 - 如何在这种情况下,实现getListItemClick()? [英] Android - How to implement getListItemClick() in this case?

查看:149
本文介绍了机器人 - 如何在这种情况下,实现getListItemClick()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在项目上单击列表中的,我想一个TextView里面能见度从水涨船高,以看得见的变化,并显示被点击的项目的位置。这不应该从<造成的故障进行href=\"http://stackoverflow.com/questions/16022034/android-onlistitemclick-appears-to-be-acting-upon-more-than-one-list-item\">incorrectly操纵的TextView 。我应该如何实现呢?

When I click on an item from the list, I want a TextView inside it to change visibility from "gone" to "visible" and display the item position that was clicked. This should be done without the glitches that result from incorrectly manipulating the TextView. How should I implement that?

推荐答案

我不像 previous答案我LL假设你希望每个TextView中显示不同的数据,让我们的一个新成员添加到您的适配器和适当的getter和setter你的适配器:

Unlike my previous answer I'll assume that you want each TextView to display different data, so let's add a new member to your adapter and appropriate getters and setters to your Adapter:

private SparseArray<String> secondary = new SparseArray<String>();

public String getSecondary(int position) {
    return secondary.get(position, "");
}
public void setSecondary(int position, String value) {
    secondary.put(position, value);
    notifyDataSetChanged();  // Updates the ViewGroup automatically!
}

SpareArrays是在台无predictable指数较好,但如果你想要一些不同的东西,你可以使用一个List,HashMap的,等等。现在调整 onListItemClick()来使用新的方法:

protected void onListItemClick(ListView l, View v, int position, long id) {
    String clickedPosition = "Clicked position = " + position;
    mAdapter.setSecondary(position, clickedPosition);
}

最后更新 getView()显示 textClickedPosition 只有当二次有数据:

TextView textView = (TextView) view.findViewById(R.id.textPosition);
textView.setText(event);

textView = (TextView) view.findViewById(R.id.textClickedPosition);
String string = getSecondary(position);
if(!string.isEmpty()) {
    textView.setText(string);
    textView.setVisibility(View.VISIBLE);
}
else // You must hide the view otherwise you will see odd behavior for the recycle method
    textView.setVisibility(View.GONE);

return view;

在最后要注意,你的真正的应该看的谷歌I / O讲课像的涡轮增压您的UI 它们含有丰富的知识!

On a final note, you really should watch the Google I/O lectures like Turbo Charge your UI they contain a wealth of knowledge!

这篇关于机器人 - 如何在这种情况下,实现getListItemClick()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆