2017-03-25 6 views
-1

Ich hatte alle Antworten von der anderen Frage mit dem gleichen Problem gesucht und implementiert, aber in meinem Fall vergeblich. Was ich erreichen möchte, ist, den Inhalt der Textansichten in meinem Fragment zu ändern, wenn ich nach links oder rechts wische. Hier ist mein Code, den ich aus anderen Fragen hier in SO umgesetzt habe.Gesten auf Fragment funktioniert nicht

@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 

    View contentView = inflater.inflate(R.layout.lesson_fragment, container, false); 

    gDetector = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener(){ 

     @Override 
     public boolean onDown(MotionEvent e) { 
      Log.d("Down", "Down triggered!"); 
      return super.onDown(e); 
     } 

     @Override 
     public boolean onFling(MotionEvent start, MotionEvent finish, float velocityX, float velocityY) { 

      Log.d("Fling", "Motion detected!"); 

      // swipe left 
      if(start.getX() - finish.getX() > 50){ 

       if(pos != 0){ 
        pos --; 
        showLesson(); 
       } 
       else{ 
        Toast.makeText(getContext(), "You've reached the first lesson.", Toast.LENGTH_SHORT).show(); 
       } 
      } 
      else if(finish.getX() - start.getX() > 50){ 

       if(pos != LessonListFragment.lessonsList.size()){ 
        pos ++; 
        showLesson(); 
       } 
       else{ 
        Toast.makeText(getContext(), "You've finished all the lessons.", Toast.LENGTH_SHORT).show(); 
       } 
      } 

      return super.onFling(start, finish, velocityX, velocityY); 
     } 
    }); 

    contentView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 
      return gDetector.onTouchEvent(motionEvent); 
     } 
    }); 

    pos = getArguments().getInt("index"); 

    topic = (TextView) contentView.findViewById(R.id.lesson_frag_topic); 
    topic_des = (TextView) contentView.findViewById(R.id.lesson_frag_desc); 
    syntax = (TextView) contentView.findViewById(R.id.lesson_frag_syntax); 
    example = (TextView) contentView.findViewById(R.id.lesson_frag_example); 

    try_example = (Button) contentView.findViewById(R.id.lesson_try); 
    try_example.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      // Show practice activity. 
     } 
    }); 

    lesson_content_container = (FrameLayout) contentView.findViewById(R.id.lesson_content_container); 

    tip_msg = (TextView) contentView.findViewById(R.id.tip_msg); 
    tip_msg.setText(R.string.lesson_content_tip); 

    close_tip = (ImageButton) contentView.findViewById(R.id.close_tip); 
    close_tip.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      lesson_content_container.removeViewAt(1); 
     } 
    }); 

    showLesson(); 

    return contentView; 
} 

und hier ist die XML-Datei

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/lesson_content_container"> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:padding="@dimen/min_padding"> 

     <TextView 
      android:id="@+id/lesson_frag_topic" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="@dimen/min_padding" 
      android:textColor="@color/colorPrimary"/> 

     <TextView 
      android:id="@+id/lesson_frag_desc" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/span_top" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="@dimen/min_padding" 
      android:text="@string/syntax" 
      android:textColor="@color/colorPrimary" /> 

     <TextView 
      android:id="@+id/lesson_frag_syntax" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="@dimen/min_padding"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/example" 
      android:textColor="@color/colorPrimary" 
      android:padding="@dimen/min_padding"/> 

     <TextView 
      android:id="@+id/lesson_frag_example" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/lesson_frag_syntax" 
      android:padding="@dimen/min_padding"/> 

     <Button 
      style="?android:attr/textViewStyle" 
      android:id="@+id/lesson_try" 
      android:layout_width="wrap_content" 
      android:layout_height="@dimen/min_btn_height" 
      android:layout_gravity="end" 
      android:text="Try It!" 
      android:background="@color/colorPrimary"/> 

    </LinearLayout> 
</ScrollView> 

<include 
    layout="@layout/tip_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

</FrameLayout> 
+0

Veröffentlichen Sie die ganze 'onCreateView()', hat es derzeit nicht schließende Klammer. Was gibst du zurück? – azizbekian

+0

'Was ich erreichen will ist, den Inhalt des Fragments zu wechseln, wenn ich nach links oder rechts wische." Welchen Schritt genau versagt ihr? – azizbekian

+0

Die Wischgesten schießen überhaupt nicht, wie Sie in meinen Codes sehen können, habe ich ein paar 'Log.d() 'in der onDown und onFling Methode hinzugefügt, aber mein logcat zeigt diese nicht an, also nehme ich an, dass die Geste nicht' t überhaupt ausgelöst. – iamLinker

Antwort

0

Die Geste Ereignis, das ich mit der Arbeit nicht umsetzen, weil ich eine Scrollview als erstes Kind des framelayout hatte. Meine Theorie ist, dass die Scrollview selbst ihr eigenes gestrure Event implementiert (was ich denke, dass sie die implementierte Geste abfängt) oder vielleicht hätte ich onTouchEvent nicht auf meinem Framelayout, sondern auf meinem Scrollview gesetzt. Hoffe, jemand könnte dies für zukünftige Referenz klären: D

Verwandte Themen