OnTouchListener阻止ListView交互

时间:2013-07-05 01:28:29

标签: java android android-listview

我在制作第一个Android版面时遇到了一些问题。在我的界面上,我的界面顶部有一个EditText,下面有一个ListView,展示了一个元素列表。我希望EditText失去焦点,当用户触摸ListView时,虚拟键盘会消失。我基于此页http://developer.android.com/guide/topics/ui/ui-events.html

问题是,当我的代码执行此操作时,它还会阻止与ListView本身进行任何其他形式的交互(无法在ListView中导航或选择元素)。有没有办法重新建立默认行为以及我添加的行为?

这是我的MainActivity.java:

public class MainActivity extends Activity {

    // Variable declarations
    EditText ed;
    ListView lv;

    // Function called when the application starts
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Elements declaration
        ed = (EditText) findViewById(R.id.search_text);
        lv = (ListView) findViewById(R.id.media_list);

        // Events declaration
        lv.setOnTouchListener(listViewListener);

        // We create a task to load the media list in a different thread.
        DownloadMediaList task = (DownloadMediaList) new DownloadMediaList (MainActivity.this,new TheInterface() {
            @Override
            public void theMethod(ArrayList<Media> result) {
                lv.setAdapter(new MediathequeListAdapter(MainActivity.this, result));
           }  
        }).execute();


    }

    private OnTouchListener listViewListener = new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent motion){
            ed.clearFocus();
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(ed.getWindowToken(), 0);
            return true;
        }

    };

    // Interface to communicate with the async load.
    public interface TheInterface {
        public void theMethod(ArrayList<Media> result);

         }

}

编辑:如果有帮助,这是我的主要布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true" >

    <LinearLayout android:id="@+id/container_search"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp">

        <EditText android:id="@+id/search_text"
            android:inputType="text"
            android:hint="@string/search_bar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.80" />

        <Button android:id="@+id/search_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.20" />

    </LinearLayout>

    <ListView
        android:id="@+id/media_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="1dp" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

false中返回onTouch()。否则,您告诉系统您已经处理了所有触摸事件。