Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android——ListView多布局+適配器(二)

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

編輯:關於Android編程

\

\

\

\

\

 

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;
    private  int   img1;
    private  int   img2;
    private  int   img3;

    public News(String title, String pubDate, int img, int img1, int img2, int img3) {
        this.title = title;
        this.pubDate = pubDate;
        this.img = img;
        this.img1 = img1;
        this.img2 = img2;
        this.img3 = img3;
    }

    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 int getImg() {
        return img;
    }

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

    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;
    }
}

package com.example.administrator.newstop.adapter;

import android.content.Context;
import android.content.Intent;
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.administrator.newstop.Main2Activity;
import com.example.administrator.newstop.R;
import com.example.administrator.newstop.entity.News;

import java.util.List;

/**
 * Created by Administrator on 2016/8/4.
 */
public class MyListViewAdapter extends BaseAdapter {
    private Context context;
    private List list;

    public MyListViewAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if(view==null){
            vh = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.list_simple_layout,null);
            vh.img = (ImageView) view.findViewById(R.id.iv);
            vh.title = (TextView) view.findViewById(R.id.tv);
            view.setTag(vh);
        }else {
            vh = (ViewHolder) view.getTag();
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, Main2Activity.class);
                context.startActivity(intent);
            }
        });
        vh.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,"點擊圖片",Toast.LENGTH_SHORT).show();
            }
        });
        News news = list.get(i);
        vh.title.setText(news.getTitle());
        vh.img.setImageResource(news.getImg());
        return view;
    }
    public class ViewHolder{
        TextView title;
        ImageView img;
    }

}
package com.example.administrator.newstop;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.newstop.adapter.MyListViewAdapter;
import com.example.administrator.newstop.entity.News;

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

public class MainActivity extends AppCompatActivity {
    private ViewPager vp;
    private List lvList ; //存放ListView的集合
    private  String[] channel;
    private RadioGroup rg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        channel= new String[]{"推薦", "煙台", "要聞", "熱點", "視頻", "財經", "健康", "科技", "書籍", "游戲", "生活", "國際"};
        rg = (RadioGroup) findViewById(R.id.rg);
        vp = (ViewPager) findViewById(R.id.vp);
        //初始化RadioButton
        intiRadioButton();
        lvList = new ArrayList();
        //創建ListView的方法
        initListView();
        vp.setAdapter(new ViewPagerAdapterForListView(lvList));
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                  RadioButton bg = (RadioButton) rg.getChildAt(position);
                  bg.requestFocus();
                  bg.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                vp.setCurrentItem(i);
            }
        });
    }
    public void intiRadioButton(){
        for(int i=0;i list;
        public ViewPagerAdapterForListView(List list) {
            this.list = list;
        }
        @Override
        public int getCount() {
            return list.size();
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
           container.removeView(list.get(position));
        }
    }
}



  


      

      

      

  
    
        

            

            

        

        
    

    

    




    
    




 



\


 

\

\

 

\

ListView多布局

 

package com.example.jreduch7292.entity;

/**
 * Created by 沖天之峰 on 2016/8/4.
 */

public class New4 {

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

