簡體   English   中英

OnTouchListener阻止ListView交互

[英]OnTouchListener blocks ListView interaction

我制作的第一個Android布局有點問題。 在我的界面上,我在界面頂部有一個EditText,下面有一個ListView,其中展示了元素列表。 當用戶觸摸ListView時,我希望EditText失去焦點,虛擬鍵盤消失。 我基於此頁面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>

onTouch()返回false 否則,您將告訴系統您已經處理了所有觸摸事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM