Android——ListView与适配器

Android——ListView与适配器

1.抽屉布局  Drawer

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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="com.example.jreduch7292.MyDrawerActivity"
    android:id="@+id/drawer_layout"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv"
        android:gravity="center"
        android:text="我的主布局"
        />
</RelativeLayout>
    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:background="#f9f113"
        android:layout_gravity="start"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#b9b9c4"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@mipmap/zyfzyf"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="抽屉1"
                />
        </LinearLayout>

    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

2.ListView新闻列表

package com.example.jreduch7292.listdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.jreduch7292.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListActivity extends AppCompatActivity {
private ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        lv=(ListView)findViewById(R.id.lv);
//        List list=new ArrayList();
//        list.add("张三  123");
//        list.add("张四  1234");
//        list.add("张五  12345");
//        list.add("张六  123456");
//        list.add("张三  123");
//        list.add("张四  1234");
//        list.add("张五  12345");
//        list.add("张六  123456");
//        list.add("张三  123");
//        list.add("张四  1234");
//        list.add("张五  12345");
//        list.add("张六  123456");
//        list.add("张三  123");
//        list.add("张四  1234");
//        list.add("张五  12345");
//        list.add("张六  123456");
//        ArrayAdapter aa=new ArrayAdapter(this,
//                android.R.layout.simple_list_item_1,list);
//        lv.setAdapter(aa);
        final List list=new ArrayList();
        Map map=new HashMap();
        map.put("img",R.mipmap.a);
        map.put("name","字母A张三  1121132323");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.b);
        map.put("name","字母B张四  123444434");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.c);
        map.put("name","字母C张五  12334233");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.d);
        map.put("name","字母D张六  11323456");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.e);
        map.put("name","字母E张五  12323445");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.f);
        map.put("name","字母F张四  12324424");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.g);
        map.put("name","字母G张五  12322445");
        list.add(map);
        map=new HashMap();
        map.put("img",R.mipmap.h);
        map.put("name","字母H张六  12233456");
        list.add(map);

        /*
        1.simpleAdapter使用的数据源必须继承Map接口
        2.from参数的意思是指向数据源Map中的键
        3.to 参数的意思是为布局中的控件Id赋值
         */
        SimpleAdapter sa=new SimpleAdapter(this,list,
                R.layout.lll,
                new String[]{"img","name"},
                new int[]{R.id.iv,R.id.tv});
       lv.setAdapter(sa);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getBaseContext(),"点击行:"+id,Toast.LENGTH_SHORT).show();
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.jreduch7292.listdemo.ListActivity">
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:dividerHeight="5dp"
    android:divider="@color/colorPrimary"
    android:id="@+id/lv"
    android:scrollbars="none"
    android:listSelector="@color/colorAccent"
    >

</ListView>
</RelativeLayout>

图片+汉子布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"

    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="centerCrop"
        android:id="@+id/iv"
        android:src="@mipmap/ic_launcher"
        /><!--android:scaleType="centerCrop"等比例缩放-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/tv"
        android:textSize="40sp"
        android:gravity="center"
        android:text="测试"
        />
</LinearLayout>

ListView优化----Holder

ListView多布局

ListView优化-----分段显示

   新闻ListView---------------------重点重点重点

package com.example.jreduch7292.entity;

/**
 * Created by 冲天之峰 on 2016/8/3.
 */
public class News {

    private String title;
    private String pubDate;
    private String newId;
    private int img;
    private String from;

    public News(String title, String pubDate, String newId, int img, String from) {
        this.title = title;
        this.pubDate = pubDate;
        this.newId = newId;
        this.img = img;
        this.from = from;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public int getImg() {
        return img;
    }

    public void setImg(int img) {
        this.img = img;
    }

    public String getNewId() {
        return newId;
    }

    public void setNewId(String newId) {
        this.newId = newId;
    }

    public String getPubDate() {
        return pubDate;
    }

    public void setPubDate(String pubDate) {
        this.pubDate = pubDate;
    }
}
package com.example.jreduch7292.entity;

/**
 * Created by 冲天之峰 on 2016/8/3.
 */
public class News {

