Android为ListView的Item设置不同的布局
发布日期:2021-06-30 11:13:46 浏览次数:2 分类:技术文章

本文共 5747 字,大约阅读时间需要 19 分钟。

MainActivity如下:

package cc.testlistview;import java.util.ArrayList;import java.util.HashMap;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import android.app.Activity;/** * Demo描述: * 为ListView的Item设置不同的布局. * 例如在该例子中ListView的第一个Item显示一张 * 图片,其余的Item都显示文字. *  * 为了达到此目的需要重写BaseAdapter中的 * 1 getViewTypeCount()和getItemViewType(int position)方法. *   1.1在getViewTypeCount中指定一共有几种不同的item *      在此返回2即可. *   1.2在getItemViewType(int position)中需要依据position的不同 *      返回不同的Type. * 2 在getView()方法中针对不同的Type为Item设置布局 *   2.1得到当前位置(position)时的Type即代码: *       currentType= getItemViewType(position); *   2.2依据Type的不同为Item设置布局 *  * 参考资料: * 1 http://blog.csdn.net/yueyue369/article/details/6115552 * 2 http://blog.sina.com.cn/s/blog_5da93c8f0100wx4v.html *   Thank you very much */public class MainActivity extends Activity {    private ListView mListView;    private HashMap
mHashMap; private ArrayList
> mArrayList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mArrayList=new ArrayList
>(); mListView=(ListView) findViewById(R.id.listview); addDataForListView(); mListView.setAdapter (new ListViewAdapter(MainActivity.this, mArrayList, R.layout.othersitem, new String []{"content"}, new int []{R.id.textView})); mListView.setOnItemClickListener(new ItemClickListenerImpl()); } private void addDataForListView(){ for (int i = 0; i < 30; i++) { mHashMap=new HashMap
(); mHashMap.put("content", "This is ---> "+i); mArrayList.add(mHashMap); } } private class ItemClickListenerImpl implements OnItemClickListener { @Override public void onItemClick(AdapterView
parent, View view, int position,long arg) { System.out.println("OnItemClickListener position="+position); } }}

ListViewAdapter如下:

package cc.testlistview;import java.util.List;import java.util.Map;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class ListViewAdapter extends BaseAdapter {	private List
> mArrayList; private int resource; private LayoutInflater mLayoutInflater; private final int TYPE_COUNT=2; private final int FIRST_TYPE=0; private final int OTHERS_TYPE=1; private int currentType; public ListViewAdapter(Context context,List
> data, int resource, String[] from,int[] to) { this.mArrayList=data; this.resource=resource; mLayoutInflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { if (mArrayList==null) { return 0; } else { return (mArrayList.size()+1); } } @Override public Object getItem(int position) { if (mArrayList==null) { return null; } else { if (position>0) { return mArrayList.get(position-1); } else { return mArrayList.get(position+1); } } } @Override public long getItemId(int position) { return position; } /// @Override public int getViewTypeCount() { return TYPE_COUNT; } @Override public int getItemViewType(int position) { if (position==0) { return FIRST_TYPE; } else { return OTHERS_TYPE; } } /// @Override public View getView(int position, View convertView, ViewGroup parent) { View firstItemView = null; View othersItemView=null; //获取到当前位置所对应的Type currentType= getItemViewType(position); System.out.println("type="+currentType); if (currentType== FIRST_TYPE) { firstItemView = convertView; FirstItemViewHolder firstItemViewHolder=null; if (firstItemView==null) { System.out.println("firstItemView==null "); firstItemView = mLayoutInflater.inflate(R.layout.firstitem,null); firstItemView.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { System.out.println("=====click first item======="); } }); firstItemViewHolder=new FirstItemViewHolder(); firstItemViewHolder.imageView=(ImageView) firstItemView.findViewById(R.id.imageView); firstItemView.setTag(firstItemViewHolder); } else { System.out.println("firstItemView!=null "); System.out.println("111 getClass="+firstItemView.getTag().getClass().toString()); firstItemViewHolder=(FirstItemViewHolder) firstItemView.getTag(); } if (firstItemViewHolder.imageView!=null) { firstItemViewHolder.imageView.setImageResource(R.drawable.ic_launcher); } convertView=firstItemView; } else { othersItemView = convertView; OthersViewHolder othersViewHolder=null; if (othersItemView==null) { System.out.println("othersItemView==null "); othersItemView = mLayoutInflater.inflate(R.layout.othersitem,null); othersViewHolder=new OthersViewHolder(); othersViewHolder.textView=(TextView) othersItemView.findViewById(R.id.textView); othersItemView.setTag(othersViewHolder); } else { System.out.println("othersItemView!=null "); System.out.println("222 getClass="+othersItemView.getTag().getClass().toString()); othersViewHolder=(OthersViewHolder) othersItemView.getTag(); } if (mArrayList!=null) { if (othersViewHolder.textView!=null) { othersViewHolder.textView.setText((String)(mArrayList.get(position-1).get("content"))); } } convertView=othersItemView; } return convertView; } //第一个Item的ViewHolder private class FirstItemViewHolder{ ImageView imageView; } //除第一个Item以外其余Item的ViewHolder private class OthersViewHolder{ TextView textView; }}

 

转载地址:https://it9527.blog.csdn.net/article/details/12419463 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Fragment详解(三)--->横竖屏幕切换完整示例
下一篇:利用ShapeDrawable自定义控件的shape

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月18日 10时35分20秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

【解决错误】AttributeError: module ‘scipy.misc‘ has no attribute ‘imread‘ 2019-04-30
【解决错误】复现RCAN的时候遇到了ImportError: cannot import name ‘_update_worker_pids’ from ‘torch._C’ 2019-04-30
【解决错误】ModuleNotFoundError: No module named ‘skimage‘ 2019-04-30
【深度学习笔记】pytorch的点乘(dot product) 2019-04-30
【深度学习笔记】残差 2019-04-30
【错误解决】cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\sr 2019-04-30
【python学习笔记】读取指定文件夹中的图片,结合边缘保留滤波EPF 2019-04-30
【工具和环境】Linux下安装pycharm 2019-04-30
【Accumulation】The last two sentences of the abstract 2019-04-30
【Accumulation】The definition of SISR 2019-04-30
【工具与环境】Windows下安装Sublime Text 3 2019-04-30
【解决错误】ValueError: some of the strides of a given numpy array are negative. 2019-04-30
【工具与环境】Excel中批量插入行 2019-04-30
【个人实验注意事项】 2019-04-30
【解决错误】ModuleNotFoundError: No module named ‘tqdm‘ 2019-04-30
【解决错误】ModuleNotFoundError: No module named ‘PIL‘ 2019-04-30
【学习笔记】对vanilla的一些个人理解 2019-04-30
【解决错误】json.decoder.JSONDecodeError: Expecting value: line 11 column 14 (char 82) 2019-04-30
【解决错误】The size of tensor a (8) must match the size of tensor b (64) at non-singleton dimension 1 2019-04-30
word文档中实现目录索引中标题加粗,前导符和页码不加粗 2019-04-30