zoukankan      html  css  js  c++  java
  • 碎片最佳实践-简易版的新闻应用

    先上成果图

    <*m* src="http://ww4.s**a*m*.c*/lar*e/85184d39*w1ez1dyb*mszj20ec0*fq4b.jp*" alt="小屏幕" />小屏幕

    <*m* src="http://ww3.s**a*m*.c*/lar*e/85184d39*w1ez1e1sf9boj20e70m*76*.jp*" alt="点击后" />点击后
    <*m* src="http://ww1.s**a*m*.c*/lar*e/85184d39*w1ez1e2eca23j20*70d40t*.jp*" alt="大屏幕Nexus7" />大屏幕Nexus7
    <*m* src="http://ww1.s**a*m*.c*/lar*e/85184d39*w1ez1e3asyf9j20*e0ef**8.jp*" alt="点击后" />点击后

    主体思想

    写两个act*v*ty_ma**布局文件,一个在layout文件夹下,另一个置于layout-sw600dp下。前者只有一个t*tleFra*me*t碎片,后者还会加上co*te*tFra*me*t碎片,系统根据设备大小加载不同的act*v*ty_ma**。但是也由于两种布局下都包含t*tleFra*me*t碎片,在点击L*stV*ew时,面临两种选择。其一,若为单页模式,就跳转到包含内容布局的活动。若为双页,获取右碎片实例(*etFra*me*tMa*a*er.f**dFra*me*tById),refresh下即可。至于如何判断当前处于单页还是双页,两个act*v*ty_ma**不同的就是大屏幕的多了一个右侧碎片,给这个碎片加上*d,在t*tleFra*me*t中的o*Act*v*tyCreated(活动加载完成才执行的函数) 用*etAct*v*ty().f**dV*ewById(右侧碎片的*d)来判断。*etAct*v*ty是获取与该碎片相关联的活动

    遇到的问题

      继承自a*dro*d.app.Fra*me*t的碎片类,里面的o*Attach(Co*text co*text) 在A*I23中
      不会被调用,导致里面的初始化操作不会执行

    解决办法: 将o*Attach()中代码转移进o*CreateV*ew()

      代码无异常,运行时抛出Act*v*tyNotFou*d异常

    解决办法:在A*dro*dMa**fest下注册活动

    上代码

    代码结构

    <*m* src="http://ww3.s**a*m*.c*/lar*e/85184d39*w1ez1f*2ousmj208y097*mt.jp*" alt="结构" />结构

    News.class

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    publ*c class News {

    pr*vate Str*** t*tle;
    pr*vate Str*** co*te*t;

    publ*c vo*d setT*tle(Str*** t*tle) {
    th*s.t*tle = t*tle;
    }

    publ*c vo*d setCo*te*t(Str*** co*te*t) {
    th*s.co*te*t = co*te*t;
    }

    publ*c Str*** *etT*tle() {
    retur* t*tle;
    }

    publ*c Str*** *etCo*te*t() {
    retur* co*te*t;
    }
    }

    NewsAdapter.class

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    publ*c class NewsAdapter exte*ds ArrayAdapter<News&*t;{

    pr*vate **t resourceId;

    pr*vate V*ew v*ew;

    pr*vate V*ewHolder v*ewHolder;

    publ*c NewsAdapter(Co*text co*text,**t textV*ewResourceId, L*st<News&*t; objects) {
    super(co*text, textV*ewResourceId, objects);
    resourceId = textV*ewResourceId;
    }

    @Overr*de
    publ*c V*ew *etV*ew(**t pos*t*o*, V*ew co*vertV*ew, V*ewGroup pare*t) {
    News *ews = *etItem(pos*t*o*);
    *f(co*vertV*ew == *ull){
    v*ew = LayoutI*flater.from(*etCo*text()).**flate(resourceId,*ull);
    v*ewHolder = *ew V*ewHolder();
    v*ewHolder.t*tleTextV*ew = (TextV*ew) v*ew.f**dV*ewById(R.*d.t*tle_*tem_tv);
    v*ew.setTa*(v*ewHolder);
    } else {
    v*ew = co*vertV*ew;
    v*ewHolder = (V*ewHolder) v*ew.*etTa*();
    }

    v*ewHolder.t*tleTextV*ew.setText(*ews.*etT*tle());
    retur* v*ew;
    }

    class V*ewHolder{
    TextV*ew t*tleTextV*ew;
    }
    }

    NewsCo*te*tFra*me*t.class

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    publ*c class NewsCo*te*tFra*me*t exte*ds Fra*me*t{

    pr*vate V*ew v*ew;
    @Nullable
    @Overr*de
    publ*c V*ew o*CreateV*ew(LayoutI*flater **flater, V*ewGroup co*ta**er, Bu*dle savedI*sta*ceState) {
    v*ew = **flater.**flate(R.layout.co*te*t_fra*,co*ta**er,false);
    retur* v*ew;
    }

    publ*c vo*d refresh(Str*** t*tle, Str*** co*te*t){
    L**earLayout l**earLayout = (L**earLayout) v*ew.f**dV*ewById(R.*d.co*te*t);
    l**earLayout.setV*s*b*l*ty(V*ew.VISIBLE);
    TextV*ew t*tle_tv = (TextV*ew) v*ew.f**dV*ewById(R.*d.tv_t*tle);
    TextV*ew co*te*t_tv = (TextV*ew) v*ew.f**dV*ewById(R.*d.tv_co*te*t);
    t*tle_tv.setText(t*tle);
    co*te*t_tv.setText(co*te*t);
    }

    }

    NewsT*tleFra*me*t.class

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    publ*c class NewsT*tleFra*me*t exte*ds Fra*me*t *mpleme*ts AdapterV*ew.O*ItemCl*ckL*ste*er{

    pr*vate L*st<News&*t; *ewsL*st = *ew ArrayL*st<News&*t;();

    pr*vate Boolea* two*a*e;
    @Nullable
    @Overr*de
    publ*c V*ew o*CreateV*ew(LayoutI*flater **flater, V*ewGroup co*ta**er, Bu*dle savedI*sta*ceState) {
    ***tData();
    NewsAdapter adapter = *ew NewsAdapter(*etAct*v*ty(),R.layout.t*tle_*tem,*ewsL*st);
    V*ew v*ew = **flater.**flate(R.layout.*ewst*tle_fra*,co*ta**er,false);
    L*stV*ew NewsT*tleL*stV*ew = (L*stV*ew) v*ew.f**dV*ewById(R.*d.t*tle_l*st_v*ew);
    NewsT*tleL*stV*ew.setAdapter(adapter);
    NewsT*tleL*stV*ew.setO*ItemCl*ckL*ste*er(th*s);
    retur* v*ew;
    }

    publ*c vo*d ***tData(){
    News *ews1 = *ew News();
    *ews1.setT*tle("Goo*le w*ll accept Gaby to be the CEO");
    *ews1.setCo*te*t("To reduce hous*** **ve*tor*es, more m**ra*t rural workers should be *ssued w*th urba*-res*de*cy perm*ts, wh*ch w*ll allow them to purchase hous*** ** c*t*es, accord*** to a stateme*t released after a meet*** of the *ol*t*cal Bureau of the C*C Ce*tral Comm*ttee pres*ded over by *res*de*t ** ***p***.");
    *ewsL*st.add(*ews1);

    News *ews2 = *ew News();
    *ews2.setT*tle("H**h-tech products at L**ht of the I*ter*et Expo Ba*du 'self-dr*v***' cars to h*t roads ** 3 years");
    *ews2.setCo*te*t("Ba*du **d*cated that the *ew bus**ess u**t w*ll focus o* research a*d developme*t of self-dr*v*** tech*olo*y as well as bu*ld the **dustr*al cha** to support the product*o* of *ext-*e*erat*o* cars.");
    *ewsL*st.add(*ews2);

    News *ews3 = *ew News();
    *ews3.setCo*te*t("I* the future, veh*cles could be self-dr*ve* a*d co*trolled by smart systems. We have set a tar*et to commerc*al*ze our operat*o*s ** the *ext three years," he sa*d, add*** that ** 10 years, about 80 perce*t of the *ewly produced veh*cles would be equ*pped w*th self-dr*v*** tech*olo**es");
    *ews3.setT*tle("Reaso*able eco*omom*c *rowth rate tar*eted");
    *ewsL*st.add(*ews3);
    }

    @Overr*de
    publ*c vo*d o*Act*v*tyCreated(Bu*dle savedI*sta*ceState) {
    super.o*Act*v*tyCreated(savedI*sta*ceState);
    *f(*etAct*v*ty().f**dV*ewById(R.*d.co*te*t_fra*_layout) == *ull){
    two*a*e = false;
    } else{
    two*a*e = true;
    }
    }

    @Overr*de
    publ*c vo*d o*ItemCl*ck(AdapterV*ew<?&*t; pare*t, V*ew v*ew, **t pos*t*o*, lo** *d) {
    News *ews = *ewsL*st.*et(pos*t*o*);
    *f(two*a*e){
    NewsCo*te*tFra*me*t *ewsCo*te*tFra*me*t = (NewsCo*te*tFra*me*t) *etFra*me*tMa*a*er().f**dFra*me*tById(R.*d.co*te*t_fra*_layout);
    *ewsCo*te*tFra*me*t.refresh(*ews.*etT*tle(), *ews.*etCo*te*t());
    } else{
    co*te*tAct*v*ty.act*o*Start(*etAct*v*ty(),*ews.*etT*tle(),*ews.*etCo*te*t());
    }
    }
    }

    co*te*t_fra*.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    <?xml vers*o*="1.0" e*cod***="utf-8"?&*t;
    <Relat*veLayout xml*s:a*dro*d="http://schemas.a*dro*d.com/apk/res/a*dro*d"
    a*dro*d:or*e*tat*o*="vert*cal"
    a*dro*d:layout_w*dth="match_pare*t"
    a*dro*d:layout_he**ht="match_pare*t"&*t;

    <L**earLayout
    a*dro*d:*d="@+*d/co*te*t"
    a*dro*d:layout_w*dth="match_pare*t"
    a*dro*d:layout_he**ht="match_pare*t"
    a*dro*d:or*e*tat*o*="vert*cal"
    a*dro*d:v*s*b*l*ty="**v*s*ble"&*t;

    <TextV*ew
    a*dro*d:*d="@+*d/tv_t*tle"
    a*dro*d:textS*ze="20sp"
    a*dro*d:layout_w*dth="match_pare*t"
    a*dro*d:layout_he**ht="wrap_co*te*t"
    a*dro*d:padd***="10dp"/&*t;

    <Ima*eV*ew
    a*dro*d:layout_w*dth="match_pare*t"
    a*dro*d:layout_he**ht="1dp"
    a*dro*d:src="@drawable/va*dh"
    a*dro*d:scaleType="f*t*Y"/&*t;

    <TextV*ew
    a*dro*d:*d="@+*d/tv_co*te*t"
    a*dro*d:textS*ze="20sp"
    a*dro*d:layout_w*dth="match_pare*t"
    a*dro*d:layout_he**ht="0dp"
    a*dro*d:layout_we**ht="1"
    a*dro*d:padd***="15dp"/&*t;
    </L**earLayout&*t;

    <Ima*eV*ew
    a*dro*d:layout_w*dth="1dp"
    a*dro*d:layout_he**ht="match_pare*t"
    a*dro*d:src="@drawable/va*dh"
    a*dro*d:layout_al****are*tLeft="true"/&*t;
    </Relat*veLayout&*t;

    *ewsT*tle_fra*.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <?xml vers*o*="1.0" e*cod***="utf-8"?&*t;
    <L**earLayout xml*s:a*dro*d="http://schemas.a*dro*d.com/apk/res/a*dro*d"
    a*dro*d:or*e*tat*o*="vert*cal" a*dro*d:layout_w*dth="match_pare*t"
    a*dro*d:layout_he**ht="match_pare*t"&*t;

    <L*stV*ew
    a*dro*d:*d="@+*d/t*tle_l*st_v*ew"
    a*dro*d:layout_w*dth="match_pare*t"
    a*dro*d:layout_he**ht="match_pare*t"&*t;</L*stV*ew&*t;
    </L**earLayout

    t*tle_*tem.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <?xml vers*o*="1.0" e*cod***="utf-8"?&*t;
    <L**earLayout xml*s:a*dro*d="http://schemas.a*dro*d.com/apk/res/a*dro*d"
    a*dro*d:or*e*tat*o*="vert*cal" a*dro*d:layout_w*dth="match_pare*t"
    a*dro*d:layout_he**ht="match_pare*t"&*t;

    <TextV*ew
    a*dro*d:*d="@+*d/t*tle_*tem_tv"
    a*dro*d:layout_w*dth="match_pare*t"
    a*dro*d:layout_he**ht="match_pare*t"
    a*dro*d:s***leL**e="true"
    a*dro*d:ell*ps*ze="e*d"
    a*dro*d:padd***Left="10dp"
    a*dro*d:padd***R**ht="10dp"
    a*dro*d:padd***Top="15dp"
    a*dro*d:padd***Bottom="15dp"/&*t;
    </L**earLayout&*t;

    co*te*t.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?xml vers*o*="1.0" e*cod***="utf-8"?&*t;
    <L**earLayout xml*s:a*dro*d="http://schemas.a*dro*d.com/apk/res/a*dro*d"
    a*dro*d:or*e*tat*o*="vert*cal" a*dro*d:layout_w*dth="match_pare*t"
    a*dro*d:layout_he**ht="match_pare*t"&*t;

    <fra*me*t
    a*dro*d:*d="@+*d/co*te*t_alo*e"
    a*dro*d:*ame="com.example.*aby.fra*me*tbestpact*cey.NewsCo*te*tFra*me*t"
    a*dro*d:layout_w*dth="match_pare*t"
    a*dro*d:layout_he**ht="match_pare*t"/&*t;

    </L**earLayout

    layout-a*ct*v*ty_ma**

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?xml vers*o*="1.0" e*cod***="utf-8"?&*t;
    <Relat*veLayout xml*s:a*dro*d="http://schemas.a*dro*d.com/apk/res/a*dro*d"
    a*dro*d:layout_w*dth="match_pare*t"
    a*dro*d:layout_he**ht="match_pare*t"&*t;

    <fra*me*t
    a*dro*d:*d="@+*d/t*tle_fra*"
    a*dro*d:*ame="com.example.*aby.fra*me*tbestpact*cey.NewsT*tleFra*me*t"
    a*dro*d:layout_w*dth="match_pare*t"
    a*dro*d:layout_he**ht="match_pare*t"/&*t;

    </Relat*veLayout&*t;

    layout-sw600dp-act*v*ty_ma**

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <?xml vers*o*="1.0" e*cod***="utf-8"?&*t;
    <L**earLayout xml*s:a*dro*d="http://schemas.a*dro*d.com/apk/res/a*dro*d"
    a*dro*d:layout_w*dth="match_pare*t"
    a*dro*d:layout_he**ht="match_pare*t"&*t;

    <fra*me*t
    a*dro*d:*d="@+*d/*ews_t*tle_fra*me*t"
    a*dro*d:*ame="com.example.*aby.fra*me*tbestpact*cey.NewsT*tleFra*me*t"
    a*dro*d:layout_w*dth="0dp"
    a*dro*d:layout_he**ht="match_pare*t"
    a*dro*d:layout_we**ht="1"/&*t;

    <fra*me*t
    a*dro*d:*d="@+*d/co*te*t_fra*_layout"
    a*dro*d:*ame="com.example.*aby.fra*me*tbestpact*cey.NewsCo*te*tFra*me*t"
    a*dro*d:layout_w*dth="0dp"
    a*dro*d:layout_he**ht="match_pare*t"
    a*dro*d:layout_we**ht="3"
    /&*t;

    </L**earLayout&*t;
  • 相关阅读:
    面试题总结
    软件测试面试心得
    git上传本地文件到gitee上
    一个测试员失业的自述
    整理了Linux常用命令变量
    java B2B2C多用户商城系统-搜索分词架构分享
    B2B2C商城系统-Table组件封装代码分享
    Javashop 电商系统sso登录实现
    b2b2c商城系统-会员预存款架构及源码分享
    java 商城系统源码分享-snowflake发号器
  • 原文地址:https://www.cnblogs.com/gabygoole/p/5299865.html
Copyright © 2011-2022 走看看