Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> ListView(列表視圖)+BaseAdapter(基礎適配器)=android滑動列表

ListView(列表視圖)+BaseAdapter(基礎適配器)=android滑動列表

編輯:關於Android編程

首先,我們要建一個layout,裡面放一個ListView,長寬皆適應屏幕即可。

 

    
    
然後,我們要在這個ListView裡顯示東西,顯示什麼樣的東西,什麼樣的布局呢?所以,我們要再建一個layout,這裡存放我們要顯示的布局,此處以一個新聞軟件的界面為例。

 

\

由該界面可見,此處用了一個ListView來顯示這些新聞,而每一個新聞,則由Title標題,Image圖片,pubDate發布日期,goodCount點贊量以及newsFrom新聞來源構成。

所以,我們這個布局可以這樣寫:

 




    
    
        
        
        
    
    
        
        
        
    
好了,布局建好了,接下來我們就要加載數據到布局中,然後用適配器,讓布局在ListView中規則地顯示出來。

 

我們先寫一個適配器,建一個新的類,類名自定,但要讓其繼承BaseAdapter

 

public class MyListAdapter2 extends BaseAdapter {
    private List myData;
    private Context context;
    public MyListAdapter2(List myData,Context context){
        this.myData = myData;
        this.context = context;
    }
    @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) {
        //----------------------------
//        View view = LayoutInflater.from(context).inflate(R.layout.list_layout_base1,null);
//        TextView title = (TextView) view.findViewById(R.id.title);
//        TextView pubDate = (TextView) view.findViewById(R.id.pubDate);
//        TextView from = (TextView) view.findViewById(R.id.from);
//        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 news2 = myData.get(position);
//        title.setText(news2.getTitle());
//        pubDate.setText(news2.getPubDate());
//        from.setText(news2.getFrom());
//        img1.setImageResource(news2.getImg1());
//        img2.setImageResource(news2.getImg2());
//        img3.setImageResource(news2.getImg3());
//        return view;
        //--------------下面的方法更加節省資源----------------
        //引用用了setTag()方法,保存了控件,復用了資源。
        final ViewHolder vh;
        if(convertView==null){
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_layout_base1,null);
            vh.title = (TextView) convertView.findViewById(R.id.title);
            vh.pubDate = (TextView) convertView.findViewById(R.id.pubDate);
            vh.from = (TextView) convertView.findViewById(R.id.from);
            vh.img1 = (ImageView) convertView.findViewById(R.id.img1);
            vh.img2 = (ImageView) convertView.findViewById(R.id.img2);
            vh.img3 = (ImageView) convertView.findViewById(R.id.img3);
            vh.ib1 = (ImageButton) convertView.findViewById(R.id.ib1);

//因為setTag只能包含一個參數,但我們要保存所有的view,所以我們新建一個類,讓它包含其所有,然後一起放進去

 


convertView.setTag(vh); }else{ vh = (ViewHolder) convertView.getTag(); } final News2 news2 = myData.get(position); vh.img1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context,"點擊了新聞"+position+"的圖1",Toast.LENGTH_SHORT).show(); } }); vh.ib1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int i = Integer.parseInt(vh.pubDate.getText().toString())+1; vh.pubDate.setText(String.valueOf(i)); //將贊的數量保存 news2.setCount(i);// vh.pubDate.setTextColor(0xffcc0000);顏色改變,但是沒保存,滑動過程中會發生錯位 } }); vh.title.setText(news2.getTitle()); //將保存的贊的數量再賦值給pubDate vh.pubDate.setText(news2.getCount()+""); vh.from.setText(news2.getFrom()); vh.img1.setImageResource(news2.getImg1()); vh.img2.setImageResource(news2.getImg2()); vh.img3.setImageResource(news2.getImg3()); return convertView; } public class ViewHolder { TextView title; TextView pubDate; TextView from; ImageView img1; ImageView img2; ImageView img3; ImageButton ib1; }}

適配器也弄好了,萬事俱備,只欠東風!我們要在Activity裡寫一些數據了。由於前面Adapter適配器裡我們傳入的參數是個List,所以我們要把數據都放在一個ArrayList裡,可是我們每次想要傳入的是一套數據啊,包括標題、發布時間、來源等屬性。所以,我們建一個類,每次傳一個它的對象進去,其中包括了這些屬性。

 

