在listview中设置recyclerview水平滚动

时间:2016-11-21 04:45:35

标签: android listview android-recyclerview horizontal-scrolling

我在listview中实现了水平回收视图,但是当iam滚动lisview和recyler ciew我的应用程序崩溃时出现错误

throwable java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 34(offset:34).state:48
E/AndroidRuntime: FATAL EXCEPTION: main
              Process: technologies.codegreen.com.drzonesample, PID: 4712
              java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 34(offset:34).state:48
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5014)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4970)
                  at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
                  at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
                  at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
                  at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1193)
                  at android.support.v7.widget.LinearLayoutManager.scrollHorizontallyBy(LinearLayoutManager.java:1031)
                  at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:4409)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                  at android.view.Choreographer.doFrame(Choreographer.java:603)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我是新手,请帮助解决这个问题

这是我的listview适配器

public class Doctors_Adapters extends BaseAdapter {

private Context mContext;
private ArrayList<HashMap<String,Object>> mArrayUri;
Doctors_Available_Time_Adapter adapter;
ArrayList<TimeClass> mModel=new ArrayList<>();
public static int postionofidandimage;


public Doctors_Adapters(Context c, ArrayList<HashMap<String, Object>> mArrayUri) {
    mContext = c;
    this.mArrayUri = mArrayUri;


    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mContext.getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions)
            .build();
    ImageLoader.getInstance().init(config);

}


@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mArrayUri.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return mArrayUri.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}


@SuppressLint("ViewHolder")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    TextView name;
    TextView specialists;
    TextView address;
    TextView Appointment_Request;
    RatingBar rate;
    ArrayList<HashMap<String,Object>> map;

    final String names,specialistses,addresses;
    final String  doc_id,image;
    String ratebar;
    float ratings;



    View rootView;
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    rootView = inflater.inflate(R.layout.doctors_list_custom_view, null);

    HashMap<String, Object> resultp = new HashMap<String,Object>();
    resultp = mArrayUri.get(position);

    imageView = (ImageView)rootView.findViewById(R.id.doc_image);
    name= (TextView) rootView.findViewById(R.id.doc_name);
    specialists= (TextView) rootView.findViewById(R.id.doc_speciality);
    address= (TextView) rootView.findViewById(R.id.hosp_address);
    rate= (RatingBar) rootView.findViewById(R.id.rating);
    Appointment_Request= (TextView) rootView.findViewById(R.id.appoint_request);

    doc_id= (String) resultp.get("DoctorUserID");
    names= (String) resultp.get("Name");
    addresses= (String) resultp.get("HospitalNames");
    specialistses= (String) resultp.get("Speciality");
    image=(String) resultp.get("DoctorPhoto");
    ratebar= (String) resultp.get("OverallRate");
    ratings= Float.parseFloat(ratebar);




    List<HashMap> city = (List<HashMap>) resultp.get("Avilability");
    if(city.size()!=0 ) {
        mModel.clear();
        if (city.get(0).get("BookingTypeID").equals("2")) {

            Appointment_Request.setVisibility(View.VISIBLE);
            for (int i = 0; i < city.size(); i++) {
                HashMap list = city.get(i);
                String data = (String) list.get("StartTime");
                String data2 = (String) list.get("Available");
                String data3 = (String) list.get("WeekDate");
                String data4= (String) list.get("BookingTypeID");

                GlobalArraylist.Bookingtype2.add(new TimeClass(data, data2, doc_id, data3,data4));

            }


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

                    Intent appoit_Request=new Intent(mContext, Appointment_Request.class);
                    appoit_Request.putExtra("activity","doc_from_list");
                    appoit_Request.putExtra("DATE", Global.SearchBooking);
                    appoit_Request.putExtra("DOCTOR_ID",doc_id);
                    appoit_Request.putExtra("DOCTOR_NAME",names);
                    appoit_Request.putExtra("DOCTOR_IMAGE",image);
                    appoit_Request.putExtra("DOCTOR_SPECIALITY",specialistses);

                    mContext.startActivity(appoit_Request);

                }
            });


        }
        else{

            for (int i = 0; i < city.size(); i++) {
                HashMap list = city.get(i);
                String data = (String) list.get("StartTime");
                String data2 = (String) list.get("Available");
                String data3 = (String) list.get("WeekDate");
                String data4= (String) list.get("BookingTypeID");

                mModel.add(new TimeClass(data, data2, doc_id, data3,data4));
            }


            RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
            recyclerView.setVisibility(View.VISIBLE);
            adapter = new Doctors_Available_Time_Adapter(mContext, mModel);
            recyclerView.setAdapter(adapter);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));

            adapter.notifyDataSetChanged();

        }

    }

    name.setText(names);
    address.setText(addresses);
    specialists.setText(specialistses);
    rate.setRating(ratings);

    ImageLoader.getInstance().displayImage(image, imageView);



    if (position == 0) {
       rootView.setBackgroundColor(Color.parseColor("#FFF7F7F7"));
    }
    else if (position % 2 == 1) {
       rootView.setBackgroundColor(Color.parseColor("#FFFDFDFD"));
    }
    else if (position % 2 == 0) {
        rootView.setBackgroundColor(Color.parseColor("#FFF7F7F7"));
    }



    return rootView;
}

}

这是我的Recyclerview适配器

  public class Doctors_Available_Time_Adapter extends       RecyclerView.Adapter<Doctors_Available_Time_Adapter.ViewHolder>
   {
private Context mContext;
ArrayList<TimeClass> mModel;
String doc_id;
String doc_time;


public Doctors_Available_Time_Adapter(Context mContexts, ArrayList<TimeClass> model) {
    mContext=mContexts;
    mModel = model;
}

@Override
public Doctors_Available_Time_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.availability_time_layout, parent, false);


    return new ViewHolder(view);
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener  {

    TextView timer;

    public ViewHolder(View view) {
        super(view);
        timer= (TextView) view.findViewById(R.id.timer);

    }

    @Override
    public void onClick(View v) {

        int positions  =   getAdapterPosition();

        switch (v.getId()){
            case R.id.timer:

                String data= String.valueOf(positions);
                Intent booking=new Intent(mContext, Booking_First_Page.class);
                booking.putExtra("time",data);
                booking.putExtra("doc_id",doc_id);
                booking.putExtra("activity","doc_from_list");
                mContext.startActivity(booking);

                break;
        }

    }




}

@Override
public void onBindViewHolder(Doctors_Available_Time_Adapter.ViewHolder holder,int position) {

    String status=mModel.get(position).time_status;
    doc_id=mModel.get(position).doc_id;

    if(status.equals("Yes")){
        holder.timer.setBackgroundResource(R.drawable.time_of_text_bg3);
        holder.timer.setTextColor(Color.parseColor("#000000"));
        holder.timer.setClickable(true);
        holder.timer.setText(mModel.get(position).doc_time);

    }
    else if(status.equals("No")){

        holder.timer.setBackgroundResource(R.drawable.time_of_text_bg2);
        holder.timer.setTextColor(Color.parseColor("#bdbdbd"));
        holder.timer.setClickable(false);
        holder.timer.setText(mModel.get(position).doc_time);

    }


}


@Override
public int getItemCount() {
    return mModel.size();
}

}

0 个答案:

没有答案