当前位置:天才代写 > tutorial > 安卓教程 > ListView实现加载不同Item布局

ListView实现加载不同Item布局

2021-02-26 14:25 星期五 所属: 安卓教程 浏览:58

各位好!,今日小结一片ListView载入不一样Item合理布局的blog,在Android的app开发之中ListView好像是基础配备,一般大家的ListView主要是做为目录种类的数据展示,一般的展现数据信息非常好做,只不过便是运用Volley、Xutils这些架构要求,随后Gosn或别的的分析,ImageLoader载入并表明照片,实际上今日也用这种可是要做一些解决,新浪新闻我觉得绝大多数人都看了,它的ListView展现数据信息的Item的合理布局有几种,而不是一种那麼这类ListView展现的算法设计,是怎样完成的呢,实际上要完成这一实际效果有很多方式,例如:我们可以在adapter中根据type来载入不一样的item合理布局,假如较为简单的话大家有时候还可以运用ScrollView嵌入好几个ListView或是GridView完成,也有便是根据Android5.0的中澳的特点控制RecyclerView来轻轻松松完成,这种方式我能在下面的blog之中解读,今日主要是同过type来完成,根据type来完成载入不一样Item的合理布局有一个益处便是,条理清楚,不易打错(上编码前给各位看个设计效果图)。下边就进到主题风格,上编码…………….

 public class ListViewTypeActivity extends AppCompatActivity {
 
     private ListView listview;
     private Integer[]images2={R.drawable.appmain_subject_1,
                    R.drawable.appmain_subject_1,R.drawable.appmain_subject_1};
private Integer[]images={R.drawable.appmain_subject_1}; public List<DataModel> listDatamodel = new ArrayList<DataModel>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_view_type); InitView(); } public void InitView() { initData(); listview = (ListView) findViewById(R.id.listview); listview.setAdapter(new MyAdapter(listDatamodel,this)); listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(ListViewTypeActivity.this,
                             listDatamodel.get(position).getDmiaoshu() "",Toast.LENGTH_SHORT).show(); } }); } /** 复位数据信息 public void initData() { for (int i = 0; i <1; i ) { DataModel dml = new DataModel(images,"一个美女","一个美女"); listDatamodel.add(dml); } for (int i = 0; i <1 ; i ) { DataModel dml = new DataModel(images2,"三个漂亮美女","三个漂亮美女"); listDatamodel.add(dml); } for (int i = 0; i <2 ; i ) { DataModel dml = new DataModel(images,"一个美女","一个美女"); listDatamodel.add(dml); } for (int i = 0; i <1 ; i ) { DataModel dml = new DataModel(images2,"三个漂亮美女","三个漂亮美女"); listDatamodel.add(dml); } for (int i = 0; i <3 ; i ) { DataModel dml = new DataModel(images,"一个美女","一个美女"); listDatamodel.add(dml); } for (int i = 0; i <1 ; i ) { DataModel dml = new DataModel(images2,"三个漂亮美女","三个漂亮美女"); listDatamodel.add(dml); } }

    Activity的编码比较简单,我不解读了,下面大家看来关键的adapter的编码
 package com.example.joexiang.listviewtypedemo;
 import android.content.Context;
 import android.text.TextUtils;
 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 java.util.List;
 
 /**
  * Created by joe.xiang on 2016/2/15.
  */
 public class MyAdapter extends BaseAdapter {
     public LayoutInflater mInflater;
     public List<DataModel> listDatamodel;
     //载入合理布局种类
     public static final int LAYOUTONE=1;
     public static final int LAYOUTTWO=2;
 
     public MyAdapter(List<DataModel> listDatamodel,Context context) {
         this.listDatamodel = listDatamodel;
         mInflater = LayoutInflater.from(context);
     }
 
     @Override
     public int getCount() {
         return listDatamodel.size();
     }
 
     /**
      *  决策载入那一个种类的Item合理布局
      * @param position
      * @return
      */
     @Override
     public int getItemViewType(int position) {
         DataModel dml= listDatamodel.get(position);
         String dtitle = dml.dtitle.trim();
         if(!TextUtils.isEmpty(dtitle)&&dtitle.equals("三个漂亮美女")){
                 return LAYOUTTWO;
         }else{
             return LAYOUTONE;
         }
     }
 
     /**
      * 
      * @return
      */
     @Override
     public int getViewTypeCount() {
         return 3;
     }
 
     @Override
     public Object getItem(int position) {
         return listDatamodel.get(position);
     }
 
     @Override
     public long getItemId(int position) {
         return position;
     }
 
     @Override
     public View getView(final int position, View convertView, ViewGroup parent) {
         //获得载入合理布局的种类
        int viewType =  getItemViewType(position);
          ViewHolder1 viewHolder1 = null;
           ViewHolder2 viewHolder2 = null;
         if (convertView==null){
          //依据回到种类载入不一样的合理布局文档和建立不一样的缓存文件类ViewHolder
switch (viewType){ case 1: convertView = mInflater.inflate(R.layout.content_list_view_type,null); viewHolder1 = new ViewHolder1(); viewHolder1.imageview = (ImageView) convertView.findViewById(R.id.image); viewHolder1.tv = (TextView) convertView.findViewById(R.id.pname); convertView.setTag(viewHolder1); break; case 2: convertView = mInflater.inflate(R.layout.content_list_view_type2,null); viewHolder2 = new ViewHolder2(); viewHolder2.imageview2 = (ImageView) convertView.findViewById(R.id.image1); viewHolder2.tv2 = (TextView) convertView.findViewById(R.id.pname2); convertView.setTag(viewHolder2); break; } }else{ switch (viewType){ case 1: viewHolder1 = (ViewHolder1)convertView.getTag(); break; case 2: viewHolder2 = (ViewHolder2)convertView.getTag(); break; } }
//依据回到种类来展现不一样的数据信息
switch (viewType){ case 1: Integer[] images = listDatamodel.get(position).getDimage(); for (int i = 0; i <images.length ; i ) { viewHolder1.imageview.setImageResource(images[i]); } viewHolder1.tv.setText(listDatamodel.get(position).getDmiaoshu()); break; case 2: Integer[] images2 = listDatamodel.get(position).getDimage(); for (int i = 0; i <images2.length ; i ) { viewHolder2.imageview2.setImageResource(images2[i]); } viewHolder2.tv2.setText(listDatamodel.get(position).getDmiaoshu()); break; } return convertView; } class ViewHolder1{ TextView tv; ImageView imageview; } class ViewHolder2{ TextView tv2; ImageView imageview2; } }
主要是根据GetItemViewType()来决策载入那一个合理布局,getViewTypeCount()回到表明Item的最合理布局总数,在getView()方式中根据getItemViewType()来得到当今载入的合理布局种类,在依据种类制订载入的合理布局,和相对的缓存文件类,设定值也是根据该回到种类来明确。哼哼到此这篇blog详细介绍结束了,期待能帮上必须协助的人。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

    关键字:

天才代写-代写联系方式