C'est ma mise en page:OnTouchEvent ne fonctionne pas dans RelatiVeLayout
<?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>
Dans programm i ajouter objet ScreenPager dans LinearLayout, il étend la classe ViewPager, et j'ajouter un autre LinearLayout avec TextViews:
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;
}
};
}
Problème dans TouchEvent, il ne fonctionne pas si utilisé sur des Layots Relatifs ou Linear, si je l'utilise sur ScreenPager que l'événement commence à travailler, mais textView lag sur 1 image dans slaider. Si j'utilise TouchEvent dans ScreenPager et renvoyer true, le curseur ne fonctionne pas. Comment suivre l'événement touchlistener sur RelativeLayout?
où vous définissez TouchEvent? –
relativeLayoutHelper.setOnTouchListener (touchListenerRelativeHelper); –
dans la mise en page xml ajouter ce code 'android: descendantFocusability =" beforeDescendants "' –