Android ListView setLayoutParams导致单击第一个项目挂起

时间:2016-08-09 19:41:54

标签: android listview hang layoutparams

我正在以编程方式设置ListView中行的高度。但是,执行此操作后,单击ListView中的第一项将挂起,直到我单击其他项目。只有这样才开始运作。如果我没有设置布局参数,它工作正常。我真的很困惑,任何帮助都会很棒。这是我设置布局的地方:

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
      ViewHolder viewHolder = null;
      //Get the TextView and CheckBox of each row
      if (convertView == null) {
          LayoutInflater inflator = context.getLayoutInflater();
          convertView = inflator.inflate(R.layout.row, null);
          viewHolder = new ViewHolder();
          viewHolder.name = (TextView) convertView.findViewById(R.id.text1);
          viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.check);
          viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                  list.get(getPosition).setChecked(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
              }
          });
          convertView.setTag(viewHolder);
          convertView.setTag(R.id.check, viewHolder.checkBox);
      } else {
          viewHolder = (ViewHolder) convertView.getTag();
      }
      viewHolder.checkBox.setTag(position);

      //Set row height to height of the action bar *************CODE IN QUESTION
      final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
              new int[] { android.R.attr.actionBarSize });
      int mActionBarSize = (int) styledAttributes.getDimension(0, 0);
      styledAttributes.recycle();
      convertView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT, mActionBarSize));
      convertView.requestLayout(); //**************************CODE IN QUESTION

      //Set name and state of checkbox
      viewHolder.name.setText(list.get(position).getName());
      viewHolder.checkBox.setChecked(list.get(position).IsChecked());

      final ViewHolder holder = (ViewHolder) convertView.getTag();

      final ListView lv = (ListView) parent;

1 个答案:

答案 0 :(得分:0)

自己解决了。已将convertView.setLayoutParams更改为convertView.setMinimumHeight(mActionBarSize),但确实有效。