Why Android is truncating my ActionBar title?

前端 未结 13 2065
Happy的楠姐
Happy的楠姐 2021-02-13 10:28

In my app, I change the title in the ActionBar from each fragment displayed. When I first start my apps, I got a list of requests, so my title is \"My requests (20)\".

T

13条回答
  •  臣服心动
    2021-02-13 11:22

    Because ListView / GridView / RecyclerView is called notifyDataSetChanged() at the same time. For some reason, this would cause layout update, which I guess result in the chaos of the action bar title's layout update.

    Basically, you can postDelay() to update title to fix this problem, but I don't like this way.

    I decide to override the BaseAdapter.notifyDateSetChanged() and update item view by hand. It works:

    public abstract class BaseListAdapter> extends BaseAdapter {
        // data
        private List mList;
        // view holder
        private List mViewHolderList = new ArrayList<>();
    
        protected abstract void onBindViewHolder(VH holder, int position);
    
        protected abstract VH onCreateViewHolder(ViewGroup parent, int viewType);
    
        ...
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            VH viewHolder;
            if (convertView == null) {
                viewHolder = onCreateViewHolder(parent, getItemViewType(position));
                convertView = viewHolder.itemView;
                convertView.setTag(viewHolder);
                mViewHolderList.add(viewHolder);
            } else {
                //noinspection unchecked
                viewHolder = (VH) convertView.getTag();
            }
    
            viewHolder.setData(getItem(position));
            viewHolder.setPosition(position);
            onBindViewHolder(viewHolder, position);
    
            return convertView;
        }
    
        @Override
        public void notifyDataSetChanged() {
            if (mViewHolderList.size() == 0) {
                super.notifyDataSetChanged();
            } else {
                for (VH vh : mViewHolderList) {
                    onBindViewHolder(vh, vh.getPosition());
                }
            }
        }
    
    }
    
    public class BaseListViewHolder {
        public View itemView;
        private T data;
    
        private int position;
    
        public BaseListViewHolder(View itemView) {
            this.itemView = itemView;
        }
    
        @Override
        public T getData() {
            return data;
        }
    
        @Override
        public void setData(T data) {
            this.data = data;
        }
    
        public int getPosition() {
            return position;
        }
    
        public void setPosition(int position) {
            this.position = position;
        }
    }
    

    Sorry for my bad english.

提交回复
热议问题