滚动时,ListView Listview项改变位置。为什么?

时间:2014-01-24 13:00:32

标签: java android listview

当我滚动ListView项目的位置发生变化时,我ListView包含2个Button和2个TextView。这两个Button最初是不可见的。当用户长期点击项目时,我只是设置了visibility.Its工作但是当我滚动ListView时,它会改变位置。为什么?

我在我的适配器类中设置了这个可见性和隐形代码。 这是我的代码:::

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

    holder = new ViewHolder();
    if (convertView == null) {
        convertView = lInflater.inflate(R.layout.message_row, null);
        holder.messageDeleteButton = (Button) convertView
                .findViewById(R.id.delete_button);
        holder.messageTextView = (TextView) convertView
                .findViewById(R.id.message);
        holder.timeTextView = (TextView) convertView
                .findViewById(R.id.time);
        holder.copyMessageButton = (ImageButton) convertView
                .findViewById(R.id.copy_bt);

        holder.messageDeleteButton
                .setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(final View v) {

         System.out.println("Delete clicked");
                        }
                });

        convertView.setTag(R.layout.message_row, holder);
    } else {
        holder = (ViewHolder) convertView.getTag(R.layout.message_row);
    }
    convertView.setTag(position);

    holder.messageDeleteButton.setTag(position);
    if (mMessageData.get(position).isVisible())
        holder.messageDeleteButton.setVisibility(View.VISIBLE);
    else
        holder.messageDeleteButton.setVisibility(View.GONE);

    holder.messageTextView.setText(mMessageData.get(position)
            .getMessage_text());
    holder.messageTextView.setTypeface(Typeface.createFromAsset(
            mContext.getAssets(), "Existence-Light.ttf"));
    holder.timeTextView.setText(mMessageData.get(position)
            .getMessage_date());
    holder.timeTextView.setTypeface(Typeface.createFromAsset(
            mContext.getAssets(), "Existence-Light.ttf"));
    convertView.setOnTouchListener(mOnTouchListener);
    convertView.setOnClickListener(mOnClickListener);
    // convertView.setOnLongClickListener(mLongClick);
    return convertView;
}

这是我的OnTouchListiner代码,它可以正常工作。但是当我滚动ListView按钮时,位置会发生变化..

      class MyTouchListener implements OnTouchListener {
    @SuppressWarnings("deprecation")
    private final GestureDetector gestureDetector = new GestureDetector(
            new GestureListener());

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        int touch = 0;
        ViewHolder holder = (ViewHolder) v.getTag(R.layout.message_row);
        animation = new Animater();
        int action = event.getAction();
        int position = (Integer) v.getTag();
        pos = position;
        view = v;
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            action_down_x = (int) event.getX();
            Log.d("action", "ACTION_DOWN - ");
            touch = 0;
            break;
        case MotionEvent.ACTION_MOVE:
            Log.d("action", "ACTION_MOVE - ");
            action_up_x = (int) event.getX();
            difference = action_down_x - action_up_x;
            calcuateDifference(holder, position);
            touch = 1;
            break;
        case MotionEvent.ACTION_UP:
            Log.d("action", "ACTION_UP - ");
            // calcuateDifference(holder, position);
            action_down_x = 0;
            action_up_x = 0;
            difference = 0;
            // touch = 2;

            break;
        }
        return gestureDetector.onTouchEvent(event);
    }

    public GestureDetector getGestureDetector() {
        return gestureDetector;
    }
}

在这个gestuer课程中,当我点击ListView时,只显示一个Button。这也会改变位置

 public class GestureListener extends SimpleOnGestureListener{
    @SuppressLint("NewApi")
    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

        super.onLongPress(e);


        final ViewHolder hold = (ViewHolder) view.getTag(R.layout.message_row);
        hold.copyMessageButton.setVisibility(View.VISIBLE);
        hold.copyMessageButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                System.out.println("hello you click me..");
        });


    }

}

我的代码有什么问题。任何人请帮助我......

2 个答案:

答案 0 :(得分:1)

Android ListView使用适配器来回收视图,这就是滚动以节省内存时ListView位置更改的原因。请参阅Romain Guy提供的Google I / O演示文稿,了解适配器的工作原理。 http://www.youtube.com/watch?v=N6YdwzAvwOA

另请查看以下链接

http://strangevikas.wordpress.com/tag/how-adapter-works-in-android-android-and-adapters/

http://www.edureka.in/blog/what-are-adapters-in-android/

答案 1 :(得分:0)

您只需在代码中进行一些修改,例如:

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

    ViewHolder holder = new ViewHolder();
    if (convertView == null) {
        convertView = lInflater.inflate(R.layout.message_row, null);
        holder.messageDeleteButton = (Button) convertView
                .findViewById(R.id.delete_button);
        holder.messageTextView = (TextView) convertView
                .findViewById(R.id.message);
        holder.timeTextView = (TextView) convertView
                .findViewById(R.id.time);
        holder.copyMessageButton = (ImageButton) convertView
                .findViewById(R.id.copy_bt);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.messageDeleteButton.setTag(mMessageData.get(position));

    holder.messageDeleteButton
    .setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(final View v) {

            // message data can be retrieved here like
            MessageData mData = (MessageData) v.getTag(); 
            System.out.println("Delete clicked");
        }
    });

    if (mMessageData.get(position).isVisible())
        holder.messageDeleteButton.setVisibility(View.VISIBLE);
    else
        holder.messageDeleteButton.setVisibility(View.GONE);

    holder.messageTextView.setText(mMessageData.get(position)
            .getMessage_text());

    holder.messageTextView.setTypeface(Typeface.createFromAsset(
            mContext.getAssets(), "Existence-Light.ttf"));
    holder.timeTextView.setText(mMessageData.get(position)
            .getMessage_date());
    holder.timeTextView.setTypeface(Typeface.createFromAsset(
            mContext.getAssets(), "Existence-Light.ttf"));

    return convertView;
}