Android listView recycling cells with variable-sized cells

I have a listView. Cells have a variable height. This causes a problem when processing cells - recycled cells may have the wrong height. Disabling recycling makes scrolling too volatile. How can I get the correct heights when reusing cells?

EDIT: heights change everywhere. There is no short list of possible values.

+4
source share
2 answers

TYPE TWO CELLS

getView. , , . , , .

. / .

CustomAdapter

private final class CustomArrayAdapter extends ArrayAdapter<String> {
    //the fastest is to get the layout inflater once and not each time getView is called
    private LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    public CustomArrayAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
        super(context, resource, textViewResourceId, objects);
    }

    ...

    //here depending on the item position determine if cell is of type 1 or 2
    @Override
    public int getItemViewType(int position) {
        if (/* your type 1 criteria is match */) {
            return TYPE_1;
        } else {
            return TYPE_2;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
                case TYPE_1:
                    convertView = mInflater.inflate(R.layout.row_of_type_1, parent, false);
                    holder.text = (TextView) convertView.findViewById(R.id.textview_type_1);
                    break;
                case TYPE_2:
                    convertView = mInflater.inflate(R.layout.row_of_type_2, parent, false);
                    holder.text = (TextView) convertView.findViewById(R.id.textview_type_2);
                    break;
             }
             convertView.setTag(holder);
         } else {
             holder = (ViewHolder) convertView.getTag();
         }

         //get the text for that specific cell and set it
         holder.text.setText(getMyCustomTextForThisCell());

         return convertView;
    }
}

static class ViewHolder {     
    TextView text;
}

, , , CustomAdapter

private final class CustomArrayAdapter extends ArrayAdapter<String> {
    //the fastest is to get the layout inflater once and not each time getView is called
    private LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    public CustomArrayAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
        super(context, resource, textViewResourceId, objects);
    }

    ...

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.row_type, parent, false);
            holder.text = (TextView) convertView.findViewById(R.id.textview_in_your_row);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //get the text for that specific cell and set it
        holder.text.setText(getMyCustomTextForThisCell());

        return convertView;
    }
}

static class ViewHolder {     
    TextView text;
}

CELL LAYOUT

, , row_type.xml( ) :

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textview_in_your_row"
    android:paddingLeft="6dp"
    android:paddingRight="6dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

android:layout_height="wrap_content",


, , - recycle , , EditText

EditText, Android ListView,

0

Xamarin.

public override View GetView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        int type = GetItemViewType(position);
        if (convertView == null)
        {
            holder = new ViewHolder();
            convertView = context.LayoutInflater.Inflate(Resource.Layout.my_row_layout, parent, false);
            holder.myTextView = FindViewById<TextView>(Resource.Id.viewRow);
            convertView.Tag = holder;
        }
        else 
        {
            holder = (ViewHolder)convertView.Tag;
            int heightWeWant = heights[type];
            if (holder.myTextView.Height != heightWeWant) //height we have isn't what we want
            {
                AbsListView.LayoutParams parms = new AbsListView.LayoutParams(LinearLayout.LayoutParams.MatchParent, heightWeWant); //Width, Height //resize the view to the height we want
                convertView.LayoutParameters = parms;
            }
            convertView.Tag = holder;
        }

        holder.myText = "Some text here";

        return convertView;
    }
0

Source: https://habr.com/ru/post/1523554/


All Articles