当单行包含TextView和EditText时,Listview中的滚动问题

时间:2016-03-28 12:32:56

标签: android listview

我有一行包含TextViewEditText,最初EditText不可见,当用户点击listview的单行时,EditText变为可见,如果EditText对于指定位置可见,然后当用户再次点击时,EditText将变为不可见。

我可以在用户点击时切换到EditText,但是我无法知道EditText何时可见,而用户Scrool然后其他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;
    }

// single_row_for_type_of_truck

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll4"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="50dp"
       >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1"
        android:gravity="center_vertical|center_horizontal"

        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:gravity="center|left"
            android:layout_gravity="center"
            android:layout_weight=".9"
            android:padding="@dimen/text_padding"
            android:text="TextView" />


        <EditText
            android:id="@+id/et_nmbr_of_truck_id"
            android:layout_width="50dp"
            android:layout_height="25dp"
            android:padding="2dp"
            android:background="@drawable/rounded_edge_edittext"
            android:textSize="14dp"
            android:visibility="invisible"
            android:layout_marginRight="10dp"
            android:gravity="left"
            android:saveEnabled="true"
            android:numeric="integer"
            android:inputType="number"
            android:layout_weight=".1"/>

    </LinearLayout>
    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@color/lightgray"/>


</LinearLayout>

2 个答案:

答案 0 :(得分:1)

尝试创建布尔数组,该数组将存储编辑文本的可见性状态。在适配器的构造函数中添加此代码:

boolean[] isEditTextVisible = new boolean[list.size()];
        for (int i=0; i < isEditTextVisible.length; i++) {
            isEditTextVisible[i] = false;
        }

修改getView()方法:

    ...

            if (isEditTextVisible[position]){
                holder.nmbrOfTruck.setVisibility(View.VISIBLE);
            } else {
                holder.nmbrOfTruck.setVisibility(View.INVISIBLE);
            }

            convertView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (holder.nmbrOfTruck.isShown()) {
                        holder.nmbrOfTruck.setVisibility(View.INVISIBLE);
                        isEditTextVisible[position] = false;
                    } else {
                        holder.nmbrOfTruck.setVisibility(View.VISIBLE);
                        isEditTextVisible[position] = true;
                    }

                }
            });
    ...

答案 1 :(得分:0)

如果我理解你的问题,你可以在你的适配器类中使用一个简单的SparseBooleanArray来存储&#39;编辑模式&#39;对某个列表位置有效。

要存储已激活的编辑模式,请使用<<YourSparseBooleanArray>>.put(position, true)

getView()方法中,您必须区分<<YourSparseBooleanArray>>.get(position)是否返回true(=&gt;编辑模式)或false(TextView模式)。

并且不要忘记视图正在被回收,这意味着您必须在调用getView()的每种情况下设置当前视图的可见性。 (如果视图作为getView()参数传递给convertView,则不能认为该视图是可见的