列表视图中的按钮在滚动和按钮单击时产生问题

时间:2015-11-28 11:15:19

标签: android button android-listview custom-lists

我在列表视图中的一些列表项中有三个按钮,单击该按钮我想更改该列表项的布局,但我面临的问题列在下面。

1)。单击按钮,另一个列表项布局将更改。 2)。在列表视图的滚动中,另一个列表项目布局被更改为我没有单击的人。

这是适配器类的代码。

import java.util.ArrayList;

import com.xsinfosol.DOT.R;
import com.xsinfosol.DOT.ImageLoading.ImageLoader;
import com.xsinfosol.DOT.LibraryClasses.RippleView;
import com.xsinfosol.DOT.model.DOT_Common_Model;

import android.content.Context;

import android.graphics.drawable.Drawable;
import android.support.v7.internal.widget.ButtonBarLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Agenda_Adapter  extends BaseAdapter {

    Context context;
    ArrayList<DOT_Common_Model> arrayList;
    ViewHolder viewHolder;

    public Agenda_Adapter(Context context , ArrayList<DOT_Common_Model>  arrayList) {
        // TODO Auto-generated constructor stub


        this.context = context;
        this.arrayList = arrayList;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub



        if(convertView==null)
        {
            viewHolder = new ViewHolder();



            convertView = LayoutInflater.from
                    (context).inflate(R.layout.agenda_event_list_item, null);
            viewHolder.linearLayout = (LinearLayout)convertView.findViewById(R.id.agenda_button_layout);
            viewHolder.linearLayout.setVisibility(View.GONE);
            viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.agenda_event_checkbox);
            viewHolder.eventName = (TextView)convertView.findViewById(R.id.agenda_event_name);
            viewHolder.imageView = (ImageView)convertView.findViewById(R.id.agenda_event_imae);
            viewHolder.place = (TextView)convertView.findViewById(R.id.agenda_event_place);
            viewHolder.time = (TextView)convertView.findViewById(R.id.agenda_event_date_time);
            viewHolder.going = (RippleView)convertView.findViewById(R.id.agenda_rippleview_going);
            viewHolder.notGoing = (RippleView)convertView.findViewById(R.id.agenda_rippleview_not_going);
            viewHolder.mayBe = (RippleView)convertView.findViewById(R.id.agenda_rippleview_maybe);
            viewHolder.going.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    viewHolder.going.setVisibility(View.GONE);
                    viewHolder.notGoing.setVisibility(View.GONE);
                    viewHolder.mayBe.setVisibility(View.GONE);


                    Drawable tick = context.getResources().getDrawable(R.drawable.ic_action_tick);
                    tick.setBounds(0,0, 30, 30);
                    Button going = new Button(context);

                    LinearLayout.LayoutParams params  = new LinearLayout.LayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 40));
                    params.setMargins(10,0, 10, 4);
                    going.setLayoutParams(params);
                    going.setBackgroundColor(context.getResources().getColor(android.R.color.holo_green_dark));
                    going.setText("Going");
                    going.setTextSize(15);
                    going.setTextColor(R.color.white);
                    going.setCompoundDrawables(null, null, tick, null);
                    going.setCompoundDrawablePadding(5);
                    going.setGravity(Gravity.CENTER);
                    if(viewHolder.linearLayout!=null)
                        viewHolder.linearLayout.removeAllViews();
                    viewHolder.linearLayout.addView(going);
                }
            });


            viewHolder.notGoing.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    viewHolder.going.setVisibility(View.GONE);
                    viewHolder.notGoing.setVisibility(View.GONE);
                    viewHolder.mayBe.setVisibility(View.GONE);


                    Drawable cross = context.getResources().getDrawable(R.drawable.ic_action_cancel);
                    cross.setBounds(0,0, 30, 30);
                    Button button = new Button(context);

                    LinearLayout.LayoutParams paramsCross  = new LinearLayout.LayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 40));
                    paramsCross.setMargins(10,0, 10, 4);
                    button.setLayoutParams(paramsCross);
                    button.setBackgroundColor(context.getResources().getColor(R.color.red));
                    button.setText("Not Going");
                    button.setTextSize(15);
                    button.setTextColor(R.color.white);
                    button.setCompoundDrawables(null, null, cross, null);
                    button.setCompoundDrawablePadding(5);
                    button.setGravity(Gravity.CENTER);
                    if(viewHolder.linearLayout!=null)
                            viewHolder.linearLayout.removeAllViews();
                    viewHolder.linearLayout.addView(button);
                }
            });


            viewHolder.mayBe.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    viewHolder.going.setVisibility(View.GONE);
                    viewHolder.notGoing.setVisibility(View.GONE);
                    viewHolder.mayBe.setVisibility(View.GONE);


                    Drawable mayBe = context.getResources().getDrawable(R.drawable.ic_action_emo_err);
                    mayBe.setBounds(0,0, 30, 30);
                    Button maybe = new Button(context);

                    LinearLayout.LayoutParams paramsMaybe  = new LinearLayout.LayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 40));
                    paramsMaybe.setMargins(10,0, 10, 4);
                    maybe.setLayoutParams(paramsMaybe);
                    maybe.setBackgroundColor(context.getResources().getColor(R.color.yellow));
                    maybe.setText("May Be");
                    maybe.setTextSize(15);
                    maybe.setTextColor(R.color.white);
                    maybe.setCompoundDrawables(null, null, mayBe, null);
                    maybe.setCompoundDrawablePadding(5);
                    maybe.setGravity(Gravity.CENTER);
                    if(viewHolder.linearLayout!=null)
                            viewHolder.linearLayout.removeAllViews();
                    viewHolder.linearLayout.addView(maybe);

                }
            });


            viewHolder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub


                    int getPosition  = (Integer)buttonView.getTag();

                    arrayList.get(getPosition).setChecked(buttonView.isChecked());



                }
            });

            convertView.setTag(viewHolder);
            convertView.setTag(R.id.agenda_event_checkbox, viewHolder.checkBox);
            convertView.setTag(R.id.agenda_event_imae, viewHolder.imageView);
            convertView.setTag(R.id.agenda_event_name, viewHolder.eventName);
            convertView.setTag(R.id.agenda_event_place, viewHolder.place);
            convertView.setTag(R.id.agenda_event_date_time, viewHolder.time);
            convertView.setTag(R.id.agenda_button_layout, viewHolder.linearLayout);
            convertView.setTag(R.id.agenda_rippleview_going, viewHolder.going);
            convertView.setTag(R.id.agenda_rippleview_not_going, viewHolder.notGoing);
            convertView.setTag(R.id.agenda_rippleview_maybe, viewHolder.mayBe);






        }else
            viewHolder = (ViewHolder)convertView.getTag();



        viewHolder.checkBox.setTag(position);
        viewHolder.going.setTag(position);
        viewHolder.notGoing.setTag(position);
        viewHolder.mayBe.setTag(position);

        ImageLoader imageLoader = new ImageLoader(context);
        ImageView imageView = viewHolder.imageView;
        imageLoader.DisplayImage(arrayList.get(position).getImage(), imageView);


        viewHolder.checkBox.setChecked(arrayList.get(position).ischecked());

        switch (arrayList.get(position).getFlag()) {
        case "0":
            // hasn's seleted any option
            if(viewHolder.linearLayout.getVisibility()==View.GONE)
                    viewHolder.linearLayout.setVisibility(View.VISIBLE);

            if(viewHolder.going.getVisibility()==View.GONE)
                    viewHolder.going.setVisibility(View.VISIBLE);

            if(viewHolder.mayBe.getVisibility()==View.GONE)
                viewHolder.mayBe.setVisibility(View.VISIBLE);

            if(viewHolder.notGoing.getVisibility()==View.GONE)
                viewHolder.notGoing.setVisibility(View.VISIBLE);



            break;

        case "1":
            // selected going

            viewHolder.going.setVisibility(View.GONE);
            viewHolder.notGoing.setVisibility(View.GONE);
            viewHolder.mayBe.setVisibility(View.GONE);


            Drawable tick = context.getResources().getDrawable(R.drawable.ic_action_tick);
            tick.setBounds(0,0, 30, 30);
            Button going = new Button(context);

            LinearLayout.LayoutParams params  = new LinearLayout.LayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 40));
            params.setMargins(10,0, 10, 4);
            going.setLayoutParams(params);
            going.setBackgroundColor(context.getResources().getColor(android.R.color.holo_green_dark));
            going.setText("Going");
            going.setTextSize(15);
            going.setTextColor(context.getResources().getColor(R.color.white));
            going.setCompoundDrawables(null, null, tick, null);
            going.setCompoundDrawablePadding(5);
            going.setGravity(Gravity.CENTER);
            if(viewHolder.linearLayout!=null)
                viewHolder.linearLayout.removeAllViews();
            viewHolder.linearLayout.addView(going);







            break;
        case "2":
            // select not going


            viewHolder.going.setVisibility(View.GONE);
            viewHolder.notGoing.setVisibility(View.GONE);
            viewHolder.mayBe.setVisibility(View.GONE);


            Drawable cross = context.getResources().getDrawable(R.drawable.ic_action_cancel);
            cross.setBounds(0,0, 30, 30);
            Button button = new Button(context);

            LinearLayout.LayoutParams paramsCross  = new LinearLayout.LayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 40));
            paramsCross.setMargins(10,0, 10, 4);
            button.setLayoutParams(paramsCross);
            button.setBackgroundColor(context.getResources().getColor(R.color.red));
            button.setText("Not Going");
            button.setTextSize(15);
            button.setTextColor(context.getResources().getColor(R.color.white));
            button.setCompoundDrawables(null, null, cross, null);
            button.setCompoundDrawablePadding(5);
            button.setGravity(Gravity.CENTER);
            if(viewHolder.linearLayout!=null)
                    viewHolder.linearLayout.removeAllViews();
            viewHolder.linearLayout.addView(button);







            break;

        case "3":

            // selected may be

            viewHolder.going.setVisibility(View.GONE);
            viewHolder.notGoing.setVisibility(View.GONE);
            viewHolder.mayBe.setVisibility(View.GONE);


            Drawable mayBe = context.getResources().getDrawable(R.drawable.ic_action_emo_err);
            mayBe.setBounds(0,0, 30, 30);
            Button maybe = new Button(context);

            LinearLayout.LayoutParams paramsMaybe  = new LinearLayout.LayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 40));
            paramsMaybe.setMargins(10,0, 10, 4);
            maybe.setLayoutParams(paramsMaybe);
            maybe.setBackgroundColor(context.getResources().getColor(R.color.yellow));
            maybe.setText("May Be");
            maybe.setTextSize(15);
            maybe.setTextColor(context.getResources().getColor(R.color.black_overlay));
            maybe.setCompoundDrawables(null, null, mayBe, null);
            maybe.setCompoundDrawablePadding(5);
            maybe.setGravity(Gravity.CENTER);
            if(viewHolder.linearLayout!=null)
                    viewHolder.linearLayout.removeAllViews();
            viewHolder.linearLayout.addView(maybe);






            break;

        case "4":
            // event doesn't have any invitatin option

            if(viewHolder.linearLayout.getVisibility()==View.VISIBLE)
                    viewHolder.linearLayout.setVisibility(View.GONE);



            break;
        }




        viewHolder.eventName.setText(arrayList.get(position).getName());
        viewHolder.place.setText(arrayList.get(position).getPlace());
        viewHolder.time.setText(arrayList.get(position).getTime());


























        return convertView;
    }

static class ViewHolder
{
    TextView eventName , place, time;
    CheckBox checkBox;
    ImageView imageView;
    LinearLayout linearLayout;
    RippleView going , notGoing , mayBe;


}



}

请帮助我,我非常困难,

4 个答案:

答案 0 :(得分:1)

第1步:在此行之后放置点击侦听器(viewHolder =(ViewHolder)convertView.getTag())

第2步:在onclick方法中删除视图创建/删除逻辑。更改模型值(“在您的情况下当前项目标志”)并调用notifyDataset更改的方法

@Override
public void onClick(View view) {
    //Set the flag based on your view click
    ((DOT_Common_Model) getItem( int position)).setFlag(0);
    notifyDatasetChanged();
}

第3步:根据您的模型值更改视图(“标记值”)

if(((DOT_Common_Model) getItem( int position)).getFlag()==0){
    view1.setVisibility(View.VISIBLE);
    view2.setVisibility(View.GONE);
    view3.setVisibility(View.GONE);
}else if(((DOT_Common_Model) getItem( int position)).getFlag()==1){
    view1.setVisibility(View.GONE);
    view2.setVisibility(View.VISIBLE);
    view3.setVisibility(View.GONE);
}else if(((DOT_Common_Model) getItem( int position)).getFlag()==2){
    view1.setVisibility(View.GONE);
    view2.setVisibility(View.GONE);
    view3.setVisibility(View.VISIBLE);
}else{
    view1.setVisibility(View.VISIBLE);
    view2.setVisibility(View.VISIBLE);
    view3.setVisibility(View.VISIBLE);
}

注意:列表视图仅绑定数据,它永远不会更改每个数据值的视图查看对象,我们需要根据数据值更改视图可见性/背景。如果您使用“ if condition ”,那么“ else ”必须在那里。

答案 1 :(得分:0)

我看到你使用的适配器代码仅在条件时使用,listview不那么聪明,所以每当你只使用如果不写 else 部分然后执行一个事件那么ListView也可以改变下一个位置View,visibility或if部分中的任何内容,所以请在你的适配器代码中写下if的其他部分

我建议使用RecyclerView比ListView更先进。

答案 2 :(得分:0)

简答:

将你的onClick监听器放在if else块之外:

if(convertView==null) {
    //BLABLABLA
} else {

}

// WRITE YOUR CLICK LISTENER HERE
viewHolder.mayBe.setOnClickListener(new OnClickListener() { ..... });
viewHolder.notGoing.setOnClickListener(new OnClickListener() { .. });
viewHolder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { .... } );

为什么会出现问题

您正在做的事情称为ViewHolderPattern。它主要用于通过避免不必要的 findViewById 来提高滚动列表视图的性能。这是一个繁重的过程。

让我们看看它是如何运作的:

请看这张显示正常列表的图片: enter image description here

我们的适配器有很多行,但只有3行填充了视图。当您制作适配器时,只有前3行为空并且应该充气。其他视图将使用之前创建的相同视图。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // in our example, the convert view is only null for the first 3 items 
    if(convertView==null) {

    } else {

    }
}

注意事项是:你不应该在if else阻止中设置任何内容。你应该只是在它们中找到你的视图并将它们分配给你的viewHolder并查看相关的东西(比如margin,padding,textsize,......)。您不应该在该块中执行资源或单击与侦听器相关的事物(如setText,setBitmap,Onclicklistener,...)

为什么?! 我们之前回答过!因为它只会被填充视图的第一个项目调用!

答案 3 :(得分:-1)

您未在else案例中处理点击侦听器:

else if (convertView==null)

将所有OnClicklistener写在外面(return convertView上方)。