如何根据android中的内容设置Listview项目不可点击

时间:2018-03-23 11:56:28

标签: android listview

您好我正在开发一个Android应用程序,其中我使用ListView与自定义适配器,我想在ListView基于他们的内容设置可点击和不可点击的项目。我搜索相同,但无处不在答案是在下面使用方法

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

但它不是我想要实现的。所以有人知道如何实现这个目标吗?

这是我的自定义适配器代码

public class History_List_Adapter extends BaseAdapter {

private Activity activity;
private LayoutInflater inflater;
private List<User_History_List> historyList;
TextView time,cost,ride_time,bike_number,source_location,destination_location;
User_History_List userHistoryList;
ImageView live_status;

public Bicycle_History_List_Adapter(Activity activity, List<User_History_List> lists) {
    this.activity = activity;
    this.historyList = lists;
}

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

@Override
public Object getItem(int location) {
    return historyList.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(inflater == null){
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    if(convertView == null){
        convertView = inflater.inflate(R.layout.history_list,null);
    }
    time = convertView.findViewById(R.id.tv_history_time);
    bike_number = convertView.findViewById(R.id.tv_history_bike_number);
    cost = convertView.findViewById(R.id.tv_history_rental);
    ride_time = convertView.findViewById(R.id.tv_history_ride_time);
    source_location = convertView.findViewById(R.id.tv_history_source_address);
    destination_location = convertView.findViewById(R.id.tv_history_destination_address);
    live_status = convertView.findViewById(R.id.ride_status);

    Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(), "font/Catamaran_Regular.ttf");
    time.setTypeface(tf);
    bike_number.setTypeface(tf);
    cost.setTypeface(tf);
    ride_time.setTypeface(tf);
    source_location.setTypeface(tf);
    destination_location.setTypeface(tf);

    userHistoryList = historyList.get(position);

    time.setText(getDate_Time(userHistoryList.getPickup_time()));
    bike_number.setText(userHistoryList.getBike_number());
    source_location.setText(userHistoryList.getSourse_address());
    destination_location.setText(userHistoryList.getDestination_address());
    if(userHistoryList.getIs_live().equals("RIDING")){
        cost.setText("Live");
        ride_time.setText(getride_time(userHistoryList.getPickup_time()));
        Animation animation = new AlphaAnimation(1, 0);
        animation.setDuration(1000);
        animation.setInterpolator(new LinearInterpolator());
        animation.setRepeatCount(Animation.INFINITE);
        animation.setRepeatMode(Animation.REVERSE);
        live_status.startAnimation(animation);
        convertView.setEnabled(false);
    }else if(userHistoryList.getIs_live().equals("OUTSTANDING")){
        cost.setText("Live");
        ride_time.setText(getride_time(userHistoryList.getPickup_time()));
        live_status.setImageResource(R.drawable.ic_oustanding_24px);
        Animation animation = new AlphaAnimation(1, 0);
        animation.setDuration(1000);
        animation.setInterpolator(new LinearInterpolator());
        animation.setRepeatCount(Animation.INFINITE);
        animation.setRepeatMode(Animation.REVERSE);
        live_status.startAnimation(animation);
        convertView.setEnabled(false);
    }else {
        cost.setText("₹"+userHistoryList.getCost()+"/-");
        ride_time.setText(gettime(userHistoryList.getRide_time()));
        live_status.setImageResource(R.drawable.ic_not_live_status_24px);
        convertView.setEnabled(true);
    }


    return convertView ;
}

public String getDate_Time(Long milisecond){
    Date current_date = new Date(milisecond);

    SimpleDateFormat dateformat = new SimpleDateFormat("E,dd MMM yy h:mm a");

    return dateformat.format(current_date);
}

public String getride_time(Long milisecond){
    Date booking_date = new Date(milisecond);
    Date current_date = Calendar.getInstance().getTime();

    return gettime(TimeUnit.MILLISECONDS.toMinutes(current_date.getTime() - booking_date.getTime()));
}


public String gettime(Long min){
    String time;
    int hr = (int) (min/60);
    int minutes = (int)(min%60);
    if(hr >0) {
        time = hr + "hr " + minutes + "min";

    }else {
        time = minutes + "min";
    }
    return time ;
}
}

1 个答案:

答案 0 :(得分:1)

您可以使用以下方法根据您的情况使视图无法点击

yourView.setEnabled(假);

活动

lv.setOnItemClickListener(new OnItemClickListener()
{
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
    { 
      if(!userHistoryList.get(position).getIs_live().equals("RIDING")&&!userHistoryList.get(position).getIs_live().equals("OUTSTANDING")){

    //do something for a valid click
}

});