简体   繁体   中英

TextView being cut off in Custom ArrayAdapter inside ListView

SOLVED EDIT: I have fixed the issue. It does not actually make logical sense, but everything other than the checkbox needed to be 'Match_Parent' for both width and height, not 'Wrap_Content' (as strange as that seems).

I am having issues with my custom ArrayAdapter inside of a ListView cutting off text.

A picture is worth a thousand words, so here's a picture of the issue:

在此处输入图片说明

As you can see, the fourth line down is being cut off, on 'cracks'.

Here's the code for the custom adapter:

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

}

Here's the code for the custom 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();    
    }
}

I have been looking for a solution for this issue for almost an hour now, and I have had absolutely no luck. I have tried numerous different approaches using different gravity and layout param settings, but nothing has made it look any different to the way it looks in the screenshot.

I think you should define each row layout by xml. For example: 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>

And in getView() method, you implements like this:

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

Hope this help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM