当用户点击第一行时,如何使listview其他行无法点击

时间:2016-03-30 05:40:03

标签: android listview

我需要帮助,当用户点击第一个位置时,我想要其他位置应该是不可点击的,当用户点击第2个,第3个等位置时,我想只让第1个位置不可点击。

我试过了:

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>();
    private String[] arrTemp;
    boolean[] isEditTextVisible;
    int counter=0;
    int bestAvailiableCounter=0;
    public TypeOfTruckAdapter( Context context,List<TypeTruckPogo> list) {
        this.list = list;
        this.context = context;
        for(int i=0;i<list.size();i++)
        {
            truckHashMap.put(i,"");
        }
        arrTemp = new String[list.size()];
       isEditTextVisible = new boolean[list.size()];
        for (int i=0; i < isEditTextVisible.length; i++) {
            isEditTextVisible[i] = false;
        }

    }
    @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;
        LinearLayout ll;
        int ref;

        Viewholder(View view) {

            name = (TextView) view.findViewById(R.id.textView1);
            nmbrOfTruck = (EditText) view.findViewById(R.id.et_nmbr_of_truck_id);
            ll = (LinearLayout) view.findViewById(R.id.ll4);

        }
    }
    @Override
    public View getView(final int position, View convertView,  final 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));
            holder.ll.setBackgroundColor(context.getResources().getColor(R.color.mainBackGroundColor));
        }else{
            holder.name.setTextColor(context.getResources().getColor(R.color.darkgray));
            holder.ll.setBackgroundColor(context.getResources().getColor(R.color.mainBackGroundColor));
        }
        // 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;
        holder.nmbrOfTruck.setVisibility(View.INVISIBLE);
        // Setting the User Input at specified Position
        /*Iterator myVeryOwnIterator = truckHashMap.keySet().iterator();
        while(myVeryOwnIterator.hasNext()) {
            String key=(String)myVeryOwnIterator.next();
            String value=(String)truckHashMap.get(key);

        }*/
        // 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.setText(arrTemp[position]);

        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) {
                arrTemp[holder.ref] = s.toString();
            }
        });


        // holder.nmbrOfTruck.setText(truckHashMap.get(position) + "");
        Log.e("hsh",""+truckHashMap.get(position)+"");





        /*
        below i am facing the scrolling issue, when i scroll then alpha value is not behaving properly it seldom changing.
         */

        // user can press and unpress the single row,if he press single row then edittext is visiable and
        // if EditText is visiable then set TextView Color to white and Background color to dark Gray
        // if position 0 is selected then make other position as Unclickable and make alpha to other position to .2f
        //if other than 0 position is selected ,make 0 position row as unclickable and faint in color(alpha to .2f )for position zero.

        // Here i am binding the data if editText is visiable or not.

        if (isEditTextVisible[position]){
            //holder.nmbrOfTruck.setText(arrTemp[position]);
            Log.e("hshInsid", "" + truckHashMap.get(position) + "");
            Log.e("position", "" + position + "" + isEditTextVisible[position]);
            // if EditText is Visiable for specified position when user click make visiable for binding the data
            // because getView Recycle while scrolling so setting the visibility for specified position
            holder.nmbrOfTruck.setVisibility(View.VISIBLE);

            // comparing zero index row of editText is visiable,if user selected zero index row then set textcolor to white and background color to dark gray

            if(list.get(position).getTypeOfTruckName().equalsIgnoreCase("BEST AVAILABLE")){
                // this is zero index item
                holder.ll.setEnabled(true);
                holder.ll.setAlpha(1);
                holder.ll.setBackgroundColor(context.getResources().getColor(R.color.darkgray));
                holder.name.setTextColor(context.getResources().getColor(R.color.white));



            } else {
                // this is other item then zero index
                holder.ll.setEnabled(true);
                holder.ll.setAlpha(1);
                holder.ll.setBackgroundColor(context.getResources().getColor(R.color.darkgray));
                holder.name.setTextColor(context.getResources().getColor(R.color.white));
            }


        }else {
            // Binding the invisiable editText visibility.
            arrTemp[holder.ref] = null;

            holder.nmbrOfTruck.setVisibility(View.INVISIBLE);
//            truckHashMap.put(position, "");

             // default color of 0 row ,textView blue in color and background is light Gray(mainBackGroundColor)
            if(list.get(position).getTypeOfTruckName().equalsIgnoreCase("BEST AVAILABLE")){
                if(counter==0){
                    holder.ll.setEnabled(true);
                    holder.ll.setAlpha(1);
                    holder.ll.setBackgroundColor(context.getResources().getColor(R.color.mainBackGroundColor));
//                    holder.name.setText(list.get(position).getTypeOfTruckName());
                    holder.name.setTextColor(context.getResources().getColor(R.color.blue_color));


                }else{
                    holder.ll.setEnabled(false);
                    holder.ll.setAlpha(.2f);
                    holder.ll.setBackgroundColor(context.getResources().getColor(R.color.mainBackGroundColor));
                    holder.name.setTextColor(context.getResources().getColor(R.color.blue_color));
                }



            }else{
                // default color other than 0 row ,textView blue in color and background is light Gray(mainBackGroundColor)
                if(bestAvailiableCounter!=0){
                    for (int j = 1; j < parent.getChildCount(); j++) {
                        parent.getChildAt(j).setAlpha(.2f);
                        parent.getChildAt(j).setEnabled(false);
                        holder.ll.setBackgroundColor(context.getResources().getColor(R.color.mainBackGroundColor));
                        holder.name.setTextColor(context.getResources().getColor(R.color.darkgray));
                       // holder.name.setText(list.get(position).getTypeOfTruckName());
                    }
                }else {
                    for (int j = 1; j < parent.getChildCount(); j++) {
                        parent.getChildAt(j).setAlpha(1);
                        parent.getChildAt(j).setEnabled(true);
                        holder.ll.setBackgroundColor(context.getResources().getColor(R.color.mainBackGroundColor));
                        holder.name.setTextColor(context.getResources().getColor(R.color.darkgray));
                       // holder.name.setText(list.get(position).getTypeOfTruckName());
                    }
                }


            }
        }


        holder.ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (holder.nmbrOfTruck.isShown()) {
                    holder.nmbrOfTruck.setVisibility(View.INVISIBLE);
                    isEditTextVisible[position] = false;
                    typeTruckPogo.setIsUserTapped(false);
                    typeTruckPogo.setPosition(position);


                } else {
                    holder.nmbrOfTruck.setVisibility(View.VISIBLE);
                    isEditTextVisible[position] = true;
                    typeTruckPogo.setIsUserTapped(true);
                    typeTruckPogo.setPosition(position);
                    if(position==0){
                        holder.name.setTextColor(context.getResources().getColor(R.color.white));
                        holder.ll.setBackgroundColor(context.getResources().getColor(R.color.darkgray));
                    }


                }
            }
        });


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

