情境:
我创建了一个app抽屉并在其中放置了一个搜索栏。此app抽屉由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;
}
}
答案 0 :(得分:0)
setOntouchListener
是一个界面。
你有
final SeekBar sk=(SeekBar) rootView2.findViewById(R.id.seekBar1);
SeekBar.setOnTouchListener(new WorkarroundFixMovementOnDrawer());
应该是
sk.setOnTouchListener(new WorkarroundFixMovementOnDrawer());
因为你有
class WorkarroundFixMovementOnDrawer implements View.OnTouchListener
您的WorkarroundFixMovementOnDrawer
实现了界面View.OnTouchListener
。
答案 1 :(得分:0)
更改
TextView textProgress = (TextView)rootView.findViewById(R.id.TextView01);
到
textProgress = (TextView)rootView.findViewById(R.id.TextView01);
并向您的班级添加一个对象
public class MainActivity extends Fragment {
TextView textProgress;