day4.05总结_Adapter和Listview的优化

一、CommonLayout 优化

1.优化继承体系(减少层次结构)

例如:借助merge标签实现相同布局的合并

<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Button01" />

    <Button                                  减少一层Framelayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Button02"

        android:layout_gravity="right|top"/>

</merge>   

2.重用layout对象

例如:借助include标签导入共用布局(一般是标题或底部或左右页面)

 <include layout="@layout/top_title_layout_1"/>

 

3.实现对象的延迟加载(借助ViewStub)

layout_tv_1.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:textColor="#ffff0000"

    android:textSize="30sp"

    android:text="HelloWorld"

    android:gravity="center_horizontal">

</TextView>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

 

    <!-- 借助此标签实现元素的延迟加载 -->

    <ViewStub

        android:id="@+id/stub_id"  自身的ID           

        android:inflatedId="@+id/tv01"   关联的页面的ID

        android:layout="@layout/layout_tv_1"  关联

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        />

 

    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:layout_alignParentLeft="true"

        android:layout_alignParentRight="true"

        android:layout_marginBottom="106dp"

        android:text="Button"

        android:onClick="onClick" />

 

</RelativeLayout>

 

MainActivity

package com.example.day02;

 

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.ViewStub;

public class MainActivity extends Activity {

        ViewStub vs;

       @Override

       protected void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.activity_main);

           vs=(ViewStub) findViewById(R.id.stub_id);

           Log.i("TAG", "vs="+vs);

          /* View tv=findViewById(R.id.tv01);   没有使用时,tv为空

           Log.i("TAG", "tv="+tv);*/

       }

 

       public void onClick(View v){

              vs.inflate();//加载对应的布局       在使用

              View tv=findViewById(R.id.tv01);    使用后自动加载

              Log.i("TAG", "tv="+tv);

       }

}

 

二、Adapterlayout

所有的AdapterLayout都直接或间接的继承了ViewGroup,并借助一个Adapter对象加载数

据,构建item对象,然后以某种方式呈现给用户。

常用的AdapterLayout如下:

例子1:ArrayAdapter

MainActivity

public class MainActivity extends Activity implements OnItemClickListener {

       @Override

       protected void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.activity_main);

              //1.构建或获得ListView对象

              ListView lsv=(ListView)findViewById(R.id.lsvId);

              String data[]={"关于","张飞","赵云","黄忠","马超"};

              //2.构建Adapter对象

              ArrayAdapter<String> adapter=

                            //new ArrayAdapter<String>(this,R.layout.list_item_res_1, data);

                            new ArrayAdapter<String>(this, R.layout.list_item_res_2,R.id.tv02, data);

              //3.listview 关联adapter         this,在哪里显示,在哪里填充数据,数据)

              lsv.setAdapter(adapter);

              //4.添加监听器

              lsv.setOnItemClickListener(this);

       }

       /**当点击listview中的某个item时会自动执行此方法*/

       @Override

       public void onItemClick(

                     AdapterView<?> parent, //listview

                     View view, //item view

                     int position,//item 位置

                     long id) {//id 的值现阶段与position相等

              Log.i("TAG","parent="+parent);

              Log.i("TAG","view="+view);

              TextView tv=(TextView)view.findViewById(R.id.tv02);    View指当前那个View

              Toast.makeText(this,tv.getText(), 1).show();

       }

}

list_item_res_2.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="horizontal" >

    <ImageView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:src="@drawable/ic_launcher"

        />

    <TextView

        android:id="@+id/tv02"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="text"

        android:textSize="20sp"

        android:padding="10dp"

        />

</LinearLayout>

activity_main.xml(显示的控件)

<ListView xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/lsvId"       ListView ID

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

</ListView>

例子2SimpleAdater

list_item_3.xml

<?xml version="1.0" encoding="utf-8"?>

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:columnCount="2"

    android:rowCount="2" >

 

    <ImageView

        android:id="@+id/imgId"

        android:layout_width="56dp"

        android:layout_height="56dp"

        android:layout_rowSpan="2"

        android:src="@drawable/png_01" />

    <TextView

        android:id="@+id/tvId01"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_column="1"

        android:layout_row="1"

        android:text="text02"

        />

    <TextView

        android:id="@+id/tvId02"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_column="1"

        android:layout_row="0"

         android:layout_marginTop="10dp"

        android:text="text01" />

 

</GridLayout>

 

activity_main.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/lsvId"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

</ListView>

MainActivity

public class MainActivity extends Activity {

       private List<Map<String,Object>> data=

       new ArrayList<Map<String,Object>>();

       @Override

       protected void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.activity_main);

           initData();

       //1.获得listview

              ListView lsv=(ListView) findViewById(R.id.lsvId);

       //2.创建adapter

              SimpleAdapter adapter=

              new SimpleAdapter(this,//context

              data, //List<? extends Map<String,?>>

              R.layout.list_item_3, //item layout

              new String[]{"imgKey","titleKey","dateKey"},//map中的key

              new int[]{R.id.imgId,R.id.tvId02,R.id.tvId01});//item layout 中的位置

    //3.关联adapter

              lsv.setAdapter(adapter);

       }

       int imgs[]={R.drawable.png_01,R.drawable.png_02,R.drawable.png_03,R.drawable.png_04,R.drawable.png_05};

      

       /**初始化数据(数据假的,将来网络的服务端)*/

       private void initData(){

         for(int i=0;i<5;i++){

          Map<String,Object> map=

          new HashMap<String, Object>();

          map.put("imgKey", imgs[i]);

          map.put("titleKey", "title"+i);

          map.put("dateKey", "2016-04-05");

          data.add(map);

         }

       }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值