2016-12-24 7 views
1

Я пытаюсь создать AlertDialog, который имеет много текстовых элементов (для прокрутки в необходимом) и EditText в конце для пользователей, чтобы ввести значение, которое не является включенных в список. Когда AlertDialog впервые представлен, все выглядит хорошо. Однако, когда я прокручиваю назад вверх, после прокрутки до нижних проблем.Android AlertDialog ListView с несколькими TextViews и EditText a end

Here is the bottom of the list initially presented. All good.

But here is what it looks like after I scroll to the top. Not so good.

Это класс адаптера, который я использую:

public class MultiSelectOtherDialog extends DialogFragment { 

public static final String TAG = "MultiSelectOtherDialog"; 

private ArrayList<Bean> mList = new ArrayList<>(); 

public static MultiSelectOtherDialog newInstance(String aTitle, String[] aElems) { 
    MultiSelectOtherDialog frag = new MultiSelectOtherDialog(); 
    Bundle args = new Bundle(); 
    args.putString("title", aTitle); 
    args.putStringArray("elems", aElems); 
    frag.setArguments(args); 
    return frag; 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    // length of this list determines how many items to present - need an extra for the EditText 
    // at the end for "Other" 
    int mListLen = (getArguments().getStringArray("elems").length) + 1; 
    //int mListLen = (getArguments().getStringArray("elems").length); 
    for (int i = 0; i < mListLen; i++) { 
     mList.add(new Bean()); 
    } 

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

    View view = getActivity().getLayoutInflater().inflate(R.layout.scb_listview2, null); 

    ListView listViewItems = (ListView)view.findViewById(R.id.lvScb); 
    listViewItems.setAdapter(new MultiSelectOtherAdapter()); 
    listViewItems.setOnItemClickListener(new OnItemClickListenerListViewItem()); 

    builder.setTitle(getArguments().getString("title")).setView(view); 

    AlertDialog diagFragDialog = builder.create(); 

    return diagFragDialog; 
} 

public class MultiSelectOtherAdapter extends BaseAdapter { 

    @Override 
    public int getCount() { 
     return mList.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return mList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
      holder = new ViewHolder(); 
      // needed for the "Other" EditText at the end 
      if (position < mList.size() - 1) { 
       convertView = View.inflate(getActivity(), R.layout.scb_item, null); 
       holder.tv = (TextView) convertView.findViewById(R.id.tv); 
       Log.d(TAG, "1)"); 
      } 
      else { 
       convertView = View.inflate(getActivity(), R.layout.scb_item_other, null); 
       holder.tv = (TextView) convertView.findViewById(R.id.et); 
       Log.d(TAG, "2)"); 
      } 
      holder.cb = (SmoothCheckBox) convertView.findViewById(R.id.scb); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
      Log.d(TAG, "3)"); 
     } 

     final Bean bean = mList.get(position); 
     holder.cb.setOnCheckedChangeListener(new SmoothCheckBox.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(SmoothCheckBox checkBox, boolean isChecked) { 
       bean.isChecked = isChecked; 
      } 
     }); 
     // needed for the "Other" EditText at the end 
     if (position < getArguments().getStringArray("elems").length) { 
      String text = getArguments().getStringArray("elems")[position]; 
      holder.tv.setText(text); 
      Log.d(TAG, "4)"); 
     } 
     else { 
      convertView = View.inflate(getActivity(), R.layout.scb_item_other, null); 
      holder.tv = (TextView) convertView.findViewById(R.id.et); 
      holder.cb = (SmoothCheckBox) convertView.findViewById(R.id.scb); 
      convertView.setTag(holder); 
      Log.d(TAG, "5)"); 
     } 

     holder.cb.setChecked(bean.isChecked); 
     Log.d(TAG, "6)"); 

     return convertView; 
    } 

    class ViewHolder { 
     SmoothCheckBox cb; 
     TextView tv; 
    } 
} 

public class OnItemClickListenerListViewItem implements AdapterView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     Bean bean = (Bean) parent.getAdapter().getItem(position); 
     bean.isChecked = !bean.isChecked; 
     SmoothCheckBox checkBox = (SmoothCheckBox) view.findViewById(R.id.scb); 
     checkBox.setChecked(bean.isChecked, true); 
    } 
} 

class Bean implements Serializable { 
    boolean isChecked; 
} 

}

и макеты:

AlertDialog расположение:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/lvScb" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:cacheColorHint="#0000" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:layout_above="@+id/viewLineHoriz" /> 

    <View 
     android:id="@+id/viewLineHoriz" 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:layout_marginBottom="0dp" 
     android:layout_above="@+id/bottonRowScb" 
     android:background="?android:attr/dividerVertical" /> 

    <LinearLayout 
     style="?android:attr/buttonBarStyle" 
     android:id="@+id/bottonRowScb" 
     android:gravity="center" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:measureWithLargestChild="true" 
     android:paddingTop="0dip" 
     android:orientation="horizontal" > 

     <Button 
      style="?android:attr/buttonBarButtonStyle" 
      android:id="@+id/buttonOKScb" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/ok_string" /> 

     <View 
      android:id="@+id/viewLineVert" 
      android:layout_height="fill_parent" 
      android:layout_width="1dp" 
      android:layout_marginBottom="0dp" 
      android:background="?android:attr/dividerVertical" /> 

     <Button 
      style="?android:attr/buttonBarButtonStyle" 
      android:id="@+id/buttonCancelScb" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/cancel_string" /> 
    </LinearLayout> 
</RelativeLayout> 

TextView пункт:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/tv" 
     android:layout_weight="100" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:textSize="15sp" /> 

    <cn.refactor.library.SmoothCheckBox 
     android:id="@+id/scb" 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:layout_gravity="center_vertical|right" 
     android:layout_weight="1" 
     android:layout_margin="5dp"/> 

</LinearLayout> 

И EditText вещь:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="horizontal"> 

    <EditText 
     android:id="@+id/et" 
     android:hint="@string/other_notes_string" 
     android:layout_weight="100" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:textSize="15sp" 
     android:textStyle="bold|italic"/> 

    <cn.refactor.library.SmoothCheckBox 
     android:id="@+id/scb" 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:layout_gravity="center_vertical|right" 
     android:layout_weight="1" 
     android:layout_margin="5dp"/> 

</LinearLayout> 

ответ

0

В вашем изменения адаптер это:

@Override 
public long getItemId(int position) { 
    return position; 
} 

А также в вашем GetView метод,() , введите этот код:

converView = null; 

перед:

if(convertView == null){ // your codes }

+0

Это сделал трюк. Однако это решение отрицает шаблон зрителя, но не делает ли оно всегда настройкой convertView = null? Лично у меня нет проблемы с этим, поскольку массив, создающий список, будет статичным во время компиляции и никогда не будет более чем несколькими видами, которые изначально будут видимыми. – tscloud

 Смежные вопросы

  • Нет связанных вопросов^_^