Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中的getViewType中插入值?_Android - Fatal编程技术网

如何在android中的getViewType中插入值?

如何在android中的getViewType中插入值?,android,Android,我正在处理listview,我有一个适配器,我想在其中显示两个视图。为此,我将使用带有自定义基本适配器的listview。我的代码运行正常,但我不知道必须在getViewType()中插入什么值 代码: 私有上下文mContext; 私人名单; 私有字符串标记=cnewsadter.class.getSimpleName(); 公共CNEWSADTER(上下文m_上下文,列表项){ this.mContext=m_Context; mItems=项目; } @凌驾 public int getI

我正在处理listview,我有一个适配器,我想在其中显示两个视图。为此,我将使用带有自定义基本适配器的
listview
。我的代码运行正常,但我不知道必须在
getViewType()中插入什么值

代码:

私有上下文mContext;
私人名单;
私有字符串标记=cnewsadter.class.getSimpleName();
公共CNEWSADTER(上下文m_上下文,列表项){
this.mContext=m_Context;
mItems=项目;
}
@凌驾
public int getItemViewType(int位置){
返回2;
}
@凌驾
public int getViewTypeCount(){
返回2;
}
@凌驾
public int getCount(){//get总数组列表大小
返回mItems.size();
}
@凌驾
公共对象getItem(int位置){//get项目在数组列表中的位置
返回mItems.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@抑制警告(“弃用”)
@SuppressLint({“SetTextI18n”,“InflateParams”})
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视图v=转换视图;
int type=getItemViewType(位置);
如果(v==null){
//根据视图类型对布局进行充气
LayoutFlater充气器=(LayoutFlater)parent.getContext().getSystemService(Context.LAYOUT\u充气器\u服务);
如果(类型==0){
//用图像放大布局
v=充气机。充气(R.layout.news\u item\u为\u布局,父项,false);
}否则{
v=充气机。充气(R.layout.vcom_布局,父级,假);
}
}
最终新闻片段m=mItems.get(position);
ImageView m_图标=(ImageView)v.findViewById(R.id.imgNewsIcon);
TextView m_Titletext=(TextView)v.findViewById(R.id.txtNewsTitle);
TextView m_Description=(TextView)v.findViewById(R.id.Description);
按钮m_ActionButton=(按钮)v.findViewById(R.id.actionBtn);
如果(类型==0){
RatingBar m_RatingBar=(RatingBar)v.findViewById(R.id.pop_RatingBar);
字符串s=字符串.valueOf((float)m.s_szrating);
如果(!s.equalsIgnoreCase(“”){
m_额定杆设定值((浮动)m.s_额定值);
}
}
m_Titletext.setText(m.s_sztitle);
m_Description.setText(m.s_szdescription);
m_ActionButton.setText(m.s_szcta);
m_ActionButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent browserIntent=新的意图(Intent.ACTION\u视图,
parse(m.s_szlandingUrl));
v、 getContext().startActivity(BrowserContent);
}
});
试一试{
字符串url=m.s_szicon;
if(url!=null&&!url.equals(m_Icon.getTag()){
m_Icon.setTag(url);
新建CDownloadImageTask(m_图标)
.执行(m.s_szicon);
}
}捕获(例外e){
e、 printStackTrace();
}
返回v;
}

首先,如果您想查看其他视图,即先查看一个视图,然后查看另一个视图,则必须通过某个id来管理所需的视图。 这会有帮助的

  @Override
    public int getItemViewType(int position) {
        if(postion%2==0){
            return 2;
         }
        return 1;
    }
现在当你把视野放大时

int type = getItemViewType(position);
    if (v == null) {
        // Inflate the layout according to the view type
        LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (type == 0) {
            // Inflate the layout with image
            v = inflater.inflate(R.layout.news_item_for_layout, parent, false);
        } else {
            v = inflater.inflate(R.layout.vcom_layout, parent, false);
        }
    }
更改if条件if(type==2)else其他视图


如果您通过一些或字符串进行管理,则根据更改getItemType()方法中的条件

如果您有两个不同的视图,则基本上您有两个不同的视图类型

因此,在适配器中定义两种视图类型

private static final VIEW_TYPE_FIRST = 0;
private static final VIEW_TYPE_SECOND = 1;
这就是如何识别这两种视图类型的方法

现在,您可以使用getItemViewType()告诉适配器哪个视图应该为哪个位置充气

假设您希望仅显示第一个项目的第一个视图,而将所有其他项目显示为第二个项目

@Override
public int getItemViewType(int position) {
if(position == 0){
   return VIEW_TYPE_FIRST;
}
   return VIEW_TYPE_SECOND;
}

因此,适配器将仅为第一个位置充气第一个视图类型,为所有其他位置充气第二个视图类型。

如果要更改位置0上的布局,请执行此操作

 @Override
    public int getItemViewType(int position) {

         if(position==0)
         return 0;
         else
         return 1;
    }



       @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v = convertView;
            int type = getItemViewType(position);// **this return 0 if position is 0 else return 1**

            if (v == null) {
                // Inflate the layout according to the view type
                LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                if (type == 0) {
                    // Inflate the layout with image
                    v = inflater.inflate(R.layout.news_item_for_layout, parent, false);
                } else {
                    v = inflater.inflate(R.layout.vcom_layout, parent, false);
                }
            }

            //other code

       }

在要更改布局的位置处,从getItemViewType返回该位置。。你可以用这个答案检查你的问题是否解决了?那么请考虑接受并投票支持这个答案。
 @Override
    public int getItemViewType(int position) {

         if(position==0)
         return 0;
         else
         return 1;
    }



       @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v = convertView;
            int type = getItemViewType(position);// **this return 0 if position is 0 else return 1**

            if (v == null) {
                // Inflate the layout according to the view type
                LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                if (type == 0) {
                    // Inflate the layout with image
                    v = inflater.inflate(R.layout.news_item_for_layout, parent, false);
                } else {
                    v = inflater.inflate(R.layout.vcom_layout, parent, false);
                }
            }

            //other code

       }