ListView滚动问题

时间:2014-03-25 10:47:47

标签: android android-layout android-listview scroll

我有ListView个自定义行项目。我的项目有一个按钮,当我点击它时,然后在这一行动态添加新按钮。我的问题是当我滚动列表。动态添加按钮显示错误的行。这是重用视图的问题,但我不知道如何记住行的状态。请帮我。这是我的适配器,方法getView。

@Override
public View getView(final int position, final View convertView, ViewGroup parent) {
    View rowView = convertView;
    ViewHolder holder;
    final int positionRow = position;
    model = values.get(position);

    if (rowView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder(rowView);
        rowView.setTag(holder);
    }else
    {

        holder = (ViewHolder) rowView.getTag();
        ((ViewHolder) rowView.getTag()).mPortionButton.setTag(model);

    }

    if(values == null || ((position + 1)> values.size()) )
        return rowView;


    final int[] portionCounter = {1};
    LayoutInflater layoutInflater = (LayoutInflater) context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View counterButton = layoutInflater.inflate(R.layout.button_counter, null);
    Button counterB = (Button) counterButton.findViewById(R.id.myButton);
    counterB.setTypeface(MainActivity.font);
    final LinearLayout[] layout = {(LinearLayout) rowView.findViewById(R.id.portionLayout)};


    holder.mPortionButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (portionCounter[0] == 1) {
                layout[0].addView(counterButton);
                ++portionCounter[0];
            }
            else {
                TextView counter = (TextView) counterButton.findViewById(R.id.textOne);
                counter.setText(String.valueOf(portionCounter[0]));
                ++portionCounter[0];
            }

        }
    });



    holder.mProduct.setText(model.getName());

    holder.mBrand.setText(model.getBrand());
    if (model.getProductThumb()!=null && model.getProductThumb().trim().length() > 0){
        Picasso.with(context)
                .load(model.getProductThumb())
                .into(holder.mProductIcon);
    }
    else{
        Picasso.with(context)
                .load(R.drawable.temp_icon)
                .into(holder.mProductIcon);
    }


    return rowView;
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

你有模特所以只需使用它。您的模型必须记住添加按钮的时间,以及稍后调用方法getView时,您必须获得先前保存的行状态。样品:

@Override
public View getView(final int position, final View convertView, ViewGroup parent) {
    model = values.get(position);
    ViewGroup rowContainer = (ViewGroup) convertView;

    View button = convertView.getChildAt(0);//get already added button (if it was). getChildAt method is example. It depends on your layout.

    if(model.isButtonAdded()){
        if(button == null){//button was not added before then you shoul add it
            rowContainer.addView(new Button());
        }
    }else{
        if(button != null){//button exists, you should remove it
            rowContainer.removeChild(button);
        }
    }
}