如何禁用ListView中的先前项目

时间:2017-09-26 12:27:50

标签: android listview custom-adapter

我正在尝试开发一个活动,其中存在listView制作的自定义CustomAdapter

该列表包含TextViewEditTextEditTextclicked,它会自动提取系统时间。

我想要的是是在填写特定EditText时,我希望序列中的所有上一个(上面)列表项都被禁用。

到目前为止,我已尝试使用isEnabled()areAllItemsEnabled()函数使用position返回相应的布尔值,但是不起作用。

请帮助我实现上述目标。 enter image description here

感谢。

这是我的CustomAdapter类

public class SelectStnListByRoute extends BaseAdapter implements View.OnClickListener {
    Context context;
    ArrayList<StnNames> stnList;
    LayoutInflater layoutInflater = null;
    ViewHolder viewHolder;
    private int mLastClicked;

    public SelectStnListByRoute(Context context, ArrayList<StnNames> stnList) {
        super();
        this.context = context;
        this.stnList = stnList;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return stnList.size();
    }

    @Override
    public Object getItem(int position) {
        return stnList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return stnList.indexOf(getItem(position));
    }

    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        if(position==position){
            return false;
        }
        return false;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        int type = getItemViewType(position);
        StnNames stnDetails = stnList.get(position);
        viewHolder = new ViewHolder();
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.footplate_custome_layout, null);
            viewHolder.txtStnNAme = (TextView) convertView.findViewById(R.id.txtStnCode);
            viewHolder.txtStnArr = (TextView) convertView.findViewById(R.id.txtArrivalTime);
            viewHolder.txtStnDep = (TextView) convertView.findViewById(R.id.txtDepTime);
            convertView.setTag(viewHolder);
            viewHolder.txtStnArr.setTag(stnDetails);
            viewHolder.txtStnDep.setTag(stnDetails);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            viewHolder.txtStnArr.setTag(stnDetails);
            viewHolder.txtStnDep.setTag(stnDetails);
        }
        viewHolder.txtStnNAme.setText(stnDetails.getStnCode());

          viewHolder.txtStnArr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.e("Position: " , String.valueOf(position)); //Here I am getting the position of the row item clicked, where should I put the Onclick false for disabling all of the above fields using the position?
        }
    });
        viewHolder.txtStnDep.setOnClickListener(this);
        viewHolder.txtStnArr = (TextView) convertView.findViewById(R.id.txtArrivalTime);
        if (stnDetails.getArrivalTime() != null) {
            viewHolder.txtStnArr.setText(stnDetails.getArrivalTime());
        } else {
            viewHolder.txtStnArr.setText("");
        }


        if (stnDetails.getDeptTime() != null) {
            viewHolder.txtStnDep.setText(stnDetails.getDeptTime());
        } else {
            viewHolder.txtStnDep.setText("");
        } 
        return convertView;
    }

    class ViewHolder {
        TextView txtStnNAme, txtStnArr, txtStnDep;
        int ref;
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();

        switch (id) {
            case txtArrivalTime:
                TextView textViewArrVal = (TextView) view.findViewById(R.id.txtArrivalTime);
                textViewArrVal.setClickable(false);
                StnNames listItemsArrr = (StnNames) textViewArrVal.getTag();
                if (listItemsArrr.getArrivalTime() != getCurrentTime()) {
                    listItemsArrr.setArrivalTime(getCurrentTime());
                    if (listItemsArrr.getArrivalTime() != null) {
                        int position = textViewArrVal.getSelectionStart();
                        textViewArrVal.setText(listItemsArrr.getArrivalTime());

                    } else {
                        textViewArrVal.setText("");
                    }
                }
                break;
            case txtDepTime:
                TextView textViewDepVal = (TextView) view.findViewById(R.id.txtDepTime);
                StnNames listItemsDepp = (StnNames) textViewDepVal.getTag();
                if (listItemsDepp.getDeptTime() != getCurrentTime()) {
                    listItemsDepp.setDeptTime(getCurrentTime());
                    if (listItemsDepp.getDeptTime() != null) {
                        textViewDepVal.setText(listItemsDepp.getDeptTime());
                    } else {
                        textViewDepVal.setText("");
                    }
                }
                break;
            default:
                break;
        }

    }
    public String getCurrentTime(){

        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat mdformat = new SimpleDateFormat("HH:mm:ss");
        String arrDate = mdformat.format(calendar.getTime());
        return arrDate;
    }

}

2 个答案:

答案 0 :(得分:1)

您可以按照以下方式执行此操作 - :

您需要存储点击按钮的位置。所以在你的类中初始化一个变量

int mButtonSelected = -1;

编辑1。

然后更改您的isEnabled方法

 @Override
public boolean isEnabled(int position) {
    if(position<mButtonSelected){
        return false;
    }
    return true;
}

如果单击任何其他按钮,它将起作用。但你必须在onClick

中这样做
mButtonSelected = position;
notifyDataSetChanged();

让我工作与否

修改

请参阅以下代码中的更改 - :

public class SelectStnListByRoute extends BaseAdapter {
Context context;
ArrayList<StnNames> stnList;
LayoutInflater layoutInflater = null;
ViewHolder viewHolder;
private int mLastClicked;
private SQLiteDB sqLiteDB;
int mArrivalSelected = -1;
int mDepartSelected = -1;

public SelectStnListByRoute(Context context, ArrayList<StnNames> stnList) {
    super();
    this.context = context;
    this.stnList = stnList;
    layoutInflater = LayoutInflater.from(context);
    sqLiteDB = new SQLiteDB(context);
}

@Override
public int getCount() {
    return stnList.size();
}

@Override
public Object getItem(int position) {
    return stnList.get(position);
}

@Override
public long getItemId(int position) {
    return stnList.indexOf(getItem(position));
}

public int getViewTypeCount() {
    return 1;
}

@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    if (position <= mArrivalSelected) {
        return false;
    }
    return true;
}

public boolean isEnabledd(int position) {
    if (position <= mDepartSelected) {
        return false;
    }
    return true;
}

@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
    int type = getItemViewType(position);
    StnNames stnDetails = stnList.get(position);
    viewHolder = new ViewHolder();
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.footplate_custome_layout, null);
        viewHolder.txtStnNAme = (TextView) convertView.findViewById(R.id.txtStnCode);
        viewHolder.txtStnArr = (TextView) convertView.findViewById(R.id.txtArrivalTime);
        viewHolder.txtStnDep = (TextView) convertView.findViewById(R.id.txtDepTime);
        convertView.setTag(viewHolder);
        viewHolder.txtStnArr.setTag(stnDetails);
        viewHolder.txtStnDep.setTag(stnDetails);


    } else {
        viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.txtStnArr.setTag(stnDetails);
        viewHolder.txtStnDep.setTag(stnDetails);

    }


    viewHolder.txtStnNAme.setText(stnDetails.getStnCode());
    if (!isEnabled(position)) {
        if (position <= mArrivalSelected) {
            viewHolder.txtStnArr.setBackgroundColor(Color.parseColor("#ffa500"));
            viewHolder.txtStnArr.setEnabled(false);
            if (position < mArrivalSelected) {
                viewHolder.txtStnDep.setEnabled(false);
                viewHolder.txtStnDep.setBackgroundColor(Color.parseColor("#ffa500"));
            }
        }
    } else {
        viewHolder.txtStnArr.setEnabled(true);
        viewHolder.txtStnDep.setEnabled(true);
        viewHolder.txtStnArr.setBackgroundColor(Color.parseColor("#b4b4b4"));
        viewHolder.txtStnDep.setBackgroundColor(Color.parseColor("#b4b4b4"));
    }
    viewHolder.txtStnArr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.e("Position: ", String.valueOf(position));
            mArrivalSelected = position;
            arrivalClick(view);
            notifyDataSetChanged();
        }
    });
    if (!isEnabledd(position)) {
        if (position <= mDepartSelected) {
            viewHolder.txtStnArr.setBackgroundColor(Color.parseColor("#ffa500"));
            viewHolder.txtStnDep.setBackgroundColor(Color.parseColor("#ffa500"));
            viewHolder.txtStnArr.setEnabled(false);
            viewHolder.txtStnDep.setEnabled(false);
        } else {
            viewHolder.txtStnArr.setEnabled(true);
            viewHolder.txtStnDep.setEnabled(true);
            viewHolder.txtStnArr.setBackgroundColor(Color.parseColor("#b4b4b4"));
            viewHolder.txtStnDep.setBackgroundColor(Color.parseColor("#b4b4b4"));
        }
    }
    viewHolder.txtStnDep.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.e("Position: ", String.valueOf(position));
            mDepartSelected = position;
            departureClick(view);
            notifyDataSetChanged();
        }
    });
    viewHolder.txtStnArr = (TextView) convertView.findViewById(R.id.txtArrivalTime);
    if (stnDetails.getArrivalTime() != null) {
        viewHolder.txtStnArr.setText(stnDetails.getArrivalTime());
    } else {
        viewHolder.txtStnArr.setText("");
    }


    if (stnDetails.getDeptTime() != null) {
        viewHolder.txtStnDep.setText(stnDetails.getDeptTime());
    } else {
        viewHolder.txtStnDep.setText("");
    }

    return convertView;
}

class ViewHolder {
    TextView txtStnNAme, txtStnArr, txtStnDep;
    StnNames pos;
    int ref;
}

public void arrivalClick(View view) {
    TextView textViewArrVal = (TextView) view.findViewById(R.id.txtArrivalTime);

    StnNames listItemsArrr = (StnNames) textViewArrVal.getTag();
    if (listItemsArrr.getArrivalTime() != getCurrentTime()) {
        listItemsArrr.setArrivalTime(getCurrentTime());
        int stnId = listItemsArrr.getStnId();
        String arrClick = "arrival";
        String upSideKm = listItemsArrr.getStnUpsideKm();
        String downsideKm = listItemsArrr.getStnDownSideKm();
        String arrTime = getCurrentTime();
           /*     sqLiteDB.open();
               *//* long abc = sqLiteDB.insertJourneySchedule(stnId,arrTime,"",userId,journeyId,latitute,longitute,journyDate,arrClick);*//*
               *//* long abcd = sqLiteDB.updateJourneySchedule(stnId,arrTime,"",userId,journeyId,latitute,longitute,journyDate,arrClick,downsideKm,upSideKm);
                Log.e("arrclick",String.valueOf(abcd));*//*
                sqLiteDB.close();*/
        if (listItemsArrr.getArrivalTime() != null) {
            int position = textViewArrVal.getSelectionStart();
            textViewArrVal.setText(listItemsArrr.getArrivalTime());

        } else {
            textViewArrVal.setText("");
        }
    }
}

public void departureClick(View view) {
    TextView textViewDepVal = (TextView) view.findViewById(R.id.txtDepTime);
    StnNames listItemsDepp = (StnNames) textViewDepVal.getTag();
    if (listItemsDepp.getDeptTime() != getCurrentTime()) {
        listItemsDepp.setDeptTime(getCurrentTime());
        String depTime = getCurrentTime();
        int stnId = listItemsDepp.getStnId();
        String depClick = "departure";
        String upSideKm = listItemsDepp.getStnUpsideKm();
        String downsideKm = listItemsDepp.getStnDownSideKm();
        sqLiteDB.open();
                /*long abc = sqLiteDB.insertJourneySchedule(stnId,"",depTime,userId,journeyId,latitute,longitute,journyDate,depClick);*/
                /*long abcd = sqLiteDB.updateJourneySchedule(stnId,"",depTime,userId,journeyId,latitute,longitute,journyDate,depClick,downsideKm,upSideKm);
                Log.e("depclick",String.valueOf(abcd));*/
        sqLiteDB.close();
        if (listItemsDepp.getDeptTime() != null) {
            textViewDepVal.setText(listItemsDepp.getDeptTime());
        } else {
            textViewDepVal.setText("");
        }
    }
}



public String getCurrentTime() {

    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat mdformat = new SimpleDateFormat("HH:mm:ss");
    String arrDate = mdformat.format(calendar.getTime());
    return arrDate;
}

}

答案 1 :(得分:0)

获取单击行的位置,然后设置onclick false以获取低于点击位置的位置

如下:

viewHolder.txtStnArr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.e("Position: " , String.valueOf(position)); 
            for (int i = 0; i < position; i++) {
                  viewHolder.txtStnArr.setEnable(false);
             }
             notifyDataSetChanged();
        }
    });