    private String title;
    private String pubDate;
    private String newId;
    private int img;
    private String from;

    public News(String title, String pubDate, String newId, int img, String from) {
        this.title = title;
        this.pubDate = pubDate;
        this.newId = newId;
        this.img = img;
        this.from = from;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public int getImg() {
        return img;
    }

    public void setImg(int img) {
        this.img = img;
    }

    public String getNewId() {
        return newId;
    }

    public void setNewId(String newId) {
        this.newId = newId;
    }

    public String getPubDate() {
        return pubDate;
    }

    public void setPubDate(String pubDate) {
        this.pubDate = pubDate;
    }
}

布局程序

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="我是标题"
    android:gravity="center"
    android:id="@+id/title"
    android:layout_alignParentTop="true"
    android:layout_alignBottom="@+id/img"
    android:layout_alignParentStart="true"
    android:layout_toStartOf="@+id/img" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="未来网"
        android:gravity="center"
        android:id="@+id/from"
        android:layout_below="@+id/img"
        android:layout_alignParentStart="true" />
    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@mipmap/ic_launcher"
        android:id="@+id/img"
        android:scaleType="centerCrop"

        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="今天"
        android:gravity="center"
        android:id="@+id/time"
        android:layout_below="@+id/img"
        android:layout_alignStart="@+id/img" />
</RelativeLayout>
package com.example.jreduch7292.listdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import com.example.jreduch7292.R;
import com.example.jreduch7292.adapter.MyListAdapter;
import com.example.jreduch7292.entity.News;

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

public class List3Activity extends AppCompatActivity {
private ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list3);
        lv=(ListView)findViewById(R.id.lv);
        List list=new ArrayList();
        News ns=new News("新闻1","刚刚","n001",R.mipmap.a,"人民网");
        list.add(ns);
        ns=new News("新闻2","1小时前","n002",R.mipmap.b,"新浪网");
        list.add(ns);
        ns=new News("新闻3","刚刚","n003",R.mipmap.c,"搜狐网");
        list.add(ns);
        ns=new News("新闻4","刚刚","n004",R.mipmap.d,"谷歌网");
        list.add(ns);
        ns=new News("新闻5","1小时前","n005",R.mipmap.e,"新浪网");
        list.add(ns);
        ns=new News("新闻6","3小时前","n006",R.mipmap.f,"百度网");
        list.add(ns);
        ns=new News("新闻7","刚刚","n007",R.mipmap.g,"新浪网");
        list.add(ns);
        ns=new News("新闻8","1小时前","n008",R.mipmap.h,"新浪网");
        list.add(ns);
        ns=new News("新闻9","刚刚","n009",R.mipmap.zyf,"天涯网");
        list.add(ns);
        ns=new News("新闻10","5小时前","n0010",R.mipmap.zyfzyf,"新浪网");
        list.add(ns);
        ns=new News("新闻1","刚刚","n001",R.mipmap.a,"人民网");
        list.add(ns);
        ns=new News("新闻2","1小时前","n002",R.mipmap.b,"新浪网");
        list.add(ns);
        ns=new News("新闻3","刚刚","n003",R.mipmap.c,"搜狐网");
        list.add(ns);

        lv.setAdapter(new MyListAdapter(this,list));
    }
}
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.jreduch7292.listdemo.List3Activity">
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lv"
    >

</ListView>
</RelativeLayout>

新闻ListView+点击监听

1.News2代码

package com.example.jreduch7292.entity;

/**
 * Created by 冲天之峰 on 2016/8/3.
 */
public class News2 {

    private String title;
    private String pubDate;
    private String newId;
    private int img1;
    private int img2;
    private int img3;
    private String from;
    private String pinglun;
    private String jian;
    private String x;
   // private int goodCount;

    public News2( String jian, String x, String pinglun, String from, int img3, int img2, int img1, String newId, String pubDate, String title) {

        this.jian = jian;
        this.x = x;
        this.pinglun = pinglun;
        this.from = from;
        this.img3 = img3;
        this.img2 = img2;
        this.img1 = img1;
        this.newId = newId;
        this.pubDate = pubDate;
        this.title = title;
    }

    public String getX() {
        return x;
    }

    public void setX(String x) {
        this.x = x;
    }

    public String getJian() {
        return jian;
    }

    public void setJian(String jian) {
        this.jian = jian;
    }

    public String getPinglun() {
        return pinglun;
    }

    public void setPinglun(String pinglun) {
        this.pinglun = pinglun;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public int getImg3() {
        return img3;
    }

    public void setImg3(int img3) {
        this.img3 = img3;
    }

    public int getImg2() {
        return img2;
    }

    public void setImg2(int img2) {
        this.img2 = img2;
    }

    public int getImg1() {
        return img1;
    }

    public void setImg1(int img1) {
        this.img1 = img1;
    }

    public String getNewId() {
        return newId;
    }

    public void setNewId(String newId) {
        this.newId = newId;
    }

    public String getPubDate() {
        return pubDate;
    }

    public void setPubDate(String pubDate) {
        this.pubDate = pubDate;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

2.MyList2Adapter代码

package com.example.jreduch7292.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jreduch7292.R;
import com.example.jreduch7292.entity.News2;

import java.util.List;

/**
 * Created by 冲天之峰 on 2016/8/3.
 */
public class MyList2Adapter extends BaseAdapter {
    private List<News2> myData;

    private Context context;
    public  MyList2Adapter( Context context,List<News2> myData){
        this.context=context;
        this.myData=myData;
    }

    @Override
    public int getCount() {
        return myData.size();
    }

    @Override
    public Object getItem(int position) {
        return myData.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
   final     ViewHolder vh;
     if(convertView==null){
         vh=new ViewHolder();
         convertView=LayoutInflater.from(context).inflate(R.layout.list2_base,null);
         vh.title=(TextView)convertView.findViewById(R.id.title);
        vh.pubDate=(TextView)convertView.findViewById(R.id.time);
        vh.from=(TextView) convertView.findViewById(R.id.from);
       vh.pinglun=(TextView) convertView.findViewById(R.id.pinglun);
        vh.jian=(TextView) convertView.findViewById(R.id.jian);
        vh.x=(TextView) convertView.findViewById(R.id.x);
        vh.img1=(ImageView)convertView.findViewById(R.id.img1);
        vh.img2=(ImageView)convertView.findViewById(R.id.img2);
        vh.img3=(ImageView)convertView.findViewById(R.id.img3);

         convertView.setTag(vh);
     }else{

         vh= (ViewHolder)convertView.getTag();
     }
        vh.img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"点击了图片"+position,Toast.LENGTH_SHORT).show();
            }
        });

        final News2 ns=myData.get(position);
        vh.jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"点击了评论+1",Toast.LENGTH_SHORT).show();
               int count=Integer.parseInt(vh.pinglun.getText().toString())+1;
                vh.pinglun.setText(count+"");
               // vh.pinglun.setTextColor(0xffcc0000);
                ns.setPinglun(String.valueOf(count));
            }
        });

//       News2 ns= myData.get(position);
        vh.title.setText(ns.getTitle());
        vh. img1.setImageResource(ns.getImg1());
        vh. img2.setImageResource(ns.getImg2());
        vh. img3.setImageResource(ns.getImg3());
        vh. jian.setText(ns.getJian());
        vh. from.setText(ns.getFrom());
        vh. pinglun.setText(ns.getPinglun());
        vh. pubDate.setText(ns.getPubDate());
        vh. x.setText(ns.getX());
       // vh.goodCount.setText(ns.getGoodCount());

        return convertView;
    }

