2017-10-24 6 views
-1

Ich bin sehr neu in Android Apps Entwicklung. Ich möchte Swipe-Code in verschiedenen Webview-Fragment hinzufügen (nicht in MainActivity). Ich habe viele Ideen von Stack-Overflow verfolgt, aber ich kann keine wirkliche Lösung für meine Anforderung bekommen. Ich habe folgende Struktur.hinzufügen wischen innerhalb webview fragment

MainActivity.java

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     new Handler().postDelayed(new Runnable(){ 
     @Override 
      public void run(){ 
       Intent i=new Intent(MainActivity.this,Tabbed.class); 
       startActivity(i); 
       finish(); 

     } 

     },3000); 
    } 
} 

tabyeb.java Ich möchte in diesem Fragment Swipe hinzufügen nur (tabyeb.xml)

public class tabyeb extends Fragment{ 

    public static boolean isNetworkStatusAvialable (Context context) { 
     ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     if (connectivityManager != null) 
     { 
      NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo(); 
      if(netInfos != null) 
       if(netInfos.isConnected()) 
        return true; 
     } 
     return false; 

    } 


    public WebView mWebView; 
    SwipeRefreshLayout swipe; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View v = inflater.inflate(R.layout.tabinfo, container, false); 
     mWebView = ((WebView) v.findViewById(R.id.webview)); 
     mWebView.setWebViewClient(new WebViewClient()); 
     mWebView.setWebChromeClient(new WebChromeClient()); 
     WebSettings settings = mWebView.getSettings(); 
     settings.setSaveFormData(true); 
     settings.setJavaScriptEnabled(true); 
     settings.setSupportZoom(false); 
     settings.setCacheMode(WebSettings.LOAD_NO_CACHE); 
     settings.setDomStorageEnabled(true); 
     settings.setSupportMultipleWindows(false); 
     if (isNetworkStatusAvialable(getActivity().getApplicationContext())) { 
      mWebView.loadUrl("https://stackoverflow.com/"); 
     } else { 
      mWebView.loadUrl("file:///android_asset/error.html"); 
     } 

     return v; 
    } 


} 

tabyeb.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="office.com.np.Tabbed$PlaceholderFragment"> 

    <android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/swipe" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

      <WebView 
       android:id="@+id/webView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:layout_alignParentEnd="true" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentStart="true" 
       android:layout_alignParentTop="true" /> 

    </android.support.v4.widget.SwipeRefreshLayout> 

</RelativeLayout> 
+0

erstellen Was Sie erwerben möchten. Hängen Sie ein Diagramm an. –

Antwort

0

Wenn Sie nur Ihren Splittercode für Fragmentklassen haben möchten, müssen Sie die View-Pager-Klasse innerhalb der Fragmentklasse erweitern. you can refer the official documentation

entweder Sie können auch benutzerdefinierte Swipe-Listener für Fragment ..

public class OnSwipeTouchListener implements OnTouchListener { 

private final GestureDetector gestureDetector; 

public OnSwipeTouchListener(Context ctx) { 
    gestureDetector = new GestureDetector(ctx, new GestureListener()); 
} 

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    return gestureDetector.onTouchEvent(event); 
} 

private final class GestureListener extends SimpleOnGestureListener { 

    private static final int SWIPE_THRESHOLD = 1; 
    private static final int SWIPE_VELOCITY_THRESHOLD = 1; 

    @Override 
    public boolean onDown(MotionEvent e) { 
     return true; 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     boolean result = false; 
     try { 
      float diffY = e2.getY() - e1.getY(); 
      float diffX = e2.getX() - e1.getX(); 
      if (Math.abs(diffX) > Math.abs(diffY)) { 
       if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
        if (diffX > 0) { 
         onSwipeRight(); 
        } else { 
         onSwipeLeft(); 
        } 
       } 
       result = true; 
      } else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { 
       if (diffY > 0) { 
        onSwipeBottom(); 
       } else { 
        onSwipeTop(); 
       } 
      } 
      result = true; 

     } catch (Exception exception) { 
      exception.printStackTrace(); 
     } 
     return result; 
    } 
} 

public void onSwipeRight() { 
} 

public void onSwipeLeft() { 
} 

public void onSwipeTop() { 
} 

public void onSwipeBottom() { 
}} 
+0

Danke @Ashokkumar Adichill – AnandaLC

+0

Haapy zu helfen .. @ AnandaLC –

Verwandte Themen