繁体   English   中英

OnClickListner接口不适用于片段中的linearlayout

[英]OnClickListner interface is not working for linearlayout in the fragment

我正在创建一个片段,其中使用了可单击的linearLayout作为按钮,我已经创建了所有必需的方法,并实现了该类的OnClickListener接口,但是当我单击布局时,什么都没有发生。这是xml和java中的LinearLayout和TextView码。

          <LinearLayout
            android:orientation="vertical"
            android:layout_height="match_parent"
            android:layout_width="50dip"
            android:layout_weight="1"
            android:clickable="true"

            android:background="@drawable/button_layout"
            android:layout_margin="1dip">
            <LinearLayout
                android:orientation="vertical"
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="match_parent">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="e"
                    android:textColor="@color/abc_primary_text_disable_only_material_dark"
                    android:gravity="top"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    android:text="π"
                    android:gravity="bottom"/>
            </LinearLayout>

        </LinearLayout>

主视图TextView是

<TextView android:id="@+id/mainview"
            android:layout_width="match_parent"
            android:background="@color/button_material_light"
            android:layout_height="60dip"
            android:layout_weight="1"/>

类代码是

public class HomeFragment extends Fragment implements View.OnClickListener{

TextView mainView;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mainView = (TextView) getActivity().findViewById(R.id.mainview);


}

public static HomeFragment newInstance(int i){
    HomeFragment homeFragment = new HomeFragment();
    Bundle args = new Bundle();
    args.putInt("index", i);
    homeFragment.setArguments(args);
    return homeFragment;
}

public HomeFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_home, container, false);
}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    ((MainActivity) activity).onSectionAttached(getArguments().getInt("index"));
}

@Override
public void onClick(View view) {
    mainView.setText("ghalib");
 }
}

我还尝试调试该程序,发现单击Layout时未调用该方法。

感谢rahul提醒我在用作按钮的Clickable LinearLayout的setOnClickListener中传递“ this”对象

因此,首先我通过android:id =“ @ + id / pie”将ID分配给布局,然后在代码中设置列表器,然后将新代码设置为

public class HomeFragment extends Fragment implements View.OnClickListener{

TextView mainView;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mainView = (TextView) getActivity().findViewById(R.id.mainview);

    //This is the New Part
    ((LinearLayout)getActivity().findViewById(R.id.pie))
    .setOnClickListener(this);

}

public static HomeFragment newInstance(int i){
    HomeFragment homeFragment = new HomeFragment();
    Bundle args = new Bundle();
    args.putInt("index", i);
    homeFragment.setArguments(args);
    return homeFragment;
}

public HomeFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_home, container, false);
}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    ((MainActivity)activity).
    onSectionAttached(getArguments().getInt("index"));
}

@Override
public void onClick(View view) {
    mainView.setText("ghalib");
 }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM