DrawerLayout可防止EditText上的触摸事件

时间:2014-03-16 12:27:36

标签: android drawerlayout

我有一个DrawerLayout,里面有一个EditText和一个ListView。     问题是,我无法触摸我的EditText或ListView项目。而不是它    DrawerLayout接收所有触摸事件。单击DrawerLayout中的任何项目会导致DrawerLayout关闭(我猜这是drawerlayout类中的默认onTouch方法)

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background" >
    <-- my main layouts here -->
    </FrameLayout>

    <LinearLayout
        android:id="@+id/left_linear"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:clickable="true"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:clickable="true"
            android:orientation="horizontal" >

            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/noborder"
                android:clickable="true"
                android:hint="search box 1"
                android:padding="8dp"
                android:singleLine="true" />

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:padding="5dp"
                android:src="@android:drawable/ic_menu_search" />
        </LinearLayout>

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#111"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

5 个答案:

答案 0 :(得分:3)

在你的oncreate中尝试drawerlayout.requestDisallowInterceptTouchEvent(true)

答案 1 :(得分:1)

将编辑文本和文本视图显式设置为可点击..然后为列表视图实现类似的内容

    /******************************Listener for Drawer***************/
private class DrawerItemClickListener implements ListView.OnItemClickListener
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        //your code here based on posotion

    }   
}

你可以使用类似的东西设置监听器

     DrawerList.setOnItemClickListener(new DrawerItemClickListener());

答案 2 :(得分:1)

我做过类似的事情,

我已更新您的代码。

edittext现在可以重点关注

创建main.xml

<android.support.v4.widget.DrawerLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <FrameLayout
                android:background="@drawable/background"
                android:id="@+id/main_content"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <-- my main layouts here -->
    </FrameLayout>

  <FrameLayout
        android:id="@+id/navigation_frame"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start" />

    </android.support.v4.widget.DrawerLayout>

创建navigation.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/left_linear"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:clickable="true"
        android:layout_gravity="start">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:clickable="true"
            android:orientation="horizontal">
            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:clickable="true"
                android:background="@drawable/noborder"
                android:hint="search box 1"
                android:padding="8dp"
                android:singleLine="true" />

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:padding="5dp"
                android:src="@android:drawable/ic_menu_search" />
        </LinearLayout>

            <ListView android:id="@+id/left_drawer"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:choiceMode="singleChoice"
                android:divider="@android:color/transparent"
                android:dividerHeight="0dp"
                android:background="#111"/>
            </LinearLayout>

//创建活动,如

public class HomeActivity extends actionbaractivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // add menu to navigation drawer
        addMenu();
    }


    private void addMenu()
    {

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();
        fragmentTransaction.replace(R.id.navigation_frame,
                new NavigationFragment());
        fragmentTransaction.commit();
    }
}

//创建新的片段类,将菜单添加到抽屉布局

public class NavigationFragment extends Fragment 
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        View view=inflater.inflate(R.layout.navigation_view, null);
               return view;
    }
}

//为我工作..

答案 3 :(得分:1)

我发现了一个类似的问题,我有更新sdk和特别的额外文件夹,它工作正常。

我测试了你的代码,它在我的设备上正常运行。

谢谢,我希望它有所帮助。

答案 4 :(得分:0)

试试这个。

package com.example.drawersample;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private String[] drawerListViewItems;
    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private ActionBarDrawerToggle actionBarDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // get list items from strings.xml
        drawerListViewItems = getResources().getStringArray(R.array.items);
        // get ListView defined in activity_main.xml
        drawerListView = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        drawerListView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawerlist, drawerListViewItems));

        // 2. App Icon
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        // 2.1 create ActionBarDrawerToggle
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
        drawerLayout, /* DrawerLayout object */
        R.drawable.ic_launcher, /* nav drawer icon to replace 'Up' caret */
        R.string.drawer_open, /* "open drawer" description */
        R.string.drawer_close /* "close drawer" description */
        );

        // 2.2 Set actionBarDrawerToggle as the DrawerListener
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        // 2.3 enable and show "up" arrow
        getActionBar().setDisplayHomeAsUpEnabled(true);

        // just styling option
        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);

        drawerListView.setOnItemClickListener(new DrawerItemClickListener());
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        actionBarDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns
        // true
        // then it has handled the app icon touch event

        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position,
                long id) {
            Toast.makeText(MainActivity.this, ((TextView) view).getText(),
                    Toast.LENGTH_LONG).show();
            drawerLayout.closeDrawer(drawerListView);

        }
    }
}

drawerlist.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:textSize="20sp"
    android:gravity="center_vertical"
    android:paddingStart="14.5sp"
    android:paddingEnd="14.5sp"
    android:minHeight="35sp"

/>

main.xml


<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="#666"
        android:dividerHeight="1dp"
        android:background="#333"
        android:paddingLeft="15sp"
        android:paddingRight="15sp"
        />

</android.support.v4.widget.DrawerLayout>



strings.xml



<resources>

    <string name="app_name">DrawerSample</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

    <string-array name="items">
        <item>Item 1</item>
        <item>Item 2</item>
        <item>Item 3</item>
        <item>Item 4</item>
        <item>Item 5</item>
        <item>Item 6</item>
    </string-array>

    <string name="drawer_open">Open navigation drawer</string>
    <string name="drawer_close">Close navigation drawer</string>
</resources>

Try to add your Edittext and textview of the layout in below lines

    android:focusableInTouchMode="true"
    android:focusable="true"
  or 
 oncreate methos you use requestfocus(your element id);

thank you.