鸿蒙Java开发模式4:鸿蒙 ListContainer从缓存实例中获取提升性能 原创

发布于 2021-2-9 23:41
浏览
0收藏

1.鸿蒙 ListContainer组件的性能优化,从缓存实例中获取,对应Java的内部类的语法,所以面向对象的内部类是大家必须掌握的知识点,效果如下:

鸿蒙Java开发模式4:鸿蒙 ListContainer从缓存实例中获取提升性能-开源基础软件社区

2.Java代码实现如下,Java的Ability:

package com.example.javahm2.slice;

import com.example.javahm2.ResourceTable;
import com.example.javahm2.datatwo.SettingItem;
import com.example.javahm2.datatwo.SettingProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;
import java.util.List;

public class FourAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_four);
        initView();
    }

    private void  initView()
    {
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        SettingProvider provider = new SettingProvider(getData(),this);
        listContainer.setItemProvider(provider);
    }
    private List<SettingItem> getData() {
        ArrayList<SettingItem> data = new ArrayList<>();
        for (int i = 0; i < 100; i++) {

         //通过下标加载不同的视图

            if(i%2==0)
            {
                data.add(new SettingItem(
                        ResourceTable.Media_a3,
                        "男主角" + i,
                        i % 2 == 0,
                        ResourceTable.Layout_layout_item_setting
                ));
            }
            else
            {
                data.add(new SettingItem(
                        ResourceTable.Media_a4,
                        "女主角" + i,
                        i % 2 == 0,
                        ResourceTable.Layout_layout_item_settingone
                ));
            }


        }
        return data;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

 

3.数据模型

package com.example.javahm2.datatwo;

public class SettingItem {
    private int imageId;
    private String settingName;
    private boolean isChecked;

    public int getLayout() {
        return layout;
    }

    public void setLayout(int layout) {
        this.layout = layout;
    }

    private  int  layout;

    public SettingItem(int imageId, String settingName, boolean isChecked,int  layout) {
        this.imageId = imageId;
        this.settingName = settingName;
        this.isChecked = isChecked;
        this.layout=layout;
    }

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }

    public String getSettingName() {
        return settingName;
    }

    public void setSettingName(String settingName) {
        this.settingName = settingName;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }
}

 

4.Java数据适配器实现 从缓存中获取到列表项实例后,直接使用绑定的子组件信息进行数据填充,提升执行的性能。

package com.example.javahm2.datatwo;

import com.example.javahm2.ResourceTable;
import com.example.javahm2.slice.FourAbilitySlice;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;

import java.util.List;

public class SettingProvider extends BaseItemProvider {
    //ListContainer的数据集合
    private List<SettingItem> settingList;
    private FourAbilitySlice slice;



    public SettingProvider(List<SettingItem> list, FourAbilitySlice slice) {
        this.settingList = list;
        this.slice = slice;
    }
    //用于保存列表项中的子组件信息
    public class SettingHolder {
        Image settingIma;
        Text settingText;
        Switch settingSwitch;


        SettingHolder(Component component) {
            settingIma = (Image) component.findComponentById(ResourceTable.Id_ima_setting);
            settingText = (Text) component.findComponentById(ResourceTable.Id_text_setting);
            settingSwitch = (Switch) component.findComponentById(ResourceTable.Id_switch_setting);


            settingSwitch.setTrackElement(trackElementInit(
                    new ShapeElement(slice, ResourceTable.Graphic_track_on_element),
                    new ShapeElement(slice, ResourceTable.Graphic_track_off_element)));


            settingSwitch.setThumbElement(thumbElementInit(
                    new ShapeElement(slice, ResourceTable.Graphic_thumb_on_element),
                    new ShapeElement(slice, ResourceTable.Graphic_thumb_off_element)));


        }


