设想:
我创建了一个应用程序抽屉并在其中放置了一个搜索栏。此应用程序抽屉由 app_drawer.xml 组成,而我下面的片段由 main_activity.xml 组成。 我遇到的问题是,尽管搜索栏完美显示并且应用程序抽屉按应有的方式拉入和拉出,但当我拖动它时没有任何反应(我的意思是 textview 没有更新)。即使它应该。
我被告知以下是一个修复,但我不断收到错误:
无法从 View 类型对非静态方法 setOnTouchListener(View.OnTouchListener) 进行静态引用
如何摆脱错误?
使用建议的代码修复:
public class MainActivity extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main_activity, container, false);
View rootView2 = inflater.inflate(R.layout.app_drawer, container, false);
final SeekBar sk=(SeekBar) rootView2.findViewById(R.id.seekBar1);
SeekBar.setOnTouchListener(new WorkarroundFixMovementOnDrawer());
TextView textProgress = (TextView)rootView.findViewById(R.id.TextView01);
sk.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar bar, int progress,
boolean fromUser) {
textProgress.setText("Progress: "+ String.valueOf(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
return rootView;
}
}
/**
* This is just a workarround for make seekbar work on drawer
*/
static class WorkarroundFixMovementOnDrawer implements View.OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
DrawerLayout parentDrawer = getParentDrawer(v);
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow Drawer to intercept touch events.
if (parentDrawer != null) {
parentDrawer.requestDisallowInterceptTouchEvent(true);
}
break;
case MotionEvent.ACTION_UP:
// Allow Drawer to intercept touch events.
if (parentDrawer != null) {
parentDrawer.requestDisallowInterceptTouchEvent(false);
}
break;
}
// Handle seekbar touch events.
v.onTouchEvent(event);
return true;
}
/**
* Try to get DrawerLayout from parent
* @param view view to search
* @return the drawerLayout parent
*/
public static DrawerLayout getParentDrawer(View view) {
if (view != null) {
ViewParent recursiveView = view.getParent();
while (!(recursiveView instanceof DrawerLayout)) {
if (recursiveView == null) {
return null;
}
recursiveView = recursiveView.getParent();
}
return (DrawerLayout) recursiveView;
}
return null;
}
}
原始代码:
public class MainActivity extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main_activity, container, false);
View rootView2 = inflater.inflate(R.layout.app_drawer, container, false);
final SeekBar sk=(SeekBar) rootView2.findViewById(R.id.seekBar1);
TextView textProgress = (TextView)rootView.findViewById(R.id.TextView01);
sk.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar bar, int progress,
boolean fromUser) {
textProgress.setText("Progress: "+ String.valueOf(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
return rootView;
}
}
建议修复:
seekBar.setOnTouchListener(new WorkarroundFixMovementOnDrawer());
/**
* This is just a workarround for make seekbar work on drawer
*/
static class WorkarroundFixMovementOnDrawer implements View.OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
DrawerLayout parentDrawer = getParentDrawer(v);
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow Drawer to intercept touch events.
if (parentDrawer != null) {
parentDrawer.requestDisallowInterceptTouchEvent(true);
}
break;
case MotionEvent.ACTION_UP:
// Allow Drawer to intercept touch events.
if (parentDrawer != null) {
parentDrawer.requestDisallowInterceptTouchEvent(false);
}
break;
}
// Handle seekbar touch events.
v.onTouchEvent(event);
return true;
}
/**
* Try to get DrawerLayout from parent
* @param view view to search
* @return the drawerLayout parent
*/
public static DrawerLayout getParentDrawer(View view) {
if (view != null) {
ViewParent recursiveView = view.getParent();
while (!(recursiveView instanceof DrawerLayout)) {
if (recursiveView == null) {
return null;
}
recursiveView = recursiveView.getParent();
}
return (DrawerLayout) recursiveView;
}
return null;
}
}