Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

onClick never called in a fragment, android

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

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;
}

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;
    }
}

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) {...})

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.

Thanks in advance !

EDIT 1

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>

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>

Some layouts are filled will contents and addShapeAndBottom(); add a footer.

EDIT2

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To make layout click-able need to set click-able property ofelementlayout :

elementlayout.setClickable(true);

Also set

android:duplicateParentState="true"

in R.layout.relative for all child Views.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...