2

已解决编辑:我已经解决了这个问题。它实际上并不符合逻辑,但除复选框之外,其他所有内容都必须是“Match_Parent”,而不是“Wrap_Content”(看起来很奇怪)。TextView在ListView中被自定义ArrayAdapter中断

我有我的自定义ArrayAdapter ListView中断文本内的问题。

一张图片胜过千言万语,所以这里的问题的一个画面:

enter image description here

正如你所看到的,第四线下被切断,在“裂痕”。

这里的自定义适配器代码:

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();  
    } 
} 

,我完全没有运气。我已经尝试了许多不同的方法,使用不同的重力和布局参数设置,但是没有任何东西使它看起来与它在屏幕截图中显示的方式不同。

回答

0

我想你应该定义每个行布局的XML。 例如:row.xml

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

    <CheckBox 
     android:id="@+id/check" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" /> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" /> 

</LinearLayout> 

而且在getView()方法,你实现这样的:

public View getView(int position, View convertView, ViewGroup parent) { 

     ViewHolder holder = null; 
     LayoutInflater inflater = (LayoutInflater) activity 
       .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     // If holder not exist then locate all view from UI file. 
     if (convertView == null) { 
      // inflate UI from XML file 
      convertView = inflater.inflate(R.layout.home_page_listview_items, 
        parent, false); 
      // get all UI view 
      holder = new ViewHolder(convertView); 
      // set tag for holder 
      convertView.setTag(holder); 
     } else { 
      // if holder created, get tag from view 
      holder = (ViewHolder) convertView.getTag(); 
     } 
    //continue your code here 
} 
private static class ViewHolder { 

     private Checkbox check; 
     private TextView text; 

     public ViewHolder(View view) { 
      check = (ImageView) view.findViewById(R.id.check); 
      text = (TextView) view.findViewById(R.id.text); 
    } 
} 

希望这有助于!

+0

这完全不适用 - 与我在OP中发布的结果完全相同。 –

相关问题