列表视图中的滚动问题

时间:2016-03-28 09:27:41

标签: android listview

//从片段中填充ArrayList

gridmodel = new TypeTruckPogo("Truck 14 wheel",-1,false);
        list.add(gridmodel);
        gridmodel = new TypeTruckPogo("Truck 16 wheel",-1,false);
        list.add(gridmodel);

        final TypeOfTruckAdapter truckAdapter=new TypeOfTruckAdapter(context, list);
        truckListView.setAdapter(truckAdapter);

//我的POGO CLASS

public class TypeTruckPogo{
    String typeOfTruckName;
    int nmbrOfTruck;
    boolean isEditTextVisiable;

    public TypeTruckPogo(String typeOfTruckName,int nmbrOfTruck,boolean isEditTextVisiable){
        this.typeOfTruckName=typeOfTruckName;
        this.nmbrOfTruck=nmbrOfTruck;
        this.isEditTextVisiable=isEditTextVisiable;
    }

    public String getTypeOfTruckName() {
        return typeOfTruckName;
    }

    public String setTypeOfTruckName(String typeOfTruckName) {
        this.typeOfTruckName = typeOfTruckName;
        return typeOfTruckName;
    }

    public int getNmbrOfTruck() {
        return nmbrOfTruck;
    }

    public Integer setNmbrOfTruck(int nmbrOfTruck) {
        this.nmbrOfTruck = nmbrOfTruck;
        return nmbrOfTruck;
    }

    public boolean isEditTextVisiable() {
        return isEditTextVisiable;
    }

    public Boolean setIsEditTextVisiable(boolean isEditTextVisiable) {
        this.isEditTextVisiable = isEditTextVisiable;
        return isEditTextVisiable;
    }
}

//我的适配器类,这个类在列表中有20项,第一次显示8项,当用户按下单行时,editText变为可见,如果用户按两次相同的位置,则为editText变得不可思议,但问题是,当用户点击任何位置时(假设他点击位置0)然后editText是可见的但是当我向下滚动时,其他editText也会在某个时间位置8变得可见15.ANY HELP APPRECIATED !! !单行包含textView和editText

public class TypeOfTruckAdapter extends BaseAdapter{
List<TypeTruckPogo> list;
Context context;
LayoutInflater inflater;
public String[] Current;
TypeTruckPogo typeTruckPogo;
public static HashMap<Integer,String> truckHashMap=new HashMap<Integer,String>();
public TypeOfTruckAdapter( Context context,List<TypeTruckPogo> list) {
    this.list = list;
    this.context = context;
    for(int i=0;i<list.size();i++)
    {
        truckHashMap.put(i,"");
    }
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}



class Viewholder{
    TextView name;
    EditText nmbrOfTruck;
    int ref;

    Viewholder(View view) {

        name = (TextView) view.findViewById(R.id.textView1);
        nmbrOfTruck = (EditText) view.findViewById(R.id.et_nmbr_of_truck_id);
    }

}
@Override
public View getView(final int position, View convertView,  ViewGroup parent) {
    final Viewholder holder;
    typeTruckPogo = list.get(position);
    if(convertView==null){
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = inflater.inflate(R.layout.single_row_for_type_of_truck, parent, false);
        holder = new Viewholder(convertView);
        convertView.setTag(holder);
    }else{
        holder = (Viewholder)convertView.getTag();
    }
    // For position zero i have to set the text color to blue color,else dark gray color
    if(position==0){
        holder.name.setTextColor(context.getResources().getColor(R.color.blue_color));
    }else{
        holder.name.setTextColor(context.getResources().getColor(R.color.darkgray));
    }
    // setting the name on the textview
    holder.name.setText(list.get(position).getTypeOfTruckName());
    //setting the tag on edittext
    holder.nmbrOfTruck.setTag(position);
    //setting the viewholder position
    holder.ref = position;

   // clicking on listview making edittext to appear (initially edittext is invisiable)
    // if edittext is visiable make it invisiable and if it is invisiable make it visiable
    convertView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (v.findViewById(R.id.et_nmbr_of_truck_id).getVisibility() == View.VISIBLE) {
                v.findViewById(R.id.et_nmbr_of_truck_id).setVisibility(View.INVISIBLE);
            } else {
                v.findViewById(R.id.et_nmbr_of_truck_id).setVisibility(View.VISIBLE);
            }

        }
    });
    // if user write on editText save the input by the user in specified position
    //truckHashMap is hashmap where I saving the position as a key and value as the user Input

    holder.nmbrOfTruck.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start,
                                  int before, int count) {

        }

        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after) {
            // TODO Auto-generated method stub

        }

        public void afterTextChanged(Editable s) {
            Current = new String[holder.ref];

            truckHashMap.put(position, s.toString().trim());
        }
    });
    // Setting the User Input at specified Position
    holder.nmbrOfTruck.setText(truckHashMap.get(position));

    Config.colorFont(context, null, holder.name, null);
    return convertView;
}

2 个答案:

答案 0 :(得分:2)

您必须使用在您的适配器中设置默认文本颜色的else语句:

...
if(position==0){
            holder.name.setTextColor(context.getResources().getColor(R.color.blue_color));
        } else {
          // here set your default text color black for example
          holder.name.setTextColor(context.getResources().getColor(R.color.black_color));
}
...

希望它有所帮助!

答案 1 :(得分:0)

您的else条件?

    if(position==0)
    {
              holder.name.setTextColor(context.getResources().getColor(R.color.blue_color));
    } else {

    // Calling when if Condition not Satisfied .
             holder.name.setTextColor(context.getResources().getColor("Your_Color"));
            }