面向ViewHolder编程

本文介绍了一种通过面向ViewHolder编程来优化界面开发效率的方法。通过将界面分块为多个ViewHolder,并在主界面中调用相关控件addView()方法来填充视图,可以有效地提升大型界面的开发速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在一个界面有诸多元素,一个人无法完成该界面的实现,而是要交给多人分工完成。这种情况下就要面向ViewHolder编程。

面向ViewHolder编程的思想

将界面分块,分成各个ViewHolder,主界面以模糊的控件架子搭建,只确定相对位置,不填充内容;而在各个ViewHolder类中编写详细视图代码;然后在主界面Activity中调用相关控件addView()方法填空视图。

面向ViewHolder编程的例子
  • 实现如图所示效果

| 第一步:模糊的控件架子搭建主界面,确定相对位置
  • Activity布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/bottom_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="@drawable/detail_bottom_bg" >
    </FrameLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_layout"
        android:layout_marginBottom="5dp"
        android:fillViewport="true" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <FrameLayout
                android:id="@+id/detail_info"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:background="@drawable/list_item_bg_normal" />

            <FrameLayout
                android:id="@+id/detail_safe"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/detail_info"
                android:background="@drawable/list_item_bg_normal" />

            <HorizontalScrollView
                android:id="@+id/detail_screen"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/detail_safe"
                android:background="@color/detail_screen_bg"
                android:fillViewport="true"
                android:padding="5dp"
                android:scrollbars="none" />

            <FrameLayout
                android:id="@+id/detail_des"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/detail_screen"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:background="@drawable/list_item_bg_normal" />
        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

从XML中看到几乎都是FrameLayout,属性基本只设置了宽高度和默认背景图。

| 第二步:新建各个ViewHolder类,编写holder布局和代码

只以detail_screen的HorizontalScrollView填充的应用截图部分为例展示ViewHolder的作用

  • holder布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/screen_1"
        android:layout_width="@dimen/screen_iv_width"
        android:layout_height="@dimen/screen_iv_height"
        android:paddingRight="5dp"
        android:scaleType="fitXY"
        android:src="@drawable/ic_default" />

    <ImageView
        android:id="@+id/screen_2"
        android:layout_width="@dimen/screen_iv_width"
        android:layout_height="@dimen/screen_iv_height"
        android:layout_toRightOf="@id/screen_1"
        android:paddingRight="5dp"
        android:scaleType="fitXY"
        android:src="@drawable/ic_default" />

    <ImageView
        android:id="@+id/screen_3"
        android:layout_width="@dimen/screen_iv_width"
        android:layout_height="@dimen/screen_iv_height"
        android:layout_toRightOf="@id/screen_2"
        android:paddingRight="5dp"
        android:scaleType="fitXY"
        android:src="@drawable/ic_default" />

    <ImageView
        android:id="@+id/screen_4"
        android:layout_width="@dimen/screen_iv_width"
        android:layout_height="@dimen/screen_iv_height"
        android:layout_toRightOf="@id/screen_3"
        android:paddingRight="5dp"
        android:scaleType="fitXY"
        android:src="@drawable/ic_default" />

    <ImageView
        android:id="@+id/screen_5"
        android:layout_width="@dimen/screen_iv_width"
        android:layout_height="@dimen/screen_iv_height"
        android:layout_toRightOf="@id/screen_4"
        android:paddingRight="5dp"
        android:scaleType="fitXY"
        android:src="@drawable/ic_default" />

</RelativeLayout>

就是照片上的几个应用截图ImageView

  • holder代码
    实例化控件和显示
public class DetailScreenHolder extends BaseHolder<AppInfo> {

    private ImageView[] ivs;
    @Override
    public View initView() {
        View view= View.inflate(UIutil.getContext(),R.layout.detail_screen,null);
        ivs=new ImageView[5];
        ivs[0]=(ImageView) view.findViewById(R.id.screen_1);
        ivs[1]=(ImageView) view.findViewById(R.id.screen_2);
        ivs[2]=(ImageView) view.findViewById(R.id.screen_3);
        ivs[3]=(ImageView) view.findViewById(R.id.screen_4);
        ivs[4]=(ImageView) view.findViewById(R.id.screen_5);
        return view;
    }

    @Override
    public void refreshView(AppInfo data) {
        List<String> screen = data.getScreen(); // 集合的大小有可能小于5
        for(int i=0;i<5;i++){
            if(i<screen.size()){
                ivs[i].setVisibility(View.VISIBLE);
                bitmapUtils.display(ivs[i], GetHttp.URI+"image?name="+screen.get(i));
                System.out.println(screen.get(i));
            }else{
                ivs[i].setVisibility(View.GONE);
            }

        }
    }
}
| 第三步:在主界面Activity中填充ViewHolder写好的详细视图
  • 定义控件

    private HorizontalScrollView detail_screen;

  • 初始化控件并填充视图

detail_screen = (HorizontalScrollView) view.findViewById(R.id.detail_screen);

DetailScreenHolder screenHolder = new DetailScreenHolder();//实例化holder
screenHolder.setData(load);//传入数据内容
/**填充视图*/
detail_screen.addView(screenHolder.getContentView());

DetailScreenHolder继承自BaseHolder,BaseHolder代码详见http://blog.youkuaiyun.com/harrain/article/details/53439441 点开目录即可见到。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值