News類如下:

 

public class News2 {
    private String title;
    private String pubDate;
    private String newsID;
    private int img1;
    private int img2;
    private int img3;
    private String from;
    private int count;

    public News2() {
    }

    public News2(String title, String pubDate, String newsID, int img1, int img2, int img3, String from) {
        this.title = title;
        this.pubDate = pubDate;
        this.newsID = newsID;
        this.img1 = img1;
        this.img2 = img2;
        this.img3 = img3;
        this.from = from;
    }

    public News2(String title, String pubDate, String newsID, int img1, int img2, int img3, String from,int count) {
        this.count = count;
        this.title = title;
        this.pubDate = pubDate;
        this.newsID = newsID;
        this.img1 = img1;
        this.img2 = img2;
        this.img3 = img3;
        this.from = from;
    }

    public String getFrom() {
        return from;
    }

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

    public String getTitle() {
        return title;
    }

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

    public String getPubDate() {
        return pubDate;
    }

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

    public String getNewsID() {
        return newsID;
    }

    public void setNewsID(String newsID) {
        this.newsID = newsID;
    }

    public int getImg1() {
        return img1;
    }

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

    public int getImg2() {
        return img2;
    }

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

    public int getImg3() {
        return img3;
    }

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

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
最後,就是我們的Activity代碼了!

 

 

public class List3Activity extends AppCompatActivity {

    private ListView lv1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list3);
        lv1 = (ListView) findViewById(R.id.lv1);
        List list = new ArrayList();
        News2 ns = new News2("新聞1","1","n004",R.mipmap.mei01,R.mipmap.mei02,R.mipmap.mei01,"新浪",0);
        list.add(ns);
        ns = new News2("新聞2","1","n003",R.mipmap.mei02,R.mipmap.mei01,R.mipmap.mei02,"博客園",0);
        list.add(ns);
        ns = new News2("新聞3","3","n002",R.mipmap.mei01,R.mipmap.mei02,R.mipmap.mei01,"CSDN",0);
        list.add(ns);
        ns = new News2("新聞4","4","n001",R.mipmap.mei02,R.mipmap.mei01,R.mipmap.mei02,"知乎",0);
        list.add(ns);
        ns = new News2("新聞1","5","n004",R.mipmap.mei01,R.mipmap.mei02,R.mipmap.mei01,"新浪",0);
        list.add(ns);
        ns = new News2("新聞2","2","n003",R.mipmap.mei02,R.mipmap.mei01,R.mipmap.mei02,"博客園",0);
        list.add(ns);
        ns = new News2("新聞3","6","n002",R.mipmap.mei01,R.mipmap.mei02,R.mipmap.mei01,"CSDN",0);
        list.add(ns);
        ns = new News2("新聞4","7","n001",R.mipmap.mei02,R.mipmap.mei01,R.mipmap.mei02,"知乎",0);
        list.add(ns);
        ns = new News2("新聞1","8","n004",R.mipmap.mei01,R.mipmap.mei02,R.mipmap.mei01,"新浪",0);
        list.add(ns);
        ns = new News2("新聞2","9","n003",R.mipmap.mei02,R.mipmap.mei01,R.mipmap.mei02,"博客園",0);
        list.add(ns);
        ns = new News2("新聞3","1","n002",R.mipmap.mei01,R.mipmap.mei02,R.mipmap.mei01,"CSDN",0);
        list.add(ns);
        ns = new News2("新聞4","2","n001",R.mipmap.mei02,R.mipmap.mei01,R.mipmap.mei02,"知乎",0);
        list.add(ns);
        ns = new News2("新聞1","3","n004",R.mipmap.mei01,R.mipmap.mei02,R.mipmap.mei01,"新浪",0);
        list.add(ns);
        ns = new News2("新聞2","4","n003",R.mipmap.mei02,R.mipmap.mei01,R.mipmap.mei02,"博客園");
        list.add(ns);
        ns = new News2("新聞3","5","n002",R.mipmap.mei01,R.mipmap.mei02,R.mipmap.mei01,"CSDN");
        list.add(ns);
        ns = new News2("新聞4","6","n001",R.mipmap.mei02,R.mipmap.mei01,R.mipmap.mei02,"知乎");
        list.add(ns);

        lv1.setAdapter(new MyListAdapter2(list,this));
    }
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved