onClick 从未在片段中调用,android
onClick never called in a fragment, android
我正在使用动态创建的片段开发一个 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>
我想点击的模板布局必须进入 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.
EDIT2
在 onActivityCreated
上添加侦听器不会改变任何内容...
要使布局可点击需要设置可点击 属性 of elementlayout
:
elementlayout.setClickable(true);
也设置
android:duplicateParentState="true"
在 R.layout.relative
中用于所有子视图。
您可以通过 activity 将一个片段替换为另一个片段。看看 link。这是从一个片段到另一个片段的最模块化的方法。
好的,折腾了几天终于找到问题了
好像是ScrollView
抢了焦点,导致内外布局都接收不到用户的点击。
删除它解决了问题(最终没有链接到片段...)。
我正在使用动态创建的片段开发一个 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>
我想点击的模板布局必须进入 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.
EDIT2
在 onActivityCreated
上添加侦听器不会改变任何内容...
要使布局可点击需要设置可点击 属性 of elementlayout
:
elementlayout.setClickable(true);
也设置
android:duplicateParentState="true"
在 R.layout.relative
中用于所有子视图。
您可以通过 activity 将一个片段替换为另一个片段。看看
好的,折腾了几天终于找到问题了
好像是ScrollView
抢了焦点,导致内外布局都接收不到用户的点击。
删除它解决了问题(最终没有链接到片段...)。