在的onClick片段,机器人从来没有所谓的 [英] onClick never called in a fragment, android

查看:148
本文介绍了在的onClick片段,机器人从来没有所谓的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android项目的工作使用动态创建的碎片,我很难抓住用户的事件......

I'm working on an android project using dynamically created fragments, and I struggle to catch the users events...

在这里-S的证其罪code。

here-s the incriminate code.

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    Bundle data = getArguments();
    ArrayList<String> dataList = null;
    if (data != null) {
        dataList = data.getStringArrayList("jsonlist");
    }
    try {
        for(String object: dataList){
            json.add(new JSONObject(object));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }



    //Layout template

    ViewGroup maingroup= (ViewGroup) getActivity().findViewById(R.id.MainLayout);
    mainview = getActivity().getLayoutInflater().inflate(R.layout.finallayout, maingroup, false);
    ViewGroup myLayout= (ViewGroup) maingroup.findViewById(R.id.scrollcontentcontainer);

    for (int i = 0; i < json.size(); i++) {
        View  elementlayout = getActivity().getLayoutInflater().inflate(R.layout.relative, myLayout, false);
        createMainLayout(this.json.get(i), i, elementlayout, myLayout);
        //sur click sur layout

        elementlayout.setOnClickListener(this);
    }
    addShapeAndBottom();
    return mainview;
}

该片段OnClickListener为了赶事件延伸。用户可以点击布局即可。我要补充这些碎片都是动态添加。

The fragment extends OnClickListener in order to catch the events. The user can click on a Layout. I have to add that the fragments are both dynamically added.

public class MainFragment extends Fragment implements View.OnClickListener

这是什么是假设发生的,当我点击的地方:

And this is what is suppose to happen when I click somewhere:

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    switch (v.getId()) {
        case R.id.scrollcontentcontainer:

                    System.out.print("test");
                    Fragment contentFragment = new CoreFragment();
                    FragmentManager fm = getActivity().getFragmentManager();
                    FragmentTransaction fragmentTransaction = fm.beginTransaction();
                    fragmentTransaction.replace(R.id.MainLayout, contentFragment);
                    fragmentTransaction.commit();

            break;

        default:
            System.out.print("test");
            break;
    }
}

问题是,当我试图调试,我还没有进入onClick的功能,这似乎是永远不会被调用。我想声明的听众直接像这样:

The thing is when I'm trying to debug, I'm not even going into the onClick function which seems to be never called. I tried to declare the listener directly like this:

setOnClickListener(public void onClick(View v) {...})

但它不工作为好。我的目的是显示上的布局点击另一个片段, elementlayout ,它涉及几个布局中显示像一个列表。

but it was not working as well. My aim is to display another fragment on a click on a layout, elementlayout, which concerns several layout displayed like a list.

在此先感谢!

修改1

XML code。 fimallayout:

xml code. fimallayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<RelativeLayout
    android:id="@+id/bottomcontent"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">

</RelativeLayout>


<ScrollView
    android:id="@+id/scroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/bottomcontent"
    android:layout_alignParentTop="true">

        <LinearLayout
            android:id="@+id/scrollcontentcontainer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:clickable="true">

    </LinearLayout>
</ScrollView>

和模板布局我想点击它已进入scrollcontentcontainer:

and the template layout I want to click which has to go into the scrollcontentcontainer:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:background="#1d010101"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:duplicateParentState="true">

<GridLayout
    android:id="@+id/toplayout"
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    android:layout_alignParentLeft="false"
    android:layout_alignParentStart="false"
    android:columnCount="4"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <TextView
        android:id="@+id/tp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="0"
        android:text="00c"
                                                                          android:textAppearance="?android:attr/textAppearanceSmall"
        />

    <TextView
        android:id="@+id/localisation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="0"
        android:text="Unknow" />

    <Button
        android:id="@+id/cwp"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_column="3"
        android:layout_row="0" />


</GridLayout>

<View
    android:id="@+id/line"
    android:layout_width="fill_parent"
    android:layout_height="2dp"
    android:layout_below="@+id/toplayout"
    android:layout_marginTop="5dp"
    android:background="#c0c0c0"/>

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/line"
    android:layout_marginTop="10dp"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/leftlayout"
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:orientation="vertical"
        android:paddingLeft="10dp">

        <Button
            android:id="@+id/fa"
            android:layout_width="115dp"
            android:layout_height="77dp"
            android:layout_below="@+id/temperature"
            android:gravity="center|right"
            android:text=""
            android:textAlignment="gravity"/>

        <TextView
            android:id="@+id/hr"
            android:layout_width="115dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Unknow"
                    android:textAppearance="?android:attr/textAppearanceMedium" />



    </LinearLayout>
</LinearLayout>

<LinearLayout
        android:id="@+id/fcontainer"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/line"
        android:layout_centerHorizontal="true"
        android:layout_margin="1dp"
        android:orientation="horizontal">

    </LinearLayout>

    </RelativeLayout>

某些布局将填充内容和 addShapeAndBottom();
添加页脚。

EDIT2

添加监听器 onActivityCreated 不会改变任何东西...

Adding the listener on onActivityCreated doesn't change anything...

推荐答案

好吧,我终于找到了些日子的问题。

Ok, I finally found the problem after some days.

看来,滚动型偷的焦点,使得内外无法布局,以获得用户的点击。

It seems that the ScrollView was stealing the focus, making the layout inside and outside unable to receive the user's click.

将其删除解决这个问题(这是不挂片段,到底...)。

Removing it fix the problem (which was not linked to the fragment, in the end...).

这篇关于在的onClick片段,机器人从来没有所谓的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