0

我创建了一个自定义列表视图,在每行的左侧有两个箭头(向上/向下投票)。使用选择器,我已经能够更改箭头的图像(颜色)并相应地更新投票。我的问题是,例如,如果我单击“向上投票”,只要我向下滚动到足够远,以至于我投票的列表项离开屏幕,它就会重置为原始的未选中状态/图像。

这是我的获取视图方法

 public View getView(final int position, View convertView, ViewGroup parent)
    {
        //creating the ViewHolder we defined earlier.
        final ViewHolder holder = new ViewHolder(); 

        //creating LayoutInflator for inflating the row layout.
        LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //inflating the row layout we defined earlier.
        convertView = inflator.inflate(R.layout.partylist, null);

        //setting the views into the ViewHolder.
        holder.title = (TextView) convertView.findViewById(R.id.title);
        holder.voteUp = (ImageView) convertView.findViewById(R.id.vote_up_image);
        holder.voteUp.setTag(position);
        holder.voteDown = (ImageView) convertView.findViewById(R.id.vote_down_image);
        holder.voteDown.setTag(position);
        holder.votes = (TextView) convertView.findViewById(R.id.votes);

        //define an onClickListener for the ImageView.
        holder.voteUp.setOnClickListener(new OnClickListener() 
        {           
            @Override
            public void onClick(View v) 
            { 
                if (v.isSelected()){
                    v.setSelected(false);
                    holder.votes.setText(String.valueOf((list.get(position).votes)));
                } else {
                    if (holder.voteDown.isSelected()){
                    v.setSelected(true);
                    holder.voteDown.setSelected(false);
                    holder.votes.setText(String.valueOf((list.get(position).votes+1)));
                    }else{
                        v.setSelected(true);
                    }
                }

                //Toast.makeText(PartyActivity.this, "Image from row " + position + " was pressed " + list.get(position).title, Toast.LENGTH_LONG).show();
            }
        });

        holder.voteDown.setOnClickListener(new OnClickListener() 
        {           
            @Override
            public void onClick(View w) 
            { 
                if (w.isSelected()){
                    w.setSelected(false);
                    holder.votes.setText(String.valueOf((list.get(position).votes)));
                } else {
                    if (holder.voteUp.isSelected()){
                    w.setSelected(true);
                    holder.voteUp.setSelected(false);
                    holder.votes.setText(String.valueOf((list.get(position).votes-1)));
                    }else{
                        w.setSelected(true);
                    }
                }

                //Toast.makeText(PartyActivity.this, "Image from row " + position + " was pressed " + list.get(position).title, Toast.LENGTH_LONG).show();
            }
        });


        //setting data into the the ViewHolder.
        holder.title.setText(list.get(position).title);
        holder.votes.setText(String.valueOf(list.get(position).votes));
        //return the row view.
        return convertView;
    }
}
4

2 回答 2

0

所以我自己想通了。这是由于对教程的糟糕理解造成的。

我将一个对象传递给我的自定义适配器,我所要做的就是在对象内创建一些布尔变量来存储是否在每一行上选择了复选框/选择器,然后在单击图像时适当地设置这些变量。

这是我的新 getview() 给任何需要帮助的人

public View getView(final int position, View convertView, ViewGroup parent)
    {
        //creating the ViewHolder we defined earlier.
        final ViewHolder holder = new ViewHolder(); 

        //creating LayoutInflator for inflating the row layout.
        LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //inflating the row layout we defined earlier.
        convertView = inflator.inflate(R.layout.partylist, null);

        //setting the views into the ViewHolder.
        holder.title = (TextView) convertView.findViewById(R.id.title);
        holder.voteUp = (CheckBox) convertView.findViewById(R.id.vote_up_image);
        holder.voteUp.setTag(position);
        holder.voteDown = (CheckBox) convertView.findViewById(R.id.vote_down_image);
        holder.voteDown.setTag(position);
        holder.votes = (TextView) convertView.findViewById(R.id.votes);




        //define an onClickListener for the ImageView.

        holder.voteUp.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (holder.voteUp.isSelected()){
                    holder.voteUp.setSelected(false);
                    list.get(position).Up=false;
                    holder.votes.setText(String.valueOf((list.get(position).votes)));
                } else {
                    if (holder.voteDown.isSelected()){
                    holder.voteUp.setSelected(true);
                    holder.voteDown.setSelected(false);
                    list.get(position).Up=true;
                    list.get(position).Down=false;
                    holder.votes.setText(String.valueOf((list.get(position).votes+1)));
                    }else{
                        holder.voteUp.setSelected(true);
                        list.get(position).Up=true;
                        holder.votes.setText(String.valueOf((list.get(position).votes+1)));
                    }

            }}
        });

        holder.voteDown.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {// Toast.makeText(PartyActivity.this, "Image from row " + position + " was pressed " + list.get(position).title, Toast.LENGTH_LONG).show();
                if (holder.voteDown.isSelected()){
                    holder.voteDown.setSelected(false);
                    list.get(position).Down=false;
                    holder.votes.setText(String.valueOf((list.get(position).votes)));
                } else {
                    if (holder.voteUp.isSelected()){
                    holder.voteDown.setSelected(true);
                    holder.voteUp.setSelected(false);
                    list.get(position).Down=true;
                    list.get(position).Up=false;
                    holder.votes.setText(String.valueOf((list.get(position).votes-1)));
                    }else{
                        holder.voteDown.setSelected(true);
                        list.get(position).Down=true;
                        holder.votes.setText(String.valueOf((list.get(position).votes-1)));
                    }

            }}
        }); 


        //setting data into the the ViewHolder.
        holder.title.setText(list.get(position).title);
        holder.votes.setText(String.valueOf(list.get(position).votes));
        holder.voteUp.setChecked(list.get(position).Up);
        holder.voteDown.setChecked(list.get(position).Down);
        //return the row view.
        return convertView;
    }
于 2013-09-13T04:16:58.147 回答
0

我所做的是创建一个实现 Checkable 的自定义 RelativeLayout。我正在使用背景颜色来标记所选项目,但您也可以使用图像:

package com.xxx;

public class DictionaryRelativeLayout extends RelativeLayout implements Checkable {

public DictionaryRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public DictionaryRelativeLayout(Context context) {
    super(context);
}

private boolean mChecked = false;

@Override
public boolean isChecked() {
    return mChecked;
}

@Override
public void setChecked(boolean checked) {
    mChecked = checked;
    this.setBackgroundColor(mChecked ? 
            getResources().getColor(R.color.list_selected) : 
                getResources().getColor(R.color.list_default));
}

@Override
public void toggle() {
    setChecked(!mChecked);
}
}

当我为列表项定义布局时,我使用该 RelativeLayout:

<com.xxx.DictionaryRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:padding="@dimen/list_margin" >

... the rest of the layout

</com.xxx.DictionaryRelativeLayout>

我在片段中使用我的列表,您必须定义您的列表正在使用单选。我在 onActivityCreated 中执行此操作:

 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    getListView().setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
 }
于 2013-09-10T21:18:37.960 回答