【发布时间】:2015-04-27 08:02:06
【问题描述】:
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativeLayoutHelper"
android:focusableInTouchMode="true"
android:focusable="true"
android:clickable="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/LinearForPager"
android:focusableInTouchMode="true"
android:clickable="true"
android:focusable="true">
</LinearLayout>
</RelativeLayout>
在程序中,我在 LinearLayout 中添加对象 ScreenPager,它扩展了 ViewPager 类,我添加了另一个带有 TextViews 的 LinearLayout:
public class ModuleHelpOfGuide extends Activity {
private final String PATH_IMAGE = "helper";
float x1,y1, x2,y2;
ScreenPager screenPagerHelper;
LinearLayout linearLayoutHelper,linearLayoutHorizontalHelper;
RelativeLayout relativeLayoutHelper;
TextView pageTextView [];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_pager_layout);
LayoutInflater.from(this).setFactory(ScreenPager.getShortNameFactory());
screenPagerHelper = new ScreenPager(this);
addInScreen();
relativeLayoutHelper = (RelativeLayout)findViewById(R.id.relativeLayoutHelper);
linearLayoutHelper = (LinearLayout) findViewById(R.id.LinearForPager);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
screenPagerHelper.setLayoutParams(layoutParams);
linearLayoutHelper.addView(screenPagerHelper);
relativeLayoutHelper.setOnTouchListener(touchListenerRelativeHelper);
linearLayoutHorizontalHelper = new LinearLayout(this);
LinearLayout.LayoutParams layoutParamsHorizontal = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
linearLayoutHorizontalHelper.setLayoutParams(layoutParamsHorizontal);
LinearLayout.LayoutParams layoutParamsPageText = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParamsPageText.gravity = Gravity.BOTTOM;
String [] listAssets;
try {
listAssets = getAssets().list(PATH_IMAGE);
int count = listAssets.length;
pageTextView = new TextView [count];
for (int i = 0; i < count; i++) {
pageTextView[i] = createTexViewHelper();
pageTextView[i].setLayoutParams(layoutParamsPageText);
linearLayoutHorizontalHelper.addView(pageTextView[i]);
}
pageTextView[screenPagerHelper.getCurrentItem()].setTextColor(Color.WHITE);
} catch (IOException e) {
e.printStackTrace();
}
relativeLayoutHelper.setOnTouchListener(touchListenerRelativeHelper);
relativeLayoutHelper.addView(linearLayoutHorizontalHelper);
}
private void addInScreen (){
String [] listAssets;
try {
listAssets = getAssets().list(PATH_IMAGE);
if (listAssets.length > 0){
for (String pathFile : listAssets){
InputStream file = getAssets().open(PATH_IMAGE + "/" + pathFile);
Drawable drawable = Drawable.createFromStream(file,null);
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(layoutParams);
imageView.setImageDrawable(drawable);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
screenPagerHelper.addScreen(imageView);
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
private TextView createTexViewHelper (){
TextView textViewHelper = new TextView(getApplicationContext());
textViewHelper.setText(".");
textViewHelper.setTextSize(65);
textViewHelper.setTypeface(null, Typeface.BOLD);
textViewHelper.setTextColor(android.graphics.Color.GRAY);
return textViewHelper;
};
View.OnTouchListener touchListenerRelativeHelper = new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
int current = screenPagerHelper.getCurrentItem();
for (int i = 0; i < pageTextView.length; i++){
pageTextView[i].setTextColor(android.graphics.Color.GRAY);
}
pageTextView[current].setTextColor(getResources().getColor(R.color.white));
return true;
}
if (event.getAction() == MotionEvent.ACTION_UP){
int current = screenPagerHelper.getCurrentItem();
for (int i = 0; i < pageTextView.length; i++){
pageTextView[i].setTextColor(android.graphics.Color.GRAY);
}
pageTextView[current].setTextColor(getResources().getColor(R.color.white));
return false;
}
return true;
}
};
}
TouchEvent 中的问题,如果在相对或线性 Layots 上使用它不起作用,如果我在 ScreenPager 上使用它而不是事件开始工作,但 textView 滞后于 slaider 中的 1 个图像。如果我在 ScreenPager 中使用 TouchEvent 并返回 true,则滑块不起作用。如何在 RelativeLayout 上跟踪事件 touchlistener?
【问题讨论】:
-
你在哪里设置了TouchEvent?
-
relativeLayoutHelper.setOnTouchListener(touchListenerRelativeHelper);
-
在布局xml中添加此代码
android:descendantFocusability="beforeDescendants" -
hmm.. 所以.. 我认为这是触摸事件流问题。 here 参考这里。
-
它并不能解决问题。
标签: android android-layout touch-event