2017-01-08 20 views
-3

これは私のアプリのレイアウトの表現です:
私は付属のレイアウトにonClickListenersを作成する必要があります
layoutフラグメント内に含まれるレイアウトのOnClickListenerを初期化するにはどうすればよいですか?

?私は断片の中で試したが、私はfindViewByIdで解決できなかった。だから私はメインアクティビティから試しましたが、そこからレイアウトを含む方法を知りません。

私はまた、フラグメント内でこれを試してみました:

public class MainMenu extends Fragment implements View.OnClickListener{ 

View button_call; 

@Override 
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) { 
    View myView = inflater.inflate(R.layout.fragment_main_menu, container, false); 
    button_call = myView.findViewById(R.id.btn_call); 
    button_call.setOnClickListener(this); 
    return myView; 
} 

@Override 
public void onClick(View v) { 
    // implements your things 
} 


public MainMenu() { 

} 

} 

しかし、その後フラグメントは空

フラグメントXMLのようだ:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<LinearLayout 
android:id="@+id/mainmenu_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:paddingRight="60dp"> 

<include android:id="@+id/btn_call" 
    layout="@layout/call_button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="30dp" 
    android:paddingBottom="30dp" 
    android:layout_marginTop="20dp" 
    android:layout_marginBottom="5dp"/> 

<include android:id="@+id/button2" 
    layout="@layout/message_button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="30dp" 
    android:paddingBottom="30dp" 
    android:layout_marginTop="5dp" 
    android:layout_marginBottom="5dp" /> 

<include android:id="@+id/button3" 
    layout="@layout/navigate_button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="30dp" 
    android:paddingBottom="30dp" 
    android:layout_marginTop="5dp" 
    android:layout_marginBottom="5dp"/> 

<include android:id="@+id/button4" 
    layout="@layout/remind_button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="30dp" 
    android:paddingBottom="30dp" 
    android:layout_marginTop="5dp" 
    android:layout_marginBottom="5dp"/> 


</LinearLayout> 

</ScrollView> 
+0

それはフラグメントクラス –

+0

@Charuka Okですが、私の例が正しくないためにどのように見えるのでしょうか – Somachr

+0

あなたが求めているものは得られませんでしたか?コードの意味ですか? –

答えて

1

ステップ

  1. 含めるXMLファイルを作成します(例:。あなたはこれがあなたのweb.xmlのルートタグについて確認して行うときのweb.xml)
  2. 使用フラグメントのXMLでタグを含めると、あなたはlayout="@layout/
  3. 初期化を使用して含める必要があります。レイアウトを接続するには)(フラグメント内のタグを含める
  4. 一度初期化
  5. は、タグが使用して含まれるレイアウト(web.xmlファイル)内のアクセスビューを行われているが含ま私はを持っているタグ

にここに私のフラグメントで

が含まれますタグがXMLにあります。あなたが右ルートタグを使用しない場合(..

は今、私は私のincludeタグを初期化する方法を見て...それは私のweb.xmlに接続し、..web.xmlの親タグ/ルートタグはFrameLayoutです初期化は

...それは NULLポインタ)私は私が私のフラグメントに含まれ(XMLレイアウト) が、私はそのビューにアクセスする方法を見て、私のweb.xmlの g_bettaと呼ばれるIDを持っている

でクラッシュします、含ま

public class FragmentAbout extends Fragment { 

private RelativeLayout relativeLayoutConnectedInsideIncludeTag; 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.my_fragment,container,false); 


     FrameLayout view =(FrameLayout) rootView.findViewById(R.id.include_tag); 
     relativeLayoutConnectedInsideIncludeTag = (RelativeLayout) view.findViewById(R.id.g_betta); 

     relativeLayoutConnectedInsideIncludeTag.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(getActivity(), "Yes I Found you", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     return rootView; 
    } 


} 

私のフラグメントのxml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<LinearLayout 
    android:id="@+id/mainmenu_layout" 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    android:orientation="vertical"> 

    <include 
     android:id="@+id/include_tag" 
     layout="@layout/web" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 


</ScrollView> 

には、私の - > web.xmlの

<?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:background="@color/maroon" 
    android:gravity="center_horizontal"> 


    <ScrollView 

     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <RelativeLayout 
      android:id="@+id/g_betta" 
      android:gravity="center" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_alignParentBottom="true" 
      android:orientation="vertical"> 

      <TextView 
       android:id="@+id/tv" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       android:text="HELLO" 
       android:textColor="#FFF" 
       android:textSize="20dp" /> 

      <RelativeLayout 
       android:id="@+id/lin" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center"> 

       <Button 
        android:id="@+id/button" 
        android:layout_width="wrap_content" 
        android:layout_height="50dp" 

        android:layout_alignParentBottom="true" 
        android:layout_marginBottom="16dp" 
        android:background="@color/colorPrimaryDark" 
        android:gravity="bottom" 
        android:paddingLeft="24dp" 
        android:paddingRight="24dp" /> 
      </RelativeLayout> 

      <ImageView 
       android:id="@+id/imageone" 
       android:layout_width="match_parent" 
       android:layout_height="250dp" 
       android:layout_below="@+id/lin" 
       android:layout_margin="16dp" 
       android:background="@drawable/girl" 
       android:scaleType="fitXY" /> 

      <ImageView 
       android:id="@+id/imagetwo" 
       android:layout_width="match_parent" 
       android:layout_height="250dp" 
       android:layout_below="@+id/imageone" 
       android:layout_margin="16dp" 
       android:background="@drawable/amanda" 
       android:scaleType="fitXY" /> 


     </RelativeLayout> 

    </ScrollView> 

</FrameLayout> 
1

は次のように行います:あなたは、フラグメントXMLにして、そのビューを追加したい場合は、含まれている必要があり

button_call = myView.findViewById(R.id.btn_call); 
button_call.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 

          // write your code here... 
        }); 
関連する問題