Android recyclerview内容更改为onscroll

时间:2015-08-10 05:40:57

标签: android scroll android-recyclerview android-cardview

我正在使用RecyclerView显示列表,RecyclerView中的每个项目都显示为CardView。当RecyclerView向下滚动时,CardView内的内容会发生变化。

向下滚动之前:

 enter image description here

向下滚动后:

enter image description here

正如您在上面看到的那样,两张图片“UNKNOWN”被更改为“null”。 RecyclerView中显示的内容是从SQLite中获取的。经过数小时和数小时的谷歌搜索后找不到任何解决方案。

适配器类:

 public class DraftListAdapter extends
RecyclerView.Adapter<DraftListAdapter.ViewHolder> {

    private static final String TAG = "DraftListAdapter";

    private ArrayList<EventDAO> al_item_list;

    private int rowLayout;

    private Activity obj_activity;

    private DisplayImageOptions obj_display_image_options;

    private static RecyclerViewClickListener itemListener;

    private SimpleDateFormat obj_date_formater_display = new SimpleDateFormat(
            "MMMM d, yyyy");
    private SimpleDateFormat obj_date_formater_sqlite = new SimpleDateFormat(
            "yyyy-MM-dd");

    public DraftListAdapter(ArrayList<EventDAO> al_item_list, int rowLayout,
            Activity obj_activity, DisplayImageOptions obj_display_image_options) {

        this.al_item_list = al_item_list;
        this.rowLayout = rowLayout;
        this.obj_activity = obj_activity;
        this.obj_display_image_options = obj_display_image_options;
    }

    public DraftListAdapter(ArrayList<EventDAO> al_item_list, int rowLayout,
            Activity obj_activity,
            DisplayImageOptions obj_display_image_options,
            RecyclerViewClickListener itemListener) {

        this.al_item_list = al_item_list;
        this.rowLayout = rowLayout;
        this.obj_activity = obj_activity;
        this.obj_display_image_options = obj_display_image_options;
        this.itemListener = itemListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout,
                viewGroup, false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {

        final EventDAO obj_current = al_item_list.get(position);

        viewHolder.button_edit.setText("EDIT");

        viewHolder.imageview_bookmark.setVisibility(View.INVISIBLE);
        viewHolder.imageview_call.setVisibility(View.INVISIBLE);

        //display event banner
        viewHolder.imageview_event_banner.setTag(position);
        viewHolder.textview_event_name.setTag(position);

        ImageLoader.getInstance().displayImage("file:///"+obj_current.eventBanner,
                viewHolder.imageview_event_banner, obj_display_image_options,null);

        viewHolder.imageview_event_banner.setAlpha(200);

        //display event location slug
        viewHolder.textview_event_name.setText(obj_current.eventName);

        if (obj_current.isOnline.equals("1")) {
            viewHolder.linearlayout_location.setVisibility(View.GONE);
            viewHolder.linearlayout_online.setVisibility(View.VISIBLE);
            viewHolder.textview_location_slug.setText("ONLINE EVENT");
        } else {

            viewHolder.linearlayout_location.setVisibility(View.VISIBLE);
            viewHolder.linearlayout_online.setVisibility(View.GONE);
            if(obj_current.objLocation != null 
                    && obj_current.objLocation.locationName != null
                    && !obj_current.objLocation.locationName.equalsIgnoreCase(""))
                viewHolder.textview_location_slug.setText(obj_current.objLocation.locationName);

        }

        //display date
        //display time
        viewHolder.textview_event_time.setText("Time: " + obj_current.startTime);
        viewHolder.button_edit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Fragment obj_fragment = CreateEventFragment.newInstance(obj_current);

                FragmentTransaction fragment_transaction_eventdetails = ((MainActivity) obj_activity)
                        .getSupportFragmentManager().beginTransaction();

                // Get the details of that view to the EventDetailsFragment..
                fragment_transaction_eventdetails.replace(R.id.container,
                        obj_fragment);


                fragment_transaction_eventdetails.addToBackStack(obj_fragment
                        .getClass().getName());

                fragment_transaction_eventdetails.commit();

            }
        });


        viewHolder.button_delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                new AlertDialog.Builder((MainActivity) obj_activity)
                .setTitle("Quit")
                .setMessage("Do you really want to delete this drafted event?")
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog,
                            int which) {
                        EventDAO obj_delete_event_dao = new EventDAO(obj_activity);
                        obj_delete_event_dao.deleteEvent(obj_current.localEventID);
                        obj_delete_event_dao.closeDbConnection();
                        al_item_list.remove(position);
                        notifyDataSetChanged();
                    }
                }).setNegativeButton("No", null).show();
            }
        });

        viewHolder.linearlayout_location
        .setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(obj_activity, "Map intent",
                        Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(Intent.ACTION_VIEW, Uri
                        .parse("geo:0,0?q=Kothrud"));

                obj_activity.startActivity(Intent.createChooser(intent,
                        "Select "));
            }
        });

    }

    @Override
    public int getItemCount() {
        return al_item_list == null ? 0 : al_item_list.size();
    }

    /**
     * class to hold event list item
     * 
     * 
     */
    public static class ViewHolder extends RecyclerView.ViewHolder implements
    OnClickListener {

        public ImageView imageview_event_banner;
        public ImageView imageview_bookmark;
        public ImageView imageview_call;

        public TextView textview_event_name;
        //      public TextView textview_event_type;
        public TextView textview_location_slug;
        public TextView textview_event_date;
        public TextView textview_event_time;
        public Button button_edit;
        public Button button_delete;
        public LinearLayout linearlayout_location;
        public LinearLayout linearlayout_online;

        public ViewHolder(View itemView) {
            super(itemView);

            linearlayout_location = (LinearLayout) itemView
                    .findViewById(R.id.linearlayout_location);

            linearlayout_online = (LinearLayout) itemView
                    .findViewById(R.id.linearlayout_online);

            imageview_event_banner = (ImageView) itemView
                    .findViewById(R.id.imageview_event_banner);

            imageview_bookmark = (ImageView) itemView
                    .findViewById(R.id.imageview_bookmark);

            imageview_call = (ImageView) itemView
                    .findViewById(R.id.imageview_call);

            textview_event_name = (TextView) itemView
                    .findViewById(R.id.textview_event_name);
            textview_event_name.setSelected(true);

            textview_location_slug = (TextView) itemView
                    .findViewById(R.id.textview_location_slug);

            textview_location_slug.setSelected(true);

            textview_event_date = (TextView) itemView
                    .findViewById(R.id.textview_event_date);

            textview_event_time = (TextView) itemView
                    .findViewById(R.id.textview_event_time);

            button_edit = (Button) itemView
                    .findViewById(R.id.button_edit);
            button_delete = (Button) itemView
                    .findViewById(R.id.button_delete);

            linearlayout_location.setOnClickListener(this);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {

            itemListener.recyclerViewListClicked(v, getPosition());

        }
    }

}

0 个答案:

没有答案