Android – horizontalscrollview: the customadapter with getview () does not reuse convertviews like listview

In my past project, I implemented a "time picker carousel". It is based on horizontalscrollview. Users can select the time when scrolling this view. The time value is calculated according to the x-offset of horizontalscrollview

I wanted to share this project on GitHub, but I found some bad performance problems while cleaning up the code

Horizontalscrollview is populated with custom arrayadapter. Getview () uses holder as convertview. I think it may work as an adapter in listview, so only visible items will be destroyed and reused. Instead, all items will be rendered! (1008 in my case!) I add them myself (in the code example, #1), but even if I try to add fewer, deleting (and recycling) the old logic doesn't work, or what do I miss?

So my basic question is: what do I need to change to make my adapter like listview?

>I found this so link and tried to override the deletion function, but it was never called (somehow logical, because I just added) > there is a good pageradapter example in GitHub, but somehow I can't convert it to arrayadapter < string >

Welcome any ideas, links!

Please do not suggest using listview. We decided to use horizontalscrollview because of the callback and the fact that we already have a listview in the layout

HorizontalScrollView

InfiniteTimeScrubberHorizontalView extends horizontalscrollview{
...
public void setAdapter(Context context, TimeScrubberlistadapter mAdapter) {
    this.mAdapter = mAdapter;
    try {
        fillViewWithAdapter(mAdapter);
    } catch (ZeroChildException e) {
        e.printStackTrace();
    }
}

private void fillViewWithAdapter(TimeScrubberlistadapter mAdapter) {
    //...

    ViewGroup parent = (ViewGroup) getChildAt(0);
    parent.removeAllViews();
    for (int i = 0; i < mAdapter.getCount(); i++) {
        //#1: Here: ALL views are added
        parent.addView(mAdapter.getView(i, null, parent));   
    }
}

Adpater

public class TimeScrubberlistadapter extends ArrayAdapter<String> {

//...
private ArrayList<String> list;  //list from 0:00,0:30...23:00,23:30
final static int MAXIMUM_DAYS = 21

@Override
public int getCount() {
    return list.size() * MAXIMUM_DAYS;
}

@Override
public String getItem(int position) {
    return list.get(position % list.size());
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    RelativeLayout layout;

    if (convertView == null) {
        layout = (RelativeLayout) View.inflate(context, layoutId, null);
        holder = new Holder();
        holder.title = (TextView) layout.findViewById(R.id.epg_item_text);
        layout.setTag(holder);
    } else {
        layout = (RelativeLayout) convertView;
        view = layout;
        holder = (Holder) layout.getTag();
    }

    layout.setLayoutParams(mLP);        

    String timeValue = getItem(position);
    holder.title.setText(timeValue);            

    return layout;
}
//...
@Override
public void remove(String object) {
    //not called...some how logic, because i do not remove an item
    super.remove(object);    
}

resolvent:

I think maybe you will confuse the rendering logic between the view and the adapter. Using the adapter will not lead to the view recycling behavior. Listview itself and its parent abslistview implement the view recycling behavior. Listview needs an adapter to fill the view displayed on the screen correctly, But the logic for choosing which views to display and when and how to recycle them is not in the adapter at all

If you look at the source code of horizontalscrollview and listview, you will find that they are very different. They have different directions

Therefore, its disadvantage is that you will not be able to get the view recycling you are looking for by using horizontalscrollview or even simple descendants. If you want to view recycling, please check recyclerview

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>