我想在导航抽屉中使用搜索栏
基本上我需要向左和向右滑动来设置搜索栏,而且,你猜对了,它会滑动导航抽屉......
我想要做的是在搜索栏聚焦时滑动搜索栏,而在不聚焦时滑动导航条。
我该怎么做?
这是设置项目时的代码(我还没有设置搜索栏)
private View onCritereView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.fragment_critere, container, false);
LinearLayout mLinearLayout = (LinearLayout) mView.findViewById(R.id.critere_linearlayout);
View mViewElement = inflater.inflate(R.layout.item_critere, null);
((TextView)mViewElement.findViewById(R.id.critere_title)).setText("Prix Maximum");
mLinearLayout.addView(mViewElement);
return mView;
}
以下是item_critere.xml
和搜索栏:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/critere_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Large Text"
android:id="@+id/critere_title"
android:layout_gravity="left|center_vertical"
android:layout_column="0"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/critere_qualifier"
android:layout_column="1"
android:layout_span="4"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Medium Text"
android:id="@+id/critere_value"
android:layout_span="4"/>
</TableRow>
</TableLayout>
提前致谢:)
答案 0 :(得分:31)
只需添加onTouchListener
即可。当您触摸搜索栏上的屏幕(action_down)时,禁止父级拦截该事件。
seekbar.setOnTouchListener(new ListView.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
int action = event.getAction();
switch (action)
{
case MotionEvent.ACTION_DOWN:
// Disallow Drawer to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow Drawer to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
// Handle seekbar touch events.
v.onTouchEvent(event);
return true;
}
});
答案 1 :(得分:0)
使用DrawerLayout.setDrawerLockMode
代替ViewParent.requestDisallowInterceptTouchEvent
。
@dumazy答案是解决此问题的正确方法,但会导致另一个问题。
在导航抽屉上滚动时,如果触摸SeekBar,则会触发OnSeekBarChangeListener
。因此,滚动事件将中断,并且通过移动手指,搜索栏进度将开始发生变化。
我使用DrawerLockMode
修改了原始答案以解决此问题:
try {
mSeekBar.setOnTouchListener(new SeekBar.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow Drawer to intercept touch events.
// v.getParent().requestDisallowInterceptTouchEvent(true);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
break;
case MotionEvent.ACTION_UP:
// Allow Drawer to intercept touch events.
// v.getParent().requestDisallowInterceptTouchEvent(false);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
break;
}
// Handle SeekBar touch events.
v.onTouchEvent(event);
return true;
}
});
} catch (Throwable e) {
e.printStackTrace();
}