【问题标题】:How to initialize OnClickListener for included layout inside fragment?如何为片段内包含的布局初始化 OnClickListener?
【发布时间】:2017-01-08 17:28:39
【问题描述】:

这是我的应用布局的表示:

我应该在哪里为包含的布局创建onClickListeners?我在片段内部尝试过,但我无法通过findViewById。所以我尝试了 Main Activity 但我不确定如何从那里进入包含的布局。

我也在片段中试过这个:

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() {

}

}

但是 Fragment 似乎是空的

片段 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>

【问题讨论】:

  • 如果您想在片段 xml 中添加该视图,您应该将其包含在片段类中
  • @Charuka 好的,但它应该是什么样子,因为我的例子不正确
  • 没有得到您的要求?你的意思是在代码中?
  • 你红了我的问题吗?您可以看到我尝试创建 onclicklisteners 但它不起作用,那我该怎么办呢?还有为什么是-1分?
  • 我把它弄红了,我问的是你最后的评论,不是你的问题 -1 不是来自我:) “它应该是什么样子,因为我的例子不正确”

标签: android android-layout android-fragments onclicklistener


【解决方案1】:

步骤

  1. 创建要包含的 xml 文件(例如 web.xml)
  2. 在片段的 xml 中使用包含标签并使用 layout="@layout/ 连接您需要包含的布局
  3. 初始化片段内的包含标签(当你这样做时,请确保你的 web.xml 的根标签)
  4. 完成包含标记的初始化后,使用包含标记访问包含 (web.xml) 的布局内的视图

示例

在我的片段中,我的 XML 中有一个 include 标记。它连接到我的 web.xml 和 ..web.xml 的父标签/根标签是 FrameLayout ..

现在看看我是如何初始化我的include 标签..(如果你不使用右根标签来初始化包含,它会因空指针而崩溃 )

我的 web.xml 中有一个名为 g_betta 的 ID(我在片段中包含的 XML 布局) 看看我如何访问该视图..

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>

【讨论】:

    【解决方案2】:

    这样做:

    button_call = myView.findViewById(R.id.btn_call);
    button_call.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
    
                               // write your code here...
                        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多