//        View view= LayoutInflater.from(context).inflate(R.layout.list2_base,null);
//        TextView title=(TextView) view.findViewById(R.id.title);
//        TextView pubDate=(TextView) view.findViewById(R.id.time);
//        TextView  from=(TextView) view.findViewById(R.id.from);
//        TextView  pinglun=(TextView) view.findViewById(R.id.pinglun);
//        TextView  jian=(TextView) view.findViewById(R.id.jian);
//        TextView x=(TextView) view.findViewById(R.id.x);
//        ImageView img1=(ImageView)view.findViewById(R.id.img1);
//        ImageView img2=(ImageView)view.findViewById(R.id.img2);
//        ImageView img3=(ImageView)view.findViewById(R.id.img3);
//
//        News2 news=myData.get(position);
//        title.setText(news.getTitle());
//        img1.setImageResource(news.getImg1());
//        img2.setImageResource(news.getImg2());
//        img3.setImageResource(news.getImg3());
//        jian.setText(news.getJian());
//        from.setText(news.getFrom());
//        pinglun.setText(news.getPinglun());
//        pubDate.setText(news.getPubDate());
//        x.setText(news.getX());

    public class ViewHolder{
        TextView title;
        ImageView img1;
        TextView pubDate;
        TextView  from;
        TextView  pinglun;
        TextView  jian;
        TextView x;
        ImageView img2;
        ImageView img3;
     //   TextView goodCount;
    }
}

3(R.layout.list2_base,null);布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我是标题"
        android:gravity="center"
        android:id="@+id/title"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/title"
        android:id="@+id/ll1">
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_launcher"
            android:id="@+id/img1"
            android:scaleType="centerCrop"
            android:layout_weight="1"

             />
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_launcher"
            android:id="@+id/img2"
            android:scaleType="centerCrop"
            android:layout_weight="1"
            />
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_launcher"
            android:id="@+id/img3"
            android:scaleType="centerCrop"
            android:layout_weight="1"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/ll2"
        android:layout_below="@+id/ll1"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="荐"
            android:background="#0920f2"
            android:gravity="center"
            android:id="@+id/jian"
            android:textSize="25sp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="未来网"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:id="@+id/from"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="评论291"
            android:layout_marginLeft="80dp"
            android:gravity="center"
            android:id="@+id/pinglun"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100dp"
            android:text="今天"
            android:gravity="center"
            android:id="@+id/time"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="X"
            android:background="#fd8f8686"
            android:gravity="center"
            android:textSize="25sp"
            android:id="@+id/x"
            />
    </LinearLayout>

</RelativeLayout>

4.List2Activity代码

<pre name="code" class="java">package com.example.jreduch7292.listdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import com.example.jreduch7292.R;
import com.example.jreduch7292.adapter.MyList2Adapter;
import com.example.jreduch7292.entity.News2;

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

public class List2Activity extends AppCompatActivity {
    private ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list2);
        lv=(ListView)findViewById(R.id.lv);
        List list=new ArrayList();
        News2 ns=new News2("荐","X","21323","人民网",R.mipmap.a,R.mipmap.b,R.mipmap.c,"n001","刚刚","新闻1");
        list.add(ns);
        ns=new News2("荐","X","21323","人民网",R.mipmap.d,R.mipmap.e,R.mipmap.f,"n001","刚刚","新闻2");
        list.add(ns);
        ns=new News2("荐","X","21323","人民网",R.mipmap.a,R.mipmap.b,R.mipmap.c,"n001","刚刚","新闻3");
        list.add(ns);
        ns=new News2("荐","X","21323","人民网",R.mipmap.d,R.mipmap.e,R.mipmap.f,"n001","刚刚","新闻4");
        list.add(ns);
        ns=new News2("荐","X","21323","人民网",R.mipmap.a,R.mipmap.b,R.mipmap.c,"n001","刚刚","新闻5");
        list.add(ns);
        ns=new News2("荐","X","21323","人民网",R.mipmap.d,R.mipmap.e,R.mipmap.f,"n001","刚刚","新闻6");
        list.add(ns);

        lv.setAdapter(new MyList2Adapter(this,list));
    }
}


