Disable listview items with selector and isEnabled()

前端 未结 2 910
无人共我
无人共我 2021-01-13 17:25

Scope: - using overriden ArrayAdapter; - using selector; - using isEnabled for disabling items.

Aim: - disable some list items and

相关标签:
2条回答
  • 2021-01-13 17:44

    The function isEnabled() in the adapter only makes item unfocusable and unclickable. You need to call view.setEnabled() at the end of adapter.getView() to make your selector functional.

    Also, for a parent view to pass enable state to its descendants, you need to specify the attribute android:duplicateParentState="true" to the child views in your xml file.

    0 讨论(0)
  • 2021-01-13 18:06

    Hack: use getView to check disabled item logic and inflate view with another layout. isEnabled is still usefull.

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row;
    
        if (mListItem[position].isEnabled() == false) row = inflater.inflate(
                R.layout.list_row_disabled, null);
        else {
            row = inflater.inflate(R.layout.list_row, null);
            // set right extensible icon
            if (mListItem[position].getType()) {
                ImageView ic_arrow = (ImageView) row.findViewById(R.id.list_row_arrow);
                ic_arrow.setImageResource(R.drawable.ic_arrow_right);
            }
        }
        // set left icon
        ImageView ic_item = (ImageView) row.findViewById(R.id.list_row_icon);
        ic_item.setImageResource(mListItem[position].getIcon());
        // blend icon if item is disabled
        if (mListItem[position].isEnabled() == false) 
            ic_item.setColorFilter(0x99D0D0D0,Mode.SRC_ATOP); // make icons look grayer 
    
        // set title text
        TextView txvTitle = (TextView) row.findViewById(R.id.list_row_title);
        txvTitle.setText(mListItem[position].getTitle());
    
        return row;
    }
    
    @Override
    public boolean isEnabled(int position) {
        return mListItem[position].isEnabled();
    }
    
    0 讨论(0)
提交回复
热议问题