简体   繁体   English

在android自定义listview内扩展textview的高度

[英]Expand textview height inside android custom listview

I am having a custom list view with text view. 我有一个带有文本视图的自定义列表视图。 when text view is clicked that row height has to be increased based on the text available. 单击文本视图时,必须根据可用文本增加行高。 For example when holder.asr_tv_Content is pressed the current row where it is pressed along with other controls height has to increased 例如,当按下holder.asr_tv_Content时,当前行所在的行以及其他控件的高度必须增加

public class StudentRemarksAdapter extends BaseAdapter {

        Context _Context = null;
        ArrayList<String> _as_Day;
        ArrayList<String> _as_Month;
        ArrayList<String> _as_Year;
        ArrayList<String> _as_Heading;
        ArrayList<String> _as_Content;
        ArrayList<String> _as_Values;
        LayoutInflater inflater = null;

        public StudentRemarksAdapter(Context a_Context,
                ArrayList<String> as_Day, ArrayList<String> as_Month,
                ArrayList<String> as_Year, ArrayList<String> as_Heading,
                ArrayList<String> as_Content, ArrayList<String> as_Values) {
            // TODO Auto-generated constructor stub
            this._Context = a_Context;

            this._as_Day = as_Day;
            this._as_Month = as_Month;
            this._as_Year = as_Year;
            this._as_Heading = as_Heading;
            this._as_Content = as_Content;
            this._as_Values = as_Values;
            inflater = (LayoutInflater) _Context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return _as_Day.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public class Holder {
            RelativeLayout asr_rl_background;
            TextView asr_tv_Day, asr_tv_Month, asr_tv_Year, asr_tv_Heading,
                    asr_tv_Content;

        }

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub
            Holder holder = new Holder();
            if (convertView == null) {
                holder = new Holder();

                convertView = inflater.inflate(R.layout.adapter_studentremarks,
                        null);

                holder.asr_rl_background = (RelativeLayout) convertView
                        .findViewById(R.id.asr_rl_background);

                holder.asr_tv_Day = (TextView) convertView
                        .findViewById(R.id.asr_tv_Day);
                setControlsStyle
                        .SetCustomTextViewStyleForContent(holder.asr_tv_Day);

                holder.asr_tv_Month = (TextView) convertView
                        .findViewById(R.id.asr_tv_Month);
                setControlsStyle
                        .SetCustomTextViewStyleForContent(holder.asr_tv_Month);

                holder.asr_tv_Year = (TextView) convertView
                        .findViewById(R.id.asr_tv_Year);
                setControlsStyle
                        .SetCustomTextViewStyleForContent(holder.asr_tv_Year);

                holder.asr_tv_Heading = (TextView) convertView
                        .findViewById(R.id.asr_tv_Heading);
                setControlsStyle
                        .SetCustomTextViewStyleForContentWithBlackText(holder.asr_tv_Heading);

                holder.asr_tv_Heading.setTextSize(18);
                holder.asr_tv_Heading.setGravity(Gravity.CENTER | Gravity.LEFT);

                holder.asr_tv_Content = (TextView) convertView
                        .findViewById(R.id.asr_tv_Content);
                setControlsStyle
                        .SetCustomTextViewStyleForContentWithBlackText(holder.asr_tv_Content);
                holder.asr_tv_Month.setTextSize(14);
                holder.asr_tv_Year.setTextSize(14);

                holder.asr_tv_Content.setGravity(Gravity.TOP | Gravity.LEFT);
                convertView.setTag(holder);
            }

            else {
                holder = (Holder) convertView.getTag();
            }

            holder.asr_tv_Day.setText(_as_Day.get(position));
            holder.asr_tv_Day.setTextSize(26);

            holder.asr_tv_Month.setText(_as_Month.get(position));

            holder.asr_tv_Year.setText(_as_Year.get(position));

            holder.asr_tv_Heading.setText(_as_Heading.get(position));
            holder.asr_tv_Content.setText(_as_Content.get(position));



            if (_as_Values.get(position).equals(
                    _Context.getResources()
                            .getStringArray(R.array.as_mode_Text)[0])) {
                holder.asr_rl_background.setBackgroundColor(_Context
                        .getResources().getColor(
                                R.color.asr_tv_positiveicon_bgcolor));
            } else {
                holder.asr_rl_background.setBackgroundColor(_Context
                        .getResources().getColor(
                                R.color.asr_tv_negativeicon_bgcolor));
            }

            return convertView;
        }
    }

Set the view height to WRAP_CONTENT (and do the same for its ancestors) 将视图高度设置为WRAP_CONTENT (并对其祖先执行相同的操作)

When you show the text by default, show a small part of it 默认情况下显示文本时,仅显示其中一小部分

private static final int MAX_LEN = 22; //choose this yourself

if (text.length() > MAX_LEN) {
    textView.setText(text.substring(0, MAX_LEN) + "...");
} else {
    textView.setText(text);
}

Then onClick, update again with the full value 然后点击,再次更新完整值

textView.setText(text);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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