Я работаю над проектом Android, используя динамически создаваемые фрагменты, и я изо всех сил пытаюсь поймать события пользователей...
вот инкриминируемый код.
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, чтобы перехватывать события. Пользователь может щелкнуть Макет. Я должен добавить, что оба фрагмента добавляются динамически.
public class MainFragment extends Fragment implements View.OnClickListener
И вот что должно произойти, когда я нажму где-нибудь:
@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, которая, кажется, никогда не вызывается. Я попытался объявить слушателя напрямую следующим образом:
setOnClickListener(public void onClick(View v) {...})
но это тоже не работало. Моя цель - отобразить другой фрагмент при нажатии на макет, elementlayout, который касается нескольких макетов, отображаемых в виде списка.
Заранее спасибо !
ИЗМЕНИТЬ 1
xml-код. окончательный макет:
<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>
Некоторые макеты заполняются содержимым и addShapeAndBottom();
add a footer.
ИЗМЕНИТЬ2
Добавление прослушивателя на onActivityCreated
ничего не меняет...