5.ListView布局

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.jreduch7292.listdemo.List2Activity">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv"
        android:dividerHeight="10dp"

        ></ListView>
</RelativeLayout>

作者:冲天之峰    20160803

时间: 2024-10-13 00:26:51

Android——ListView与适配器的相关文章

Android——ListView布局+适配器(三)

Android--ListView布局+适配器(三) package com.example.administrator.newstop; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import andro

Android listview的适配器以及各种监听、效率的提升

之前写过一篇关于listview的博客,现在感觉那篇博客关于listview认识不够全面.但有些方法还是可取,例如灵活的监听,写适配器.链接在这里android listview长按,单击各种事件捕捉.那个单击监听是一项项设置监听,效率也不好. 不断工作的过程,也是不断总结过程.现在对于listview有了更透彻的理解,所以重新写了一个实践demo.这个demo有涉及到listview的数据源,布局,适配器以及各种监听.效率的提升.现在对于这些理解,想写出一个通用的适配器,但发现还是有点困难,就

Android ListView的适配器 Adapter 中GetView方法调用次数大于数据条数的问题

情况描述: 今天在写数据展示的时候,用了ListView,在它的适配器当中,发现getview方法执行的次数是数据条目的N倍(我这边显示的是4倍,这个倍数值不知道会不会变化),这显然是不科学的! 查阅资料,发现是ListView的布局问题.我把 android:layout_height="wrap_content"高度设置成了wrap_content.而wrap_content值使得ListView没有取到实际的高度,他还要根据计算才能确定,而每一次计算应该会触发listview的渲

android listview万能适配器

参考 https://github.com/hongyangAndroid/base-adapter public class CommonViewHolder { private Context context;    private int position;    private int layoutId; private View convertView;    private SparseArray<View> views; private CommonViewHolder(Cont

Android——ListView多布局+适配器(二)

Android--ListView多布局+适配器(二) <span style="font-size:18px;">package com.example.administrator.newstop.entity; /** * Created by Administrator on 2016/8/4. */ public class News { private String title; private String pubDate; private int img; p

Android 快速开发系列 打造万能的ListView GridView 适配器

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38902805 ,本文出自[张鸿洋的博客] 1.概述 相信做Android开发的写得最多的就是ListView,GridView的适配器吧,记得以前开发一同事开发项目,一个项目下来基本就一直在写ListView的Adapter都快吐了~~~对于Adapter一般都继承BaseAdapter复写几个方法,getView里面使用ViewHolder模式,其实大部分的代码基本都是类似的

Android:ListView和适配器的使用

ListView作用:将数据填充到布局 实例:下载>> Listview与适配器ArrayAdapter配合使用,ArrayAdapter比较简单,但它只能用于显示文字.布局文件可以自己写,也可以用系统的. 在布局文件中加入一个ListView控件: <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wra

Android开发之适配器-ListView适配器的重复数据

适配器是Android中的数据与View视图的桥梁,作用就是将数据通过适配器显示到对应的View视图上. 工作中,在用ListView做适配器数据时候,有些人肯定碰见过,如何优化效率,但是又出现重复数据的情况,如何避免重复数据而且又能提高ListView大数据量时候的效率呢?,解决方案就是2点: 1. 在getView方法中 进行View的判断,即做convertView ==null 这样的判断,这样是为了提高数据量大的时候的效率. 2.设置数据用setTag()/getTag()方法来进行对

android listview级联三菜单选择地区,本地数据库sqlite级联地区,item选中不变色

前言:因为找了N多网上的资源都没有好的解决方案,别人都是只给思路没给具体源码,真TMD纠结,干嘛求别人,自己动手才是真,最痛恨那些所谓大牛的作风,给了点点代码就让别人去想,你让我们这种小白情何于堪!!!!!!此例是基于listview来实现本地sqlite实现的! 二话不说,程序猿求的是有图有真相有源码!大家下载后有什么问题可以找到本人:QQ508181017 核心代码如下 1.数据库操作类 package com.icq.demo.db; import java.util.ArrayList;