简体   繁体   English

Android:列出项目的选择性“可点击性”

[英]Android: List Item selective “clickability”

I'm trying to make a ListView in Android where some items have details and some items do not. 我正在尝试在Android中创建一个ListView ,其中某些项目包含详细信息,而某些项目则没有。 For those items that have details, I want them to be clickable and for a click on them to present a new fragment. 对于那些具有详细信息的项目,我希望它们可以单击,并单击它们以显示新的片段。 For those items that have no details, I want to hide an ImageView and set the list item to be "unclickable". 对于那些没有详细信息的项目,我想隐藏一个ImageView并将列表项设置为“不可点击”。

I'm trying to accomplish this in the following way (this is in my @Override getView method in my ArrayAdapter class): 我正在尝试通过以下方式完成此操作(这在ArrayAdapter类的@Override getView方法中):

ParseGeoPoint location  = itemScan.getParseGeoPoint("location");

ImageView chevron       = (ImageView)convertView.findViewById(R.id.chevron_image_view);

if (location == null) {
    subtitleLabel.setText("{Location Data Unavailable}");
    chevron.setVisibility(View.GONE);
    convertView.setClickable(false);

} else {
    subtitleLabel.setText(""); <-- Not important
    chevron.setVisibility(View.VISIBLE);
    convertView.setClickable(true);

}   

Here's my list view's on item click listener: 这是我的列表视图的项目单击侦听器:

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // This may be unnecessary when I get this 
        // issue solved, but I put this in because 
        // of the odd behavior described below
        if (view.isClickable() == true) {
            // Push to a new fragment
        }
    }
});

Part of this code works as expected and part does not. 此代码的一部分按预期方式工作,而一部分则不然。 The list view items have their labels populated correctly. 列表视图项的标签正确填充。 The list view items have the "disclosure chevron" shown or hidden as expected. 列表视图项目具有“显示人字形”显示或隐藏,如预期的那样。

Weirdly, though, the click-ability of the views seems to be inverted from my expectations. 但是,奇怪的是,视图的可单击性似乎与我的预期相反。 The "good" items (the ones with details) don't respond to clicks/taps in any way. “好”项目(带有详细信息的项目)不会以任何方式响应点击/轻击。 The "bad" items (the ones without details) have the audible "click" sound played, but the if statement in my OnItemClickListener() prevents the code from firing. “不良”项目(没有详细信息的项目)播放了可听见的“ OnItemClickListener()哒”声,但OnItemClickListener()if语句阻止代码触发。 So, my list view is rendered nonfunctional. 因此,我的列表视图无法正常工作。

Have I misunderstood somehow what is the intended functionality of setClickable(boolean) ? 我是否以某种方式误解了setClickable(boolean)的预期功能? I got this idea from this answer . 我从这个答案中得到了这个主意。

Im guessing that your using a ArrayList to store your data you could do this 我猜测您使用ArrayList存储数据可以做到这一点

 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            ArrayList Object Type itemScan = arrayList.get(position);
            ParseGeoPoint location  = itemScan.getParseGeoPoint("location");

            if (location == null) {
                  return;

           } else {
              // Push to a new fragment
           }   
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM