2015-02-06 70 views
-1

在下面的代码中,我已经在水平List视图中实现了calander。 现在我想滚动到特定的日期,如果我通过 22日期,所以它应该自动滚动到第21项。我有 尝试horizo​​ntalListView.scrollTO(int x),但它不工作。请 帮我解决,如果您需要关于下面的代码的更多解释,请让我知道 。我已经上传全班这个URL如何滚动到水平列表视图中的特定项目?

http://tinyurl.com/o6q2uty

+0

请,如果您打算向我们展示了一些代码,尝试包括它的问题,或至少使用一个要点。 – Logain 2015-02-06 07:38:55

回答

0
public class MyAppointment extends BaseActivity implements OnClickListener, 
    DataListener { 
private LinearLayout myAppointment; 
public LinearLayout llCalTop; 
private ListView appointmentsExpandableList; 
private ArrayList<String> alist; 
private ArrayList<AppointmentDO> childs1, childs2, childs3; 
private AppointmentListAdapter adapter; 
private HashMap<String, ArrayList<AppointmentDO>> map; 
private Vector<RequestDO> vecRequestDOs; 
Boolean flag = true; 
private View monthview; 
private MyAppointment context; 
private TextView tvSelMonthYear; 
public View selView; 
private View oldView; 
private LinearLayout llcalBody; 
static Calendar calendar; 
private Calendar CalNext; 
private Calendar CalPrev; 
private int month, year; 
TextView tvPrevMonthClick, tvNextMonthClick; 
private CustomCalendar customCalender; 
static String calSelectedDate; 
private ImageView ivCalRightArrow, ivCalLeftArrow; 
public static int width; 
// ----------------------------------Slider calendar 
// variables------------------------------------// 
private HorizontialListView lvItems; 
private CalendarAdapter calendarAdapter; 
private TextView tvMonth; 
private ImageView ivleft, ivright; 
private Calendar cal; 
private CommonBL commonBL; 
private RequestDO requestDO; 
private String appointmentdate = "", appointmenttime = ""; 

// -------- Selected date intializing with -1 because 0 is the minimum value 
// in calander.-----// 
int selectedDay = -1, selectedMonth = -1, selectedYear = -1; 

// -------------------------------------------------*/*---------------------------------------// 
@SuppressLint("NewApi") 
@Override 
public void initialize() { 
    myAppointment = (LinearLayout) inflater.inflate(
      R.layout.my_appointments_layout, null); 
    llCalTop = (LinearLayout) myAppointment.findViewById(R.id.llCalTop); 
    llBody.addView(myAppointment, new LayoutParams(
      LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
    appointmentsExpandableList = (ListView) findViewById(R.id.appointmentsExpandableList); 
    ivHeaderRight = (ImageView) findViewById(R.id.ivHeaderRight); 

    // initLists(); 

    vecRequestDOs = new MyAppointmentsDA() 
      .getAllInitiativesById("31-01-2015"); 
    adapter = new AppointmentListAdapter(MyAppointment.this, vecRequestDOs); 
    header.setText("My Appointment"); 
    appointmentsExpandableList.setAdapter(adapter); 
    ivHeaderRight.setVisibility(View.VISIBLE); 
    ivHeaderRight.setImageResource(R.drawable.cal); 

    ivHeaderRight.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      if (flag) { 
       ivHeaderRight.setImageResource(R.drawable.list); 
       flag = false; 
       oldView = findViewById(R.id.llCalTop); 
       // 

       show(); 

      } else { 
       ivHeaderRight.setImageResource(R.drawable.cal); 
       flag = true; 
       oldView = (LinearLayout) inflater.inflate(
         R.layout.my_appointments_layout, null); 
       llCalTop.setLayoutParams(new LinearLayout.LayoutParams(
         LayoutParams.MATCH_PARENT, 
         LayoutParams.MATCH_PARENT)); 
       llCalTop.removeAllViews(); 
       selectedDay = CustomCalendar.todayDate; 
       selectedMonth = customCalender.month; 
       selectedYear = customCalender.year; 

       llCalTop.addView(oldView, new LayoutParams(
         LayoutParams.MATCH_PARENT, 
         LayoutParams.MATCH_PARENT)); 
       sliderCalendar(); 

      } 

     } 
    }); 

    sliderCalendar(); 

} 

@SuppressLint("NewApi") 
public void show() { 
    monthview = inflater.inflate(R.layout.monthview_cal, null); 
    monthview.setLayoutParams(new LinearLayout.LayoutParams(
      LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
    llCalTop.removeAllViews(); 
    llCalTop.addView(monthview, new LayoutParams(LayoutParams.MATCH_PARENT, 
      LayoutParams.MATCH_PARENT)); 
    context = MyAppointment.this; 
    Display display = getWindowManager().getDefaultDisplay(); 
    Point size = new Point(); 
    display.getSize(size); 
    int width = size.x; 
    CustomCalendar.CALENDER_CELL_WIDTH = width/7; 
    tvSelMonthYear = (TextView) findViewById(R.id.tvSelMonthYear); 
    llcalBody = (LinearLayout) findViewById(R.id.llcalBody); 
    tvPrevMonthClick = (TextView) findViewById(R.id.prevMonth); 
    tvNextMonthClick = (TextView) findViewById(R.id.nextMonth); 
    ivCalLeftArrow = (ImageView) findViewById(R.id.ivCalLeftArrow); 
    ivCalRightArrow = (ImageView) findViewById(R.id.ivCalRightArrow); 
    tvSelMonthYear.setTextSize(16); 
    calendar = Calendar.getInstance(); 

    if (selectedDay != -1 && selectedMonth != -1 && selectedYear != -1) { 
     calendar.set(selectedYear, selectedMonth, selectedDay); 
    } 

    month = calendar.get(Calendar.MONTH); 
    year = calendar.get(Calendar.YEAR); 

    CalPrev = Calendar.getInstance(); 
    CalPrev.add(Calendar.MONTH, -1); 

    CalNext = Calendar.getInstance(); 
    CalNext.add(Calendar.MONTH, 1); 

    tvSelMonthYear.setText(CalendarUtility.getMonth(month) + " " 
      + calendar.get(Calendar.YEAR)); 
    showCalender(month, year); 

    ivCalLeftArrow.setOnClickListener(this); 
    ivCalRightArrow.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    if (v.getId() == R.id.ivCalLeftArrow) { 
     calendar.add(Calendar.MONTH, -1); 
     tvSelMonthYear.setText(CalendarUtility.getMonth(calendar 
       .get(Calendar.MONTH)) + " " + calendar.get(Calendar.YEAR)); 
     setMonthText(); 
     showCalender(calendar.get(Calendar.MONTH), 
       calendar.get(Calendar.YEAR)); 

    } else if (v.getId() == R.id.ivCalRightArrow) { 

     calendar.add(Calendar.MONTH, +1); 
     tvSelMonthYear.setText(CalendarUtility.getMonth(calendar 
       .get(Calendar.MONTH)) + " " + calendar.get(Calendar.YEAR)); 
     showCalender(calendar.get(Calendar.MONTH), 
       calendar.get(Calendar.YEAR)); 
     setMonthText(); 
    } 
} 

public void showCalender(int month, int year) { 
    llcalBody.removeAllViews(); 
    customCalender = new CustomCalendar(context, month, year); 
    llcalBody.addView(customCalender.makeCalendar(), 
      LayoutParams.MATCH_PARENT); 
} 

public void setMonthText() { 
    int tempCurrentMonth = calendar.get(Calendar.MONTH); 
    if (tempCurrentMonth > 0 && tempCurrentMonth < 11) { 
     tvPrevMonthClick.setText(CalendarUtility 
       .getMonth(tempCurrentMonth - 1)); 
     tvNextMonthClick.setText(CalendarUtility 
       .getMonth(tempCurrentMonth + 1)); 
    } else if (tempCurrentMonth == 0) { 
     tvPrevMonthClick.setText(CalendarUtility.getMonth(11)); 
     tvNextMonthClick.setText(CalendarUtility.getMonth(1)); 
    } else if (tempCurrentMonth == 11) { 
     tvPrevMonthClick.setText(CalendarUtility.getMonth(10)); 
     tvNextMonthClick.setText(CalendarUtility.getMonth(0)); 
    } 
} 

private void initLists() { 
    // alist = new ArrayList<String>(); 
    // childs1 = new ArrayList<AppointmentDO>(); 
    // childs2 = new ArrayList<AppointmentDO>(); 
    // childs3 = new ArrayList<AppointmentDO>(); 
    // alist.add("WED, JAN 28"); 
    // // alist.add("THU, JAN 29"); 
    // // alist.add("FRI, JAN 30"); 
    // childs1.add(new AppointmentDO("R.MOHAN REDDY", "address", "09:00 AM", 
    // "40 minutes")); 
    // // childs1.add(new AppointmentDO("KIRAN KATTA", "address", 
    // "10:00 AM", 
    // // "40 minutes")); 
    // // childs1.add(new AppointmentDO("R.MOHAN REDDY", "address", 
    // "11:00 AM", 
    // // "40 minutes")); 
    // childs2.add(new AppointmentDO("KIRAN KATTA", "address", "12:00 PM", 
    // "40 minutes")); 
    // childs2.add(new AppointmentDO("R.MOHAN REDDY", "address", "10:30 AM", 
    // "40 minutes")); 
    // childs3.add(new AppointmentDO("KIRAN KATTA", "address", "10:45 AM", 
    // "40 minutes")); 
    // map = new HashMap<String, ArrayList<AppointmentDO>>(); 
    // map.put(alist.get(0), childs1); 
    // map.put(alist.get(1), childs2); 
    // map.put(alist.get(2), childs3); 

} 

protected void sliderCalendar() { 
    // ---------------------------------Slider Calendar Implementation 
    // -------------------------------------------// 

    llCalTop = (LinearLayout) myAppointment.findViewById(R.id.llCalTop); 

    lvItems = (HorizontialListView) myAppointment 
      .findViewById(R.id.lvItems); 
    tvMonth = (TextView) myAppointment.findViewById(R.id.tvMonth); 
    ivleft = (ImageView) myAppointment.findViewById(R.id.ivleft); 
    ivright = (ImageView) myAppointment.findViewById(R.id.ivright); 

    commonBL = new CommonBL(MyAppointment.this, MyAppointment.this); 

    cal = Calendar.getInstance(); 

    if (selectedDay != -1 && selectedMonth != -1 && selectedYear != -1) { 
     cal.set(selectedYear, selectedMonth, selectedDay); 
    } 

    if (lvItems.getAdapter() == null) { 
     calendarAdapter = new CalendarAdapter(MyAppointment.this, cal); 
     calendarAdapter.setSelectedPosition(selectedDay - 1); 

     lvItems.setAdapter(calendarAdapter); 
     tvMonth.setText(CalendarUtils.getMonthFromNumber(cal 
       .get(Calendar.MONTH) + 1)); 
     // /// -----------------Trying to scroll at selected date ---------------------// 
     // ---------------///// 
     lvItems.scrollTo((selectedDay - 1) * (60) 
       + (lvItems.getChildCount() - (selectedDay - 1))); 

     } else { 
      calendarAdapter.notifyDataSetChanged(); 
     } 

    // --------------------**--------------------------------------// 

    lvItems.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View view, 
       int position, long arg3) { 
      appointmentdate = ""; 
      calendarAdapter.setSelectedPosition(position); 
      // ----------------------**----------------// 
      selectedDay = position; 
      selectedMonth = cal.get(Calendar.MONTH); 
      selectedYear = cal.get(Calendar.YEAR); 
      Toast.makeText(getApplicationContext(), 
        selectedDay + "/" + selectedMonth + "/" + selectedYear, 
        Toast.LENGTH_SHORT).show(); 
      LogUtils.errorLog(
        "Clicked Date", 
        (position + 1) 
          + "" 
          + CalendarUtils.getMonthFromNumber(cal 
            .get(Calendar.MONTH) + 1) + "," 
          + cal.get(Calendar.YEAR)); 

      if (cal.get(Calendar.MONTH) + 1 < 10) 
       appointmentdate = (position + 1) + "-0" 
         + (cal.get(Calendar.MONTH) + 1) + "-" 
         + cal.get(Calendar.YEAR); 
      else 
       appointmentdate = (position + 1) + "-" 
         + (cal.get(Calendar.MONTH) + 1) + "-" 
         + cal.get(Calendar.YEAR); 
     } 
    }); 

    ivleft.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      cal.add(Calendar.MONTH, -1); 
      tvMonth.setText(CalendarUtils.getMonthFromNumber(cal 
        .get(Calendar.MONTH) + 1) 
        + " " 
        + cal.get(Calendar.YEAR)); 
      calendarAdapter.refresh(cal); 
      calendarAdapter.setSelectedPosition(0); 

     } 
    }); 

    ivright.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      cal.add(Calendar.MONTH, 1); 
      tvMonth.setText(CalendarUtils.getMonthFromNumber(cal 
        .get(Calendar.MONTH) + 1) 
        + " " 
        + cal.get(Calendar.YEAR)); 
      calendarAdapter.refresh(cal); 
      calendarAdapter.setSelectedPosition(0); 
     } 
    }); 

    // --------------------------------------/**/--------------------------------------------------// 
} 

private class CalendarAdapter extends BaseAdapter { 

    private String week[] = { "Su", "M", "T", "W", "Th", "F", "S" }; 
    private Context context; 
    private int seletedPosition; 
    private Calendar cal; 

    public CalendarAdapter(Context context, Calendar cal) { 
     this.cal = cal; 
     this.context = context; 
    } 

    @Override 
    public int getCount() { 
     return cal.getActualMaximum(Calendar.DAY_OF_MONTH); 
    } 

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

    public void setSelectedPosition(int seletedPosition) { 
     this.seletedPosition = seletedPosition; 
     notifyDataSetChanged(); 
    } 

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     ViewHolder viewHolder = new ViewHolder(); 
     if (convertView == null) { 
      convertView = LayoutInflater.from(context).inflate(
        R.layout.calendar_cell, null); 
      viewHolder.tvweek = (TextView) convertView 
        .findViewById(R.id.tvweek); 
      viewHolder.tvday = (TextView) convertView 
        .findViewById(R.id.tvday); 
      convertView.setTag(viewHolder); 
     } else 
      viewHolder = (ViewHolder) convertView.getTag(); 

     viewHolder.tvday.setText((position + 1) + ""); 

     cal.set(Calendar.DAY_OF_MONTH, position + 1); 

     viewHolder.tvweek 
       .setText(week[cal.get(Calendar.DAY_OF_WEEK) - 1 % 7]); 

     if (seletedPosition == position) { 
      viewHolder.tvday.setBackgroundResource(R.drawable.date_hover); 
      viewHolder.tvday.setTextColor(getResources().getColor(
        R.color.white_color)); 
     } else { 
      viewHolder.tvday.setBackgroundResource(0); 
      viewHolder.tvday.setTextColor(getResources().getColor(
        R.color.date_color)); 
     } 

     return convertView; 
    } 

    public void refresh(Calendar cal) { 
     this.cal = cal; 
     notifyDataSetChanged(); 
    } 

} 

private static class ViewHolder { 
    public TextView tvweek, tvday; 
} 

@Override 
public void dataRetreived(Response data) { 
    if (data.data != null) { 
     if (data.method == ServiceMethods.WS_INSERT_REQUEST) { 
      requestDO = (RequestDO) data.data; 

      if (requestDO != null) { 
       LogUtils.errorLog("Data Came", requestDO.Reason); 
      } 
     } 
    } 
} 

}

0

尝试listView.setSelection(index);