ListView中的OnClickListener与自定义适配器

时间:2014-09-16 19:20:18

标签: android listview

我有一个listview,其中包含一个自定义适配器,可以扩展布局。适配器膨胀的布局包含多个按钮。我的目标是为自定义适配器中的每个项目扩展此布局,并使与该布局对应的按钮仅响应它所在的布局(它所在的列表视图项目位置)。这是我的自定义适配器的代码:

    private class StableArrayAdapter extends ArrayAdapter<String> {

    public FragmentManager fragmentManager;

    ViewHolder viewHolder;
    private final Context context;
    private List<String> viewContent;
    //private List<String> posts;
    private List<String> posts;

    int index = 0;
    String title;
    TextView testText;

    public StableArrayAdapter(Context context, List<String> strings, List<String> content) {
        super(context, R.layout.post_layout, strings);
        this.context = context;
        this.posts = strings;
        this.viewContent = content;
    }

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

        viewHolder = new ViewHolder();

        positionHolder = position;
        Log.i("Position", "" + position);

        if(postLayout == null) {
            Log.i("CONVERTVIEW", "Convert View is null");
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            postLayout = inflater.inflate(R.layout.post_layout, parent, false);


            viewHolder.addToCalVF = (ViewFlipper)postLayout.findViewById(R.id.addToCalendarVF);
            viewHolder.unameTV = (TextView) postLayout.findViewById(R.id.postUnameTv);
            viewHolder.fillSpace = (TextView)postLayout.findViewById(R.id.posthelpSpace);
            viewHolder.image = (ImageView) postLayout.findViewById(R.id.postProfPic);
            viewHolder.eventTitle = (TextView) postLayout.findViewById(R.id.postTitleTV);
            viewHolder.eventImage = (ImageView) postLayout.findViewById(R.id.eventImage);
            viewHolder.likesTV = (TextView) postLayout.findViewById(R.id.likesTV);
            viewHolder.planToAttendTV = (TextView) postLayout.findViewById(R.id.planToAttendTV);
            viewHolder.addressTV = (TextView) postLayout.findViewById(R.id.postLocationTV);
            viewHolder.addToCalendarButton = (TextView) postLayout.findViewById(R.id.addToCalendarButton);
            viewHolder.addToCalConfirm = (TextView) postLayout.findViewById(R.id.addToCalendarConfirmedButton);
            viewHolder.postImageVF = (ViewFlipper) postLayout.findViewById(R.id.postBackgroundHolderVF);
            viewHolder.eventMap = (ImageView) postLayout.findViewById(R.id.location_map);
            viewHolder.detailsButton = (ImageButton) postLayout.findViewById(R.id.detailsButton);
            viewHolder.detailsButtonHelp = (Button) postLayout.findViewById(R.id.detailsButtonHelp);
            viewHolder.descriptionTV = (TextView) postLayout.findViewById(R.id.postEventDescription);
            viewHolder.showInfoButton = (Button) postLayout.findViewById(R.id.showinfobutton);
            viewHolder.eventInfoVF = (ViewFlipper) postLayout.findViewById(R.id.eventInfoVF);

            postLayout.setTag(viewHolder);
            String objectItem = viewContent.get(position);
            viewHolder.addToCalVF.setTag(objectItem);


        }
        else
        {
            viewHolder = (ViewHolder) postLayout.getTag();
            //viewHolder.addToCalVF = (ViewFlipper)
        }

            viewHolder.addToCalVF.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((ViewFlipper)view).showNext();
                    Log.i("VIEWFLIPPERVF", viewHolder.addToCalVF.toString());
                }
            });



            viewHolder.unameTV.setText(adapterUtilities.POSTSLISTS.get(position).get(0).toString());
            Log.i("POSTLISTS", adapterUtilities.POSTSLISTS.get(0).get(0).toString());
            Log.i("POSTLISTS", adapterUtilities.POSTSLISTS.get(1).get(0).toString());


        viewHolder.unameTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Starting new intent
                Intent in = new Intent(getActivity(),
                        Profile.class);
                // sending pid to next activity
                String username = ((TextView) view).getText().toString();
                in.putExtra("username", username);
                Log.i("Username", "" + username);

                // starting new activity and expecting some response back
                startActivityForResult(in, 100);
            }
        });

            viewHolder.fillSpace.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    View relayout = lv.getChildAt(0);
                    viewHolder.unameTV = (TextView) relayout.findViewById(R.id.postUnameTv);
                    String username = viewHolder.unameTV.getText().toString();

                    Intent in = new Intent(getActivity(),
                            Profile.class);
                    // sending pid to next activity
                    in.putExtra("username", username);
                    Log.i("Username", "" + username);

                    // starting new activity and expecting some response back
                    startActivityForResult(in, 100);
                }
            });




            DisplayImageOptions options = initiateDisplayImageOptions();
            viewHolder.image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    View relayout = lv.getChildAt(0);
                    viewHolder.unameTV = (TextView) relayout.findViewById(R.id.postUnameTv);
                    String username = viewHolder.unameTV.getText().toString();

                    Intent in = new Intent(getActivity(),
                            Profile.class);
                    // sending pid to next activity
                    in.putExtra("username", username);
                    Log.i("Username", "" + username);

                    // starting new activity and expecting some response back
                    startActivityForResult(in, 100);
                }
            });

            ImageLoader imageloader = ImageLoader.getInstance();
            initImageLoader(getActivity());
            imageloader.displayImage(adapterUtilities.POSTSLISTS.get(position).get(1).toString(), viewHolder.image, options);



        viewHolder.addToCalendarButton.setText(adapterUtilities.POSTSLISTS.get(position).get(2).toString());
        viewHolder.addToCalendarButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Calendar startCal = new GregorianCalendar();
                startCal.setTime(new Date());
                startCal.set(STARTYEARS.get(position), STARTMONTHS.get(position), STARTDAYS.get(position), STARTHOURS.get(position), STARTMINS.get(position));


                Calendar endCal = new GregorianCalendar();
                endCal.setTime(new Date());
                endCal.set(ENDYEARS.get(position), ENDMONTHS.get(position), ENDDAYS.get(position), ENDHOURS.get(position), ENDMINS.get(position));

                String id = Double.toString(EventUtils.assignName());

                View relayout;
                try {
                    relayout = lv.getChildAt(1);
                    viewHolder.eventTitle = (TextView) relayout.findViewById(R.id.postTitleTV);
                    title = viewHolder.eventTitle.getText().toString();

                    addEvent(id, title, startCal.getTimeInMillis(), endCal.getTimeInMillis(), position);

                    viewHolder.addToCalVF = (ViewFlipper) relayout.findViewById(R.id.addToCalendarVF);
                    viewHolder.addToCalVF.showNext();

                    Log.i("VIEWFLIPPER", viewHolder.addToCalVF.toString());
                }
                catch (NullPointerException np)
                {
                    relayout = lv.getChildAt(0);
                    viewHolder.eventTitle = (TextView) relayout.findViewById(R.id.postTitleTV);
                    title = viewHolder.eventTitle.getText().toString();

                    addEvent(id, title, startCal.getTimeInMillis(), endCal.getTimeInMillis(), position);

                    viewHolder.addToCalVF = (ViewFlipper) relayout.findViewById(R.id.addToCalendarVF);
                    viewHolder.addToCalVF.showNext();

                    Log.i("VIEWFLIPPER", viewHolder.addToCalVF.toString());

                }

            }
        });





            viewHolder.eventTitle.setText(adapterUtilities.POSTSLISTS.get(position).get(3).toString());


            testText = viewHolder.eventTitle;
            imageloader.displayImage(adapterUtilities.POSTSLISTS.get(position).get(4).toString(), viewHolder.eventImage, options);


            viewHolder.likesTV.setText("" + adapterUtilities.POSTSLISTS.get(position).get(5).toString());

            viewHolder.planToAttendTV.setText(adapterUtilities.POSTSLISTS.get(position).get(6).toString());

            viewHolder.addressTV.setText("" + adapterUtilities.POSTSLISTS.get(position).get(7).toString());
            viewHolder.addressTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    viewHolder.detailsButton.performClick();
                }
            });



            viewHolder.addToCalConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    View relayout;
                    try {

                        relayout = lv.getChildAt(1);
                        viewHolder.addToCalVF = (ViewFlipper) relayout.findViewById(R.id.addToCalendarVF);
                        viewHolder.addToCalVF.showNext();

                        viewHolder.eventTitle = (TextView) relayout.findViewById(R.id.postTitleTV);
                        title = viewHolder.eventTitle.getText().toString();
                        DeleteCalendarEntry(EVENTIDS.get(position), title);
                    }
                    catch (NullPointerException np)
                    {
                        relayout = lv.getChildAt(0);
                        viewHolder.addToCalVF = (ViewFlipper) relayout.findViewById(R.id.addToCalendarVF);
                        viewHolder.addToCalVF.showNext();

                        viewHolder.eventTitle = (TextView) relayout.findViewById(R.id.postTitleTV);
                        title = viewHolder.eventTitle.getText().toString();
                        DeleteCalendarEntry(EVENTIDS.get(position), title);
                    }



                }
            });


        viewHolder.detailsButtonHelp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewHolder.detailsButton.performClick();
            }
        });




        viewHolder.detailsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {



            }
        });





        viewHolder.showInfoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("POSITIONINVIEW", "" + lv.getLastVisiblePosition());

                View relayout;
                try {
                    relayout = lv.getChildAt(1);
                    viewHolder.eventInfoVF = (ViewFlipper) relayout.findViewById(R.id.eventInfoVF);
                    viewHolder.eventInfoVF.setInAnimation(AnimationUtils.loadAnimation(getActivity(),
                            R.anim.push_up_in));
                    viewHolder.eventInfoVF.setOutAnimation(AnimationUtils.loadAnimation(getActivity(),
                            R.anim.push_down_out));

                    viewHolder.eventInfoVF.showNext();
                }
                catch (NullPointerException np)
                {
                    relayout = lv.getChildAt(0);
                    viewHolder.eventInfoVF = (ViewFlipper) relayout.findViewById(R.id.eventInfoVF);
                    viewHolder.eventInfoVF.setInAnimation(AnimationUtils.loadAnimation(getActivity(),
                            R.anim.push_up_in));
                    viewHolder.eventInfoVF.setOutAnimation(AnimationUtils.loadAnimation(getActivity(),
                            R.anim.push_down_out));

                    viewHolder.eventInfoVF.showNext();
                }
            }
        });


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

    @Override
    public String getItem(int position) {
        return posts.get(position);
    }

     class ViewHolder {
         ImageView image;
         TextView addToCalendarButton;
         TextView eventTitle;
         ImageView eventImage;
         ImageView eventMap;
         TextView likesTV;
         TextView planToAttendTV;
         TextView addressTV;
         TextView unameTV;
         TextView descriptionTV;
         TextView fillSpace;
         ImageButton detailsButton;
         Button detailsButtonHelp;
         Button showInfoButton;

         TextView addToCalConfirm;
         ViewFlipper addToCalVF;
         ViewFlipper postImageVF;
         ViewFlipper eventInfoVF;

     }

}

根据我的理解,使用自定义适配器,屏幕上可见项目的位置被视为位置0,位于位置1之后的位置。我通过使用3个项目填充列表视图来测试3个布局被扩展到列表视图中。但是,当我单击布局上的任何按钮时,会发生正确的操作,但是对于距离所需布局1个项目的布局也会发生这种情况。因此,当我单击位置0列表视图中布局中的按钮时,位于列表视图中位置2的布局会发生相同的操作,反之亦然,当我在列表视图位置2中选择一个按钮时,我会尝试制作每个按钮。 listview项目针对每个特定位置独立且正确地运行,但我无法自己或在互联网上找到答案。如何在自定义适配器中正确添加onclicklistener,使视图的行为适合其所需位置?就像按钮这样的Instagrams如何“喜欢”想要的图片一样?

0 个答案:

没有答案