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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值