        private StateElement trackElementInit(ShapeElement on, ShapeElement off) {
            StateElement trackElement = new StateElement();
            trackElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, on);
            trackElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, off);
            return trackElement;
        }




        private StateElement thumbElementInit(ShapeElement on, ShapeElement off) {
            StateElement thumbElement = new StateElement();
            thumbElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, on);
            thumbElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, off);
            return thumbElement;
        }
    }


    @Override
    public int getCount() {
        return settingList == null ? 0 : settingList.size();
    }


    @Override
    public Object getItem(int position) {
        if (settingList  != null && position > 0 && position < settingList.size()){
            return settingList.get(position );
        }
        return null;
    }


    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        SettingHolder holder;
        SettingItem setting = settingList.get(position);
        if (component == null) {
            cpt = LayoutScatter.getInstance(slice).parse(setting.getLayout(), null, false);
            holder = new SettingHolder(cpt);
            //将获取到的子组件信息绑定到列表项的实例中
            cpt.setTag(holder);
        } else {
            cpt = component;
            //从缓存中获取到列表项实例后,直接使用绑定的子组件信息进行数据填充,提升执行的性能。
            holder = (SettingHolder) cpt.getTag();
        }
        holder.settingIma.setPixelMap(setting.getImageId());
        holder.settingText.setText(setting.getSettingName());
        holder.settingSwitch.setChecked(setting.isChecked());
        return cpt;
    }
}

 

5.布局文件xml :ability_four.xml

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:padding="8vp"
    ohos:orientation="vertical">
    <ListContainer
        ohos:id="$+id:list_container"
        ohos:height="match_parent"
        ohos:width="match_parent"/>
</DirectionalLayout>

 

6.ListContainer的每一项的布局文件

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="80vp"
    ohos:width="match_parent"
    ohos:padding="8vp"
    ohos:orientation="horizontal">


    <Image
        ohos:id="$+id:ima_setting"
        ohos:height="match_parent"
        ohos:width="match_parent"
        ohos:layout_alignment="vertical_center"
        ohos:weight="2">
    </Image>


    <Text
        ohos:id="$+id:text_setting"
        ohos:height="match_content"
        ohos:width="0"
        ohos:padding="4fp"
        ohos:text_size="20fp"
        ohos:start_padding="8vp"
        ohos:end_padding="8vp"
        ohos:weight="3"
        ohos:layout_alignment="vertical_center"/>


    <Switch
        ohos:id="$+id:switch_setting"
        ohos:height="20vp"
        ohos:width="0vp"
        ohos:weight="1"
        ohos:layout_alignment="vertical_center"/>


</DirectionalLayout>

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="80vp"
    ohos:width="match_parent"
    ohos:padding="8vp"
    ohos:orientation="horizontal">


    <Image
        ohos:id="$+id:ima_setting"
        ohos:height="match_parent"
        ohos:width="match_parent"
        ohos:layout_alignment="vertical_center"
        ohos:weight="2">
    </Image>


    <Text
        ohos:id="$+id:text_setting"
        ohos:height="match_content"
        ohos:width="0"
        ohos:padding="4fp"
        ohos:text_size="20fp"
        ohos:start_padding="8vp"
        ohos:end_padding="8vp"
        ohos:weight="3"
        ohos:text_color="#ff6a06"
        ohos:layout_alignment="vertical_center"/>


    <Switch
        ohos:id="$+id:switch_setting"
        ohos:height="20vp"
        ohos:width="0vp"
        ohos:weight="1"
        ohos:layout_alignment="vertical_center"/>


</DirectionalLayout>

 

7.graphic资源文件:分别为:thumb_off_element,thumb_on_element,track_off_element,,track_on_element

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
       ohos:shape="oval">
    <solid
        ohos:color="#FFFFFF"/>
    <bounds
        ohos:top="0"
        ohos:left="0"
        ohos:right="20vp"
        ohos:bottom="20vp"/>
</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
       ohos:shape="oval">
    <solid
        ohos:color="#1E90FF"/>
    <bounds
        ohos:top="0"
        ohos:left="0"
        ohos:right="20vp"
        ohos:bottom="20vp"/>
</shape>


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
       ohos:shape="rectangle">
    <solid
        ohos:color="#808080"/>
    <corners
        ohos:radius="20vp"/>
</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
       ohos:shape="rectangle">
    <solid
        ohos:color="#87CEFA"/>
    <corners
        ohos:radius="20vp"/>
</shape>

 

总结:在鸿蒙Java的编程技能中,大家需要掌握面向对象,XML,多线程,网络,数据结构和集合,JSON,RPC,这是第一步。

不着急,一步步来,鸿蒙是全场景分布式,未来可期!

 

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
标签
收藏
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