Android LongClick在Ui更新时被中断

时间:2015-08-11 01:43:14

标签: android multithreading listview

我有一个每秒更新一些TextView的线程。当我长时间处理任何事情时,如果文本视图发生变化,事件将被取消。

我点击列表视图项目,看到高亮半秒,但如果线程更新任何相关的内容,甚至列表视图之外的视图,则longpress不会触发。如果我说得对,我可以在ui更新之间进行长时间的操作,并且可以正常工作。

那么,如果他们可以取消,那么处理longclicks的正确方法是什么?我已经尝试设置一个标志来阻止点击时的ui更新,但ListView和LsitViewItems没有向下和向上事件。

编辑:这里基本上是有问题的代码。用于更新文本视图的线程,以及使用longclick侦听器

的任何其他视图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layMain"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/item_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </ListView>

    <TextView
        android:id="@+id/lblAGFD"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingTop="10dp"
        android:text=""
        android:textColor="#FFF0F0F0"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/lblAGFA"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingTop="10dp"
        android:text=""
        android:textColor="#FFF0F0F0"
        android:textStyle="bold" />
<LinearLayout>




itemList = (ListView) findViewById(R.id.item_list);
itemList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> parent, View view, int pos, long id) {
        showItemDialog(pos, id);
        return true;
    }
});




private class UpdateObserver implements Runnable {
    private AtomicBoolean stop = new AtomicBoolean(false);

    public void stop() {
        stop.set(true);
    }

    @Override
    public void run() {

        Runnable runAGF = new Runnable() {
            @Override
            public void run() {
                lblAGFD.setText(agfService.getAGFD());
                lblAGFA.setText(agfService.getAGFA());
            }

        };

        while (!stop.get()) {

            if ((agfService != null)) {     
                runOnUiThread(runAGF);
            }

            agfService.tick(true, true, false);

            try {Thread.sleep(1000);} 
            catch (Exception e) {}
        }

    }
}

1 个答案:

答案 0 :(得分:0)

我创建了一个后台线程来模拟你的更新线程。它通过向处理程序发送消息来每秒更新TextView。我还将OnScrollListener,OnItemClickListener和OnItemLongClickListener添加到ListView。我就是这样做的。

<强> activtiy_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:id="@+id/layMain"
              android:orientation="vertical"
              tools:context=".MainActivity">

    <TextView
        android:id="@+id/lblAGFD"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/lblAGFA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:textStyle="bold"/>

    <ListView
        android:id="@+id/item_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </ListView>

</LinearLayout>

<强> list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp">
    </TextView>

</LinearLayout>

<强> MainActivity.java

package com.example.testapp;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;

public class MainActivity extends Activity implements AbsListView.OnScrollListener,
        AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
    private static final String TAG = "MainActivity";

    private TextView mLblAGFD;
    private TextView mLblAGFA;
    private ListView mList;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            mLblAGFD.setText("Count " + msg.what);
            mLblAGFA.setText("Count " + msg.what);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLblAGFD = (TextView) findViewById(R.id.lblAGFD);
        mLblAGFA = (TextView) findViewById(R.id.lblAGFA);
        mList = (ListView) findViewById(R.id.item_list);

        mList.setAdapter(new MyListAdapter(50));
        mList.setOnScrollListener(this);
        mList.setOnItemClickListener(this);
        mList.setOnItemLongClickListener(this);

        mUpdateThread.start();
    }

    private Thread mUpdateThread = new Thread() {
        int count = 1;

        @Override
        public void run() {
            while (true) {
                count++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mHandler.sendEmptyMessage(count);
            }
        }
    };

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this, "Clicked on Item" + position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position,
        long id) {
        Toast.makeText(this, "Long clicked on Item" + position, Toast.LENGTH_SHORT)
                .show();
        return true;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
            int totalItemCount) {
        int firstPosition = mList.getFirstVisiblePosition();
        if (firstPosition % 10 == 0) {
            setTitle("Group " + (firstPosition / 10 + 1));
        }
    }
}

<强> MyListAdaper.java

package com.example.testapp;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by JasonCYChueh on 2015/8/11.
 */
public class MyListAdapter extends BaseAdapter {

    private List<String> mData;

    public MyListAdapter(int itemCount) {
        mData = new ArrayList<String>(itemCount);
        for (int i = 0; i < itemCount; i++) {
            mData.add("Item " + (i + 1));
        }
    }

    @Override
    public int getCount() {
        return mData != null ? mData.size() : 0;
    }

    @Override
    public String getItem(int position) {
        return mData != null ? mData.get(position) : "";
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_item, parent, false);
            holder = new ViewHolder(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(getItem(position));
        return convertView;
    }

    private static class ViewHolder {
        TextView text;

        ViewHolder(View view) {
            text = (TextView) view.findViewById(R.id.text);
            view.setTag(this);
        }
    }
}