【发布时间】:2015-03-13 09:04:54
【问题描述】:
已解决编辑:我已经解决了这个问题。它实际上没有逻辑意义,但除了复选框之外的所有内容都需要为宽度和高度的“Match_Parent”,而不是“Wrap_Content”(看起来很奇怪)。
我的自定义 ArrayAdapter 出现在 ListView 中截断文本的问题。
一张图片胜过一千个字,所以这是问题的图片:
如您所见,第四行被截断,位于“裂缝”处。
这是自定义适配器的代码:
public class CheckListAdapter extends ArrayAdapter<String>{
private int rowPadding = (int) getContext().getResources().getDimensionPixelSize(R.dimen.adapter_mediumRowVPadding);
private int medText = (int) getContext().getResources().getDimensionPixelSize(R.dimen.medium_text);
private Map<String,Boolean> map = new HashMap<String, Boolean>();
private List<String> content;
// Strings incoming are converted to a LinkedHashSet before being used.
// This ensures that the adapter content will not accidentally change,
// and that there is only one copy of a given string per adapter.
// A LinkedHashSet is a HashSet with a predictable iteration order.
public CheckListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, new ArrayList<String>(new LinkedHashSet<String>(objects)));
}
public CheckListAdapter(Context context, List<String> objects) {
this(context, android.R.layout.simple_list_item_1, objects);
}
public CheckListAdapter(Context context, String[] objects) {
this(context, android.R.layout.simple_list_item_1, Arrays.asList(objects));
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
LinearLayout row = new LinearLayout(this.getContext());
row.setBackgroundResource(R.drawable.greenclicked_background);
row.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
final CheckBox check = new CheckBox(getContext());
check.setButtonDrawable(R.drawable.gemsgreen_btn_check_holo_light);
final TextView text = new TextView(getContext());
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.FILL);
text.setLayoutParams(params);
text.setGravity(Gravity.TOP | Gravity.LEFT);
final String item = this.getItem(position);
if(!this.map.containsKey(item)){
this.map.put(item, false);
}
else{
boolean checked = map.get(item);
check.setChecked(checked);
}
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean checked = map.get(item);
checked = !checked;
map.put(item, checked);
check.setChecked(checked);
}
});
text.setText(item);
text.setMinLines(1);
text.setMaxLines(10);
row.addView(check);
row.addView(text);
row.setPadding(0, rowPadding, 0, rowPadding);
return row;
}
public Map<String,Boolean> getResults(){
return map;
}
}
这是自定义 ListView 的代码:
public class NonScrollListView extends ListView {
public NonScrollListView(Context context) {
super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
我已经为这个问题寻找解决方案将近一个小时了,但我完全没有运气。我尝试了许多不同的方法,使用不同的重力和布局参数设置,但没有任何东西使它看起来与屏幕截图中的样子有任何不同。
【问题讨论】:
标签: android android-listview android-arrayadapter textview