我有一个奇怪的问题,我不知道问题出在哪里.
我有一个XML文件,其中包含两个线性布局,根据使用的设置,一次只能看到一个.两个视图都是动态生成的,并设置了一个on click listener和onlongclicklistener.
以下是XML布局:
<?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:orientation="vertical"> <LinearLayout android:id="@+id/resultTableContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:visibility="gone"> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="horizontal" android:fillViewport="true"> <HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:fillViewport="true"> <TableLayout android:id="@+id/resultTable" android:stretchColumns="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:shrinkColumns="false"> </TableLayout> </HorizontalScrollView> </ScrollView> </LinearLayout> <LinearLayout android:id="@+id/formattedResultContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:visibility="gone"> <ScrollView android:id="@+id/formattedScrollContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical"> <LinearLayout android:id="@+id/formattedResult" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"/> </ScrollView> </LinearLayout> </RelativeLayout>
为简单起见,我将调用第一个线性布局容器视图1和第二个线性布局容器视图2.
默认情况下,两个视图的可见性设置都已消失,当屏幕首次加载时,视图1的可见性设置为可见,并使用以下代码动态生成视图:
@Override public void processResult(final View.OnClickListener editRowClickListener,final View.OnLongClickListener columnLongClickListener) { try { this.getActivity().runOnUiThread(new Runnable() { @Override public void run() { if (shouldExistingResultBeCleared()) { resultView.removeAllViews(); } if (getSettings().getInt(Defines.SharedPreferenceSettings.APPLICATION_THEME,com.BoardiesITSolutions.Library.R.style.LibAppTheme) == com.BoardiesITSolutions.Library.R.style.LibAppTheme) { resultView.setBackgroundColor(Color.WHITE); } else { resultView.setBackgroundColor(Color.BLACK); } } }); this.showDialog(getJSONResult().length()); new Thread(new Runnable() { @Override public void run() { try { for (int i = 0; i < getJSONResult().length(); i++) { HashMap<Integer,String> fieldIndexAndValue = new HashMap<Integer,String>(); final TableRow tr; if (!getSettings().getBoolean("IsDarkTheme",false)) { tr = (TableRow) getFragment().getLayoutInflater(getFragment().getArguments()).inflate(R.layout.result_table_row_light_theme,resultView,false); } else { tr = (TableRow) getFragment().getLayoutInflater(getFragment().getArguments()).inflate(R.layout.result_table_row_dark_theme,false); } if (i == 0) { tr.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.appPrimaryColour)); } else if (i % 2 == 0) { if (!getSettings().getBoolean("IsDarkTheme",false)) { tr.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.resultRowLightThemeAlternateRow)); } else { tr.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.resultRowDarkThemeAlternateRow)); } } ImageButton imageButton = (ImageButton) getFragment().getLayoutInflater(getFragment().getArguments()).inflate(R.layout.row_edit_image,tr,false); imageButton.setOnClickListener(editRowClickListener); tr.addView(imageButton); if (i == 0) { imageButton.setVisibility(View.INVISIBLE); } JSONArray array = getJSONResult().getJSONArray(i); String currentField = null; for (int j = 0; j < array.length(); j++) { final TextView textView; textView = (TextView) getFragment().getLayoutInflater(getFragment().getArguments()).inflate(R.layout.result_textview,false); textView.setText(array.getString(j)); if (!getSettings().getBoolean(Defines.SharedPreferenceSettings.MULTILINE_RESULT,true)) { textView.setSingleLine(true); } if (i == 0) { textView.setTypeface(null,Typeface.BOLD); //Get the fields into a index and field hash map addIndexAndField(j,array.getString(j)); } else { textView.setOnLongClickListener(columnLongClickListener); fieldIndexAndValue.put(j,array.getString(j)); } getActivity().runOnUiThread(new Runnable() { @Override public void run() { tr.addView(textView); } }); } imageButton.setTag(fieldIndexAndValue); getActivity().runOnUiThread(new Runnable() { @Override public void run() { resultView.addView(tr); } }); updateProgressDialog(); //handler.sendMessage(handler.obtainMessage()); } closeDialog(); //((ResultProcessor)resultProcessor).closeDialog(); } catch (JSONException ex) { } } }).start(); } catch (Exception ex) { } }
这完全没问题.用户按下选项以切换到视图2,因此视图1被清除,可见性设置为消失,视图2被设置为可见.然后动态生成此视图,并相应地将单击侦听器设置为视图. (我没有包含此视图的代码,因为我认为它不相关,并且此视图始终无例外地工作).
如果用户然后切换回视图1,则清除视图2,并且可见性设置为消失,视图1恢复可见,并且使用与上面所示相同的方法动态地重新创建视图并分配on click侦听器.
视图自我看起来很好,但是没有用户交互可能,滚动视图不再垂直或水平滚动,并且点击处理程序都不起作用.
由于视图1工作一次,我不明白为什么它不会重新生成,但视图2总是有效,无论如何.
解决方法
我已经设法解决了我遇到的问题,感谢大家的帮助,我尝试了所建议但不幸的是没有太大的影响.
我目前所拥有的是一个片段,它必须是线性布局容器,当视图被切换时,一个容器被隐藏并显示容器,然后重新加载相关的视图内容.
我改为将每个视图更改为不同的片段并使用自己的XML布局文件,然后在切换视图时,它使用片段事务将一个片段视图替换为另一个,然后使用相同的代码正如我上面动态填充相关的视图内容.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。