2017-03-07 54 views
1

如何爲僅包含片段的框架創建onTouchListener?我不需要膨脹一個activity_main文件,因爲在調用這個片段時它已經顯示出來了。如果我嘗試膨脹框架本身,它會拋出「預期的類型佈局資源」錯誤。在ontouchlistener框架內製作視圖

片段將在用戶觸摸按鈕時激活,片段應在框架中打開。該框架還需要能夠監聽觸摸事件。

這是我的片段和onCreateView的片段看起來像現在:

public class TeacherFragment extends Fragment { 

public final String TAG = getClass().getSimpleName(); 

public void onAttach(Activity myActivity){ 

    Log.v(TAG, "in TeacherFragment - onAttach, activity is: " + myActivity); 
    super.onAttach(myActivity); 
} 

/** 
* This method will only be called once when the retained 
* Fragment is first created. 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Retain this fragment across configuration changes. 
    setRetainInstance(true); 
} 

/** 
* This method will be called only when 
* Fragment is attached and ready to display the view. 
*/ 
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle SavedInstanceState){ 

    if(container == null) { 
     Log.v(TAG, "container is null. No need to inflate."); 
     return null; 
    } 

    View v = container.findViewById(R.id.assignment_view); 
    v.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      Log.d(TAG, "onShortPress: " + event.toString()); 
      Toast.makeText(getView().getContext(), "Testing Short click", Toast.LENGTH_SHORT).show(); 
      return true; 
     } 

     public void onLongPress(MotionEvent event) { 
      Log.d(TAG, "onLongPress: " + event.toString()); 
      Toast.makeText(getView().getContext(), "Testing Long click", Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    return v; 
} //end onCreateView 

}

這是我activity_main。我正在嘗試僅爲此片段使用assignment_view框架。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/main_layout" 
android:baselineAligned="false"> 

<FrameLayout 
    android:id="@+id/main_menu_frame" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:background="#FF0000" 
    android:layout_weight=".3"> 
</FrameLayout> 

<FrameLayout 
    android:layout_width="8dp" 
    android:layout_height="match_parent" 
    android:background="#000000"> 
</FrameLayout> 

<FrameLayout 
    android:id="@+id/assignment_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#00BBFF" 
    android:layout_weight="8"> 
</FrameLayout> 

</LinearLayout> 
+0

那麼問題是什麼?您嘗試設置觸摸偵聽器,但它不起作用或什麼? – azizbekian

+0

我更新了我的帖子。 –

+0

你可以將你的代碼發佈到你開始片段的位置嗎? – Gary99

回答

0

您應該只對文件使用inflate()。一個在「佈局」文件夾中。這是錯誤信息告訴你的。無需花費大量的時間在看這個,你可能想這

View v = findViewById(R.id.assignment_view); 

至於膨脹你的「activity_main.xml中」的文件,它可能會更好這裏膨脹比依靠什麼在調用活動虛增。

+0

我應該提到,對不起!當我嘗試查找viewbyid時,它返回爲「無法解析方法」。我導入了android視圖類,所以我不知道我缺少什麼。 –

+1

嘗試'container.findViewById ...' – Gary99

+0

這給了我一個穩定的構建,所以它在正確的方向!但是onTouch事件並未解僱。有什麼想法嗎? –