3 个答案:

答案 0 :(得分:2)

使用此下方代码并在第一行单击时调用setRowOneClicked,以达到您的要求。

   public class TypeOfTruckAdapter extends BaseAdapter{
        private boolean rowOneClicked;
        //your code
        public void setRowOneClicked(boolean enable){
            rowOneClicked = enable;
            notifyDataSetChanged();
        }

        @Override
        public View getView(final int position, View convertView,  final ViewGroup parent) {
            //your code goes on here
            if(position!=0 && rowOneClicked){
               convertView.setEnabled(false);
            }else{
               convertView.setEnabled(true);
            }
            return convertView;
        }

   }

答案 1 :(得分:0)

在用于填充listview的模型中设置一个布尔标志,类似于isEnabled。当用户单击第一个位置时,检查OnItemClick方法中的第一个位置,并将模型中的其他位置设置为false。然后再次点击,同样的方式为你想要的其他条件应用逻辑

     listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                         if(position==0){
                            lst.get(0).isEnabled =true;
                            for(int i=1;i<lst.size();i++){
                            lst.get(position).isEnabled =false;
                            }  
                          }else{
                             lst.get(0).isEnabled =false;
                          }
                         }
                        if (!lst.get(position).isEnabled) {
                               return;
                        }else{
                            //your code
                        }
//In end set all Enabled
                     for(int i=0;i<lst.size();i++){
                            lst.get(position).isEnabled =true;
                            }  

我希望这有点清楚

答案 2 :(得分:0)

一个简单的解决方案是分别创建第一个元素,(使用单独的行)。列表视图中的其余元素。通过这种方式,可以非常轻松地启用/禁用单击项目。