2017-10-24 4 views
-1

나는 안드로이드 애플 리케이션 개발에 매우 ​​새로운입니다. 나는 다른 webview 조각 (MainAcitivity에서 아닙니다)에 강타 코드를 추가하고 싶다. stack-overflow에서 많은 아이디어를 따라 왔지만 내 요구 사항에 대한 실제 솔루션을 얻을 수 없습니다. 나는 다음과 같은 구조를 가지고있다.webview 조각 내에서 스 와이프 추가

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 나는이 조각 만 (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

을 조각에 대한 사용자 정의 슬쩍 리스너를 만들 수 있습니다. 다이어그램을 첨부하십시오. –

답변

0

조각 클래스 스 와이프 코드 만 원한다면 조각 클래스 내에서보기 호출기 클래스를 확장해야합니다. you can refer the official documentation

중 당신은 또한 당신이 취득 할 무엇 ...

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

감사합니다 @Ashokkumar Adichill – AnandaLC

+0

도움이 Haapy .. @ AnandaLC –