我正在尝试创建一个AlertDialog,其中包含许多TextView(以便必要时滚动)和最后的EditText,供用户输入未包含在列表中的值。当AlertDialog首次出现时,事情看起来很好。但是,当我滚动到底部后滚动到顶部时出现问题。
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.
这是我正在使用的Adapter类:
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 :(得分:0)
在你的适配器中更改:
@Override
public long getItemId(int position) {
return position;
}
而且,在你的getView()方法中,输入以下代码:
converView = null;
之前:
if(convertView == null){
// your codes
}