    public New4(String x, String jian, String pinglun, String from, int img3, int img2, int img1, int img, String newId, String pubDate, String title) {
        this.x = x;
        this.jian = jian;
        this.pinglun = pinglun;
        this.from = from;
        this.img3 = img3;
        this.img2 = img2;
        this.img1 = img1;
        this.img = img;
        this.newId = newId;
        this.pubDate = pubDate;
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

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

    public String getX() {
        return x;
    }

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

    public String getPinglun() {
        return pinglun;
    }

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

    public String getJian() {
        return jian;
    }

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

    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 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.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 com.example.jreduch7292.R;
import com.example.jreduch7292.entity.New4;

import java.util.List;

/**
 * Created by 沖天之峰 on 2016/8/4.
 */
public class MyList4MultLayoutAdapter extends BaseAdapter{

    private Listlist;
    private Context context;
    private final int TYPE1=0;
    private final int TYPE2=1;

    public MyList4MultLayoutAdapter(List list, Context context) {
        this.list = list;
        this.context = context;
    }

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

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

    @Override
    public long getItemId(int position) {
        return 0;
    }
//getZ
    @Override
    public int getItemViewType(int position) {
      if (list.get(position).getImg1()==0){
          return TYPE1;
      }else{
          return  TYPE2;
      }

    }
//getZ
    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        int type=getItemViewType(position);

        if (convertView==null){
            vh=new ViewHolder();
            if (type==0){
                convertView= LayoutInflater.from(context).inflate(R.layout.list_layout_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.img=(ImageView) convertView.findViewById(R.id.img);
                convertView.setTag(vh);


            }else {
            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();
        }
        New4 ns=list.get(position);
        if (type==0){
           vh. title.setText(ns.getTitle());
           vh. pubDate.setText(ns.getPubDate());
          vh.  from.setText(ns.getFrom());
           vh. img.setImageResource(ns.getImg());
        }else{
            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()) ;
        }
        return convertView;
    }

    private class ViewHolder{

        //布局1中使用的控件
        TextView title;
        TextView  pubDate;
        TextView from;
        ImageView img;
        TextView pinglun;
        TextView jian;
        TextView x;
        //布局2中使用的控件
        ImageView img1;
        ImageView img2;
        ImageView img3;

    }
}

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.MyList4MultLayoutAdapter;
import com.example.jreduch7292.entity.New4;

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

public class List4Activity extends AppCompatActivity {
public ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list4);

        lv=(ListView)findViewById(R.id.lv);
        List list=new ArrayList();
        New4 ns=new New4("X","薦","21323","人民網",R.mipmap.a,R.mipmap.b,R.mipmap.c,0,"n001","剛剛","新聞1");
        list.add(ns);
        ns=new New4("","","","百度網",0,0,0,R.mipmap.b,"n001","1小時","新聞2");
        list.add(ns);
        ns=new New4("","","","人民網",0,0,0,R.mipmap.c,"n001","2小時","新聞3");
        list.add(ns);
        ns=new New4("X","薦","21323","新浪網",R.mipmap.d,R.mipmap.e,R.mipmap.f,0,"n001","3小時","新聞4");
        list.add(ns);
        ns=new New4("X","薦","21323","人民網",R.mipmap.a,R.mipmap.b,R.mipmap.c,0,"n001","4小時","新聞5");
        list.add(ns);
        ns=new New4("","","","搜狐網",0,0,0,R.mipmap.a,"n001","昨天","新聞6");
        list.add(ns);



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




\




    
    
    



    
    
        
        
        
    
    
        
        


        
        
        
    

\


 

\

 

 

package com.example.jreduch7292.listdemo;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.jreduch7292.R;
import com.example.jreduch7292.adapter.MyList4MultLayoutAdapter;
import com.example.jreduch7292.adapter.MyTopNewsAdapter;
import com.example.jreduch7292.entity.New4;

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

public class TopNewsActivity extends AppCompatActivity {

    //public ListView lv;
    private MyList4MultLayoutAdapter my;

//private String[] channe1;

    private RadioGroup rg;
   private ViewPager vp;
   private List<listview> img;  //存放ListView集合
    private MyTopNewsAdapter mp;
    private RadioButton rb1,rb2,rb3,rb4,rb5,rb6,rb7,rb8,rb9,rb10;
    private HorizontalScrollView hs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_top_news);

        getSupportActionBar().hide();

//        String[] channel={"推薦","煙台","財經","科學","游戲","生活","數學","健康","視頻","科技"};

        rb1=(RadioButton) findViewById(R.id.rb1);
        rb2=(RadioButton) findViewById(R.id.rb2);
        rb3=(RadioButton) findViewById(R.id.rb3);
        rb4=(RadioButton) findViewById(R.id.rb4);
        rb5=(RadioButton) findViewById(R.id.rb5);
        rb6=(RadioButton) findViewById(R.id.rb6);
        rb7=(RadioButton) findViewById(R.id.rb7);
        rb8=(RadioButton) findViewById(R.id.rb8);
        rb9=(RadioButton) findViewById(R.id.rb9);
        rb10=(RadioButton) findViewById(R.id.rb10);


        hs=(HorizontalScrollView)findViewById(R.id.hs);
       //lv=(ListView)findViewById(R.id.lv) ;
        rg=(RadioGroup)findViewById(R.id.rg);
        vp=(ViewPager)findViewById(R.id.vp);

        //創建ListView方法
        vpInit();
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId==R.id.rb1){
                    vp.setCurrentItem(0);
                    Toast.makeText(getBaseContext(),"推薦1",Toast.LENGTH_SHORT).show();
                } else if (checkedId==R.id.rb2){
                    vp.setCurrentItem(1);
                    Toast.makeText(getBaseContext(),"推薦2",Toast.LENGTH_SHORT).show();
                }
                else if (checkedId==R.id.rb3){
                    vp.setCurrentItem(2);
                    Toast.makeText(getBaseContext(),"推薦3",Toast.LENGTH_SHORT).show();
                }else{
                    switch(checkedId){
                        case R.id.rb4:
                            vp.setCurrentItem(3);
                            Toast.makeText(getBaseContext(),"推薦4",Toast.LENGTH_SHORT).show();
                            break;
                        case R.id.rb5:
                            vp.setCurrentItem(4);
                            Toast.makeText(getBaseContext(),"推薦5",Toast.LENGTH_SHORT).show();
                            break;
                        case R.id.rb6:
                            Toast.makeText(getBaseContext(),"推薦6",Toast.LENGTH_SHORT).show();
                            break;
                        case R.id.rb7:
                            Toast.makeText(getBaseContext(),"推薦7",Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            }
        });

    }

    public  void  vpInit(){
        img=new ArrayList<>();
       // View view=getLayoutInflater().inflate(R.layout.activity_list4,null);
       // ListView lv=(ListView)view.findViewById(R.id.lv);
//  View view=LayoutInflater.from(this).inflate(R.layout.activity_list4,null);
        View view=LayoutInflater.from(this).inflate(R.layout.listview4_layout,null);
        ListView lv=(ListView)view;
        List<new4> list =new ArrayList();//ListView第一個。
        New4 ns=new New4("X","薦","21323","人民網",R.mipmap.a,R.mipmap.b,R.mipmap.c,0,"n001","剛剛","新聞1");
        list.add(ns);
        ns=new New4("","","","百度網",0,0,0,R.mipmap.b,"n001","1小時","新聞2");
        list.add(ns);
        ns=new New4("","","","人民網",0,0,0,R.mipmap.c,"n001","2小時","新聞3");
        list.add(ns);
        ns=new New4("X","薦","21323","人民網",R.mipmap.a,R.mipmap.b,R.mipmap.c,0,"n001","剛剛","新聞1");
        list.add(ns);
        ns=new New4("","","","百度網",0,0,0,R.mipmap.b,"n001","1小時","新聞2");
        list.add(ns);
        ns=new New4("","","","人民網",0,0,0,R.mipmap.c,"n001","2小時","新聞3");
        list.add(ns);
        lv.setAdapter(new MyList4MultLayoutAdapter(list, this));
        img.add(lv);

       // view=getLayoutInflater().inflate(R.layout.activity_list4,null);
       // lv=(ListView)view.findViewById(R.id.lv);

        lv= (ListView) LayoutInflater.from(this).inflate(R.layout.listview4_layout,null);
        list =new ArrayList();
        ns=new New4("X","薦","21323","人民網",R.mipmap.a,R.mipmap.b,R.mipmap.c,0,"n001","剛剛","新聞1");
        list.add(ns);
        ns=new New4("","","","百度網",0,0,0,R.mipmap.b,"n001","1小時","新聞2");
        list.add(ns);
        ns=new New4("","","","百度網",0,0,0,R.mipmap.b,"n001","1小時","新聞2");
        list.add(ns);
        ns=new New4("X","薦","21323","人民網",R.mipmap.a,R.mipmap.b,R.mipmap.c,0,"n001","剛剛","新聞1");
        list.add(ns);
        ns=new New4("","","","百度網",0,0,0,R.mipmap.b,"n001","1小時","新聞2");
        list.add(ns);
        lv.setAdapter(new MyList4MultLayoutAdapter(list,this));
        img.add(lv);


       // view=getLayoutInflater().inflate(R.layout.activity_list4,null);
        // lv=(ListView)view.findViewById(R.id.lv);

        lv= (ListView) LayoutInflater.from(this).inflate(R.layout.listview4_layout,null);
        list =new ArrayList();
        ns=new New4("X","薦","21323","人民網",R.mipmap.a,R.mipmap.b,R.mipmap.c,0,"n001","剛剛","新聞1");
        list.add(ns);
        ns=new New4("","","","百度網",0,0,0,R.mipmap.b,"n001","1小時","新聞2");
        list.add(ns);
        ns=new New4("","","","百度網",0,0,0,R.mipmap.b,"n001","1小時","新聞2");
        list.add(ns);
        ns=new New4("X","薦","21323","人民網",R.mipmap.a,R.mipmap.b,R.mipmap.c,0,"n001","剛剛","新聞1");
        list.add(ns);
        ns=new New4("","","","百度網",0,0,0,R.mipmap.b,"n001","1小時","新聞2");
        list.add(ns);
        lv.setAdapter(new MyList4MultLayoutAdapter(list,this));
        img.add(lv);


        lv= (ListView) LayoutInflater.from(this).inflate(R.layout.listview4_layout,null);
        list =new ArrayList();
        ns=new New4("X","薦","21323","人民網",R.mipmap.a,R.mipmap.b,R.mipmap.c,0,"n001","剛剛","新聞1");
        list.add(ns);
        ns=new New4("","","","百度網",0,0,0,R.mipmap.b,"n001","1小時","新聞2");
        list.add(ns);
        ns=new New4("X","薦","21323","人民網",R.mipmap.a,R.mipmap.b,R.mipmap.c,0,"n001","剛剛","新聞1");
        list.add(ns);
        ns=new New4("","","","百度網",0,0,0,R.mipmap.b,"n001","1小時","新聞2");
        list.add(ns);
        lv.setAdapter(new MyList4MultLayoutAdapter(list,this));
        img.add(lv);


        lv= (ListView) LayoutInflater.from(this).inflate(R.layout.listview4_layout,null);
        list =new ArrayList();
        ns=new New4("X","薦","21323","人民網",R.mipmap.a,R.mipmap.b,R.mipmap.c,0,"n001","剛剛","新聞1");
        list.add(ns);
        ns=new New4("","","","百度網",0,0,0,R.mipmap.b,"n001","1小時","新聞2");
        list.add(ns);
        ns=new New4("X","薦","21323","人民網",R.mipmap.a,R.mipmap.b,R.mipmap.c,0,"n001","剛剛","新聞1");
        list.add(ns);
        ns=new New4("","","","百度網",0,0,0,R.mipmap.b,"n001","1小時","新聞2");
        list.add(ns);
        lv.setAdapter(new MyList4MultLayoutAdapter(list,this));
        img.add(lv);


        //節省資源
        mp=new MyTopNewsAdapter(img);
        //控件和適配器關聯起來
        vp.setAdapter(mp);
        //默認選中0
        vp.setCurrentItem(0);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                RadioButton rb=(RadioButton)rg.getChildAt(position);
                rb.setChecked(true);

            }


            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }


//    public  void intiRadioButten(){
//        for(int i=0;i<channe1.length;i++){ radiobutton="" rb="(RadioButton)getLayoutInflater().inflate(R.layout.radio_butten,null);" pre="">


    
        
        
        
    
    
        
            
            
            
            
            
            
            
            
            
            

        
    
    

        
        
    
    

    

\

 

 

\

 

 

\

 

 

\

 

 

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