使用OnTouchListener的ListView项会产生多个侦听行

时间:2014-11-12 11:37:15

标签: android listview android-arrayadapter ontouchlistener

我使用自定义适配器构建列表视图。我希望每个列表项都能在ontouchlistener上响应。因此,在getView()方法内部,在返回视图之前将侦听器分配给视图。

当我触摸行时,需要在左边的200px内设置LinearLayout的边距,当我释放行时,需要将边距设置回0。

我选择了OnTouchListener,因为稍后在我的项目中我想处理一些移动事件。

这是我的代码:

public class BudgetAdapter extends ArrayAdapter<Budget>{

    private Context context;
    private int layoutResourceId;
    private List<Budget> data = null;
    private View.OnTouchListener listener;
    private LayoutInflater inflater;


    public BudgetAdapter(Context context, int layoutResourceId, List<Budget> data) {
        super(context, layoutResourceId, data);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        this.data = data;
        listener = new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                LinearLayout front = (LinearLayout)view.findViewById(R.id.budget_list_item_header);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT
                );
                if ( motionEvent.getAction() == MotionEvent.ACTION_DOWN)
                {
                    params.setMargins(0,0,200,0);
                    front.setLayoutParams(params);

                }
                if ( motionEvent.getAction() == MotionEvent.ACTION_UP || motionEvent.getAction() == MotionEvent.ACTION_CANCEL)
                {

                    params.setMargins(0,0,0,0);
                    front.setLayoutParams(params);
                }
                return true;
            }
        };

        this.inflater = ((Activity)context).getLayoutInflater();
    }



    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        View row = convertView;
        BudgetHolder holder;
        if(row == null)
        {
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new BudgetHolder();
            holder.titleText = (TextView)row.findViewById(R.id.budget_list_item_title_label);
            holder.totalText = (TextView)row.findViewById(R.id.budget_list_item_total_label);
            holder.remainingText = (TextView)row.findViewById(R.id.budget_list_item_remaining_label);
            holder.remainingBar = (RemainingBar)row.findViewById(R.id.budget_list_item_remaining_bar);
            row.setTag(holder);
        }
        else
        {
            holder = (BudgetHolder)row.getTag();
        }

        Budget budget = data.get(position);
        holder.titleText.setText(budget.getShortDescription());
        holder.totalText.setText("budget: " + budget.getTotal().toString());
        holder.remainingText.setText(budget.getRemainingAsString());
        holder.remainingBar.setRemaining(budget.getExpensesAmount());
        holder.remainingBar.setTotal(budget.getTotal());

        row.setOnTouchListener(listener);
        return row;
    }

    static class BudgetHolder {
        TextView titleText;
        TextView totalText;
        TextView remainingText;
        RemainingBar remainingBar;
    }
}

我有以下问题。 当我触摸一行时,随机的一些行获得新的边距(有些行将其加倍到400px)。当我随机释放行时,某些行的边距加倍,将使边距恢复到200px或0px。

如何实现我想要的扫描,只有被触摸的行才能响应事件。

0 个答案:

没有答案