如何从底部停止计算列表视图的位置。

时间:2015-07-09 14:44:59

标签: android listview

我有16个项目的列表视图,所有视图在活动开始时创建完美。活动开始列表视图从顶部开始,位置为零。当我滚动平滑时,所有视图保持不变。但我的问题是当列表视图到达其最后位置并试图拉动它时,它从底部开始计数。这意味着它将第16个位置设为0(零)。

我正在使用文本视图的文本值在前三分之一视图上更新我的前三分之一视图。当活动开始工作完美时,但上面我告诉我到达最后一个位置或硬拉我的列表视图时,它开始从底部创建视图并在底部第三视图中设置第三个视图的值。

这是我的适配器get-view代码。

  @Override
 public View getView( int position, View convertView, ViewGroup parent)
 { 

  sharedpreferences = context.getSharedPreferences("ua_animation", Context.MODE_PRIVATE);
  sharedPreferences = context.getSharedPreferences(MyPREFERENCESS, Context.MODE_PRIVATE);
  Holder holder = null;
  c = unlock_animation.b;  
      sharedPreferences.edit().putInt("u_anim", c).commit();
      b1 = sharedPreferences.getInt("u_anim", 0);
      sharedPreferences.edit().putInt("sto_time", sc  reen_time_out.pt).commit();

      i4 = sharedPreferences.getInt("ptm", 0);
      i2 = sharedPreferences.getInt("switch_key", 0);
      if(i2 == 1){  tgpref = true; }else { tgpref = false; }

  LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    //convertView = null;

    if(convertView == null){

        convertView = mInflater.inflate(R.layout.screenlock_adapter, parent,false);

        holder = new Holder();      
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hsw = (Switch) convertView.findViewById(R.id.scl_sw);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);        
        convertView.setTag(holder);               
    } else{                     
        holder = (Holder) convertView.getTag();
    }      


    if(position == 0){

    holder.htv.setText(Listitem[position]);        
    holder.hdesc.setVisibility(View.GONE);
    holder.hsw.setVisibility(View.GONE);
    Picasso.with(this.context).load(R.drawable.dir_arrow).into(holder.himg);    

    }
    if(position == 1){            

        holder.htv.setText(Listitem[position]);            
        Picasso.with(this.context).load(R.drawable.dir_arrow).into(holder.himg);
        holder.hsw.setVisibility(View.VISIBLE);            
        //holder.himg.setVisibility(View.GONE);


        holder.hsw.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // do something, the isChecked will be
                // true if the switch is in the On position
                 if(isChecked ){

                    posi = 1; 
                    s.setValues(1);

                    }else{
                        posi = 0;
                        s.setValues(0);

                    }
            }
        });
        if(tgpref){
            holder.hsw.setChecked(true);

        }else
        {
            holder.hsw.setChecked(false);               
        }

    }
    if(position == 2){

        holder.htv.setText(Listitem[position]);
        Picasso.with(this.context).load(R.drawable.dir_arrow).into(holder.himg);
        holder.hsw.setVisibility(View.GONE);
        i1 = sharedPreferences.getInt("value", 0);
        switch(i1){

        case 1:
            holder.hdesc.setText("shake");
            break;
        case 2:
            holder.hdesc.setText("single tap");
            break;
        case 3:
            holder.hdesc.setText("Double tap");
            break;
        }

    }
    if(position == 3){

        holder.htv.setText(Listitem[position]);
        holder.hdesc.setVisibility(View.GONE);
        holder.hsw.setVisibility(View.GONE);
        Picasso.with(this.context).load(R.drawable.dir_arrow).into(holder.himg);
        holder.himg.setVisibility(View.VISIBLE);

    }
    if(position == 4){

        holder.htv.setText(Listitem[position]);
        holder.hdesc.setVisibility(View.GONE);
        holder.hsw.setVisibility(View.VISIBLE);
        Picasso.with(this.context).load(R.drawable.dir_arrow).into(holder.himg);
        //holder.himg.setVisibility(View.GONE);
        holder.hsw.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked ){

                 vibratorv = 1;
                    v.setValue(1);

                    }else{
                    vibratorv = 0;
                        v.setValue(0);
                    }
                }
        });
        if(vbool){
            holder.hsw.setChecked(true);                
        }else{
            holder.hsw.setChecked(false);
        }
    }
    if(position == 5){

        holder.htv.setText(Listitem[position]);
        Picasso.with(this.context).load(R.drawable.dir_arrow).into(holder.himg);
        holder.hsw.setVisibility(View.GONE);
        holder.hdesc.setText(R.string.desc);

    }
    if(position == 6){

        holder.htv.setText(Listitem[position]);  
        Picasso.with(this.context).load(R.drawable.dir_arrow).into(holder.himg);
        holder.hdesc.setVisibility(View.VISIBLE);
        holder.hsw.setVisibility(View.GONE);

        if(i5 == 0){holder.hdesc.setText("Default");}
        if(i5 == 1){holder.hdesc.setText("0s");}
        if(i5 == 2){holder.hdesc.setText("3s");}
        if(i5 == 3){holder.hdesc.setText("5s");}
        if(i5 == 4){holder.hdesc.setText("15s");}
        if(i5 == 5){holder.hdesc.setText("30s");}
        if(i5 == 6){holder.hdesc.setText("1m");}

    }
    if(position == 7){

        Toast.makeText(context, "i4 : "+i4, Toast.LENGTH_SHORT).show();
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setVisibility(View.VISIBLE);           
        holder.hsw.setVisibility(View.GONE);        
        Picasso.with(this.context).load(R.drawable.dir_arrow).into(holder.himg);
        if(i4 == 0){holder. hdesc.setText("Default");}
        if(i4 == 1){holder.hdesc.setText("0s");}
        if(i4 == 2){holder.hdesc.setText("3s");}
        if(i4 == 3){holder.hdesc.setText("5s");}
        if(i4 == 4){holder.hdesc.setText("15s");}
        if(i4 == 5){holder.hdesc.setText("30s");}
        if(i4 == 6){holder.hdesc.setText("1m");}

    }

    if(position == 8){

        holder.htv.setText(Listitem[position]);
        Picasso.with(this.context).load(R.drawable.dir_arrow).into(holder.himg);
        holder.hsw.setVisibility(View.GONE);            
        switch(b1){

        case 0:
            holder.hdesc.setText("None");       
            break;
        case 1:
            holder.hdesc.setText("Rotate");
            break;
        case 2:
            holder.hdesc.setText("Fade");
            break;
        case 3:
            holder.hdesc.setText("Zoom");
            break;
        case 4:
            holder.hdesc.setText("Bounce");
            break;
        case 5:
            holder.hdesc.setText("Blink");
            break;
        }

    }
    if(position == 9){
        Picasso.with(this.context).load(R.drawable.dir_arrow).into(holder.himg);
        holder.htv.setText(Listitem[position]);
        holder.hsw.setVisibility(View.GONE);
        holder.hdesc.setText(R.string.desc);


    }
    if(position == 10){

        holder.htv.setText(Listitem[position]);
        Picasso.with(this.context).load(R.drawable.dir_arrow).into(holder.himg);
        //holder.himg.setVisibility(View.GONE);         
        holder.hdesc.setVisibility(View.GONE);
        holder.hsw.setVisibility(View.VISIBLE);


        holder.hsw.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked ){

                 //sb = "1";
                    sb.setstatusbar(1);

                    }else{
                   // sb = "0";
                        sb.setstatusbar(0);
                    }
                }
        });
        if(statusbool){
            holder.hsw.setChecked(true);                
        }else{
            holder.hsw.setChecked(false);
        }

    }
    if(position ==11){

        holder.htv.setText(Listitem[position]);
        Picasso.with(this.context).load(R.drawable.dir_arrow).into(holder.himg);
        holder.hdesc.setVisibility(View.GONE);
        holder.hsw.setVisibility(View.GONE);


    }
    if(position == 12){

        holder.htv.setText(Listitem[position]);         
        Picasso.with(this.context).load(R.drawable.dir_arrow).into(holder.himg);
        holder.hdesc.setVisibility(View.GONE);
        holder.hsw.setVisibility(View.GONE);


    }
    if(position == 13){

        holder.htv.setText(Listitem[position]);                 
        Picasso.with(this.context).load(R.drawable.dir_arrow).into(holder.himg);
        holder.hdesc.setVisibility(View.VISIBLE);
        holder.hsw.setVisibility(View.GONE);

    }
    if(position == 14){

        holder.htv.setText(Listitem[position]);         
        Picasso.with(this.context).load(R.drawable.dir_arrow).into(holder.himg);
        holder.hdesc.setVisibility(View.GONE);
        holder.hsw.setVisibility(View.GONE);

    }
    if(position == 15){

        holder.htv.setText(Listitem[position]);         
        Picasso.with(this.context).load(R.drawable.dir_arrow).into(holder.himg);
        holder.hdesc.setVisibility(View.GONE);
        holder.hsw.setVisibility(View.GONE);

    }

    return convertView;

  }

 public void posi(){
     sharedpreferences.edit().putInt("ua_anim", unlock_animation.b).commit();
     bc = sharedpreferences.getInt("ua_anim", 0);
    }
 } 

1 个答案:

答案 0 :(得分:0)

最后我得到了我的问题解决方案。我有转换视图到常量值null convertview = null;和活动,我正在使用此方法更新我的列表视图适配器。

private void updateData() {

  runOnUiThread(new Runnable() {
      @Override
      public void run() {
              ((BaseAdapter) adapter).notifyDataSetChanged();
     }
  });
}

当我从其他活动接收数据时调用此方法。