在 android 中单击具有相应数据的不同字母时如何刷新水平布局
How to refresh horizontal layout when click different letter with respective data in android
注意:我想我的问题是:如果第一次,我点击任何字母,它会在水平视图中加载数据,但第二次,如果我点击下一个字母,尝试添加数据以前视图中的项目,所以我得到
java.lang.IllegalStateException: HorizontalScrollView can host only one direct child
at android.widget.HorizontalScrollView.addView(HorizontalScrollView.java:213)
我尝试了很多次,但每次点击不同的字母时,都无法删除旧数据视图和新数据视图。
我在 textview 中有 A 到 Z 字母,就像一个标签。
A B C D........X Y Z.
horizontalview with no data if no select
如果我点击或select A,那么。
在水平视图中显示相关数据。有圆形背景。
A B C.......X Y Z
Apple Aeroplane Argo
5 kg 100 9 and many more
如果我点击或select D,那么。
在水平视图中显示相关数据。有圆形背景。
A B C.......X Y Z
Duck Dog
5 kg 12 and many more
但是无法显示这样的时间,点击D时如果出现错误
01-12 16:21:19.031: E/AndroidRuntime(16246): java.lang.IllegalStateException: HorizontalScrollView can host only one direct child
01-12 16:21:19.031: E/AndroidRuntime(16246): at android.widget.HorizontalScrollView.addView(HorizontalScrollView.java:213)
以及我在我的代码中做了什么,请你给点建议。
for (int j = 0; j < letters.length; j++) {
btnTag = new PlanTextView(mContext);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
btnTag.setText(letters[j].toString());
btnTag.setPadding(6, 6, 6, 6);
btnTag.setTextSize(20);
btnTag.setId(j + 1);
listBtnTag.add(btnTag);
final int index = j;
btnTag.setOnClickListener(new OnClickListener() {
private boolean stateChanged = true;
private boolean flag;
private LinearLayout yelloDetailsLinearLayout1;
private LinearLayout innerYello1;
@SuppressLint("NewApi")
public void onClick(View v) {
yelloDetailsLinearLayout1 = new LinearLayout(mContext);
// letterLinear.setPadding(5, 5, 5, 5);
yelloDetailsLinearLayout1.setOrientation(LinearLayout.HORIZONTAL);
yelloDetailsLinearLayout1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
innerYello1 = new LinearLayout(mContext);
// letterLinear.setPadding(5, 5, 5, 5);
innerYello1.setOrientation(LinearLayout.HORIZONTAL);
innerYello1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// displayYello(btnTag.getText().toString());
List<String> sorted = new ArrayList<String>();
sorted = SortedList(listBtnTag.get(index).getText()
.toString(), yellonameList);
if (sorted.size() == 0) {
Toast.makeText(mContext, "No Yello Page Found",
Toast.LENGTH_SHORT).show();
} else {
for (int i = 0; i < sorted.size(); i++) {
LinearLayout rowletter1 = new LinearLayout(mContext);
rowletter1.setOrientation(LinearLayout.VERTICAL);
rowletter1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
rowletter1.setPadding(10, 0, 0, 0);
tvde = new TextView(mContext);
tvde.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
btnNumber = new Button(mContext);
btnNumber.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
String[] spits = sorted.get(i).toString().split("\,");
tvde.setText("Your Selected=" + spits[0].toString());
btnNumber.setText(spits[1].toString());
rowletter1.addView(tvde);
rowletter1.addView(btnNumber);
innerYello1.addView(rowletter1);
flag=true;
}
yelloDetailsLinearLayout1.addView(innerYello1);
horizontalScrollview.addView(yelloDetailsLinearLayout1);
}
for (int i = 0; i < listBtnTag.size(); i++) {
if (v.getId() == index) {
// reset background to default;
listBtnTag.get(index).setBackgroundResource(
R.drawable.circel_off);
} else {
listBtnTag.get(index).setBackgroundResource(
R.drawable.circel_on);
}
stateChanged = !stateChanged;
}
}
});
rowletter.addView(btnTag);
}
您想实现的功能可以通过更简单的方式实现。我的解决方案假设上面的按钮数量始终是 A 到 Z,并且您必须显示的数据始终具有相同的格式。
而不是 subclassing LinearLayout
,而是像这样创建一个 xml 文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:id="@+id/horizontal_scrollview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:alignParentTop="true" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/a_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a" /> <!-- declare a as A in string.xml -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b" />
<!-- do this till Z -->
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/horizontal_scrollview"
android:layout_alignParentBottom="true">
<!-- your layout to show data here. Showing a sample value for reference -->
<TextView
android:id="@+id/ursel_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/your_selected"/> <!-- declare your_selected as Your selected in string.xml -->
</RelativeLayout>
</RelativeLayout>
现在在这个 Fragment/Activity 的 class 中转到 onCreate 方法
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
Button aButton = (Button) this.findViewById(R.id.a_button);
final TextView urSelTextView = (TextView) this.findViewById(R.id.ursel_textview);
//set the Alphabets click listeners to set the values of data fields accordingly
aButton.setOnClickListener(new View.OnClickListener(){
@Override onClick(View v) {
//do things to set data's layout to show data relevant for A button
urSelTextView.setText("set A's data here.");
}
});
}
创建 26 个这样的侦听器,并根据单击的按钮为 urSelTextView 设置适当的数据。您还可以让 Activity 实现 OnClickListener
并将所有代码写在一个由 activity 实现的公共 onClick
函数中。我还没有测试过这段代码。它可能需要一些改进。但总的来说,你应该从中得到灵感。
EDIT:以下是根据您的要求修改后的代码。我没试过,但我觉得应该可以
在您的 class 中将 yelloDetailsLinearLayout1 存储为成员变量
public class YourClass extends Activity {
private LinearLayout mYelloDetailsLinearLayout; //stores the currently added layout
//later in your code
for (int j = 0; j < letters.length; j++) {
btnTag = new PlanTextView(mContext);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
btnTag.setText(letters[j].toString());
btnTag.setPadding(6, 6, 6, 6);
btnTag.setTextSize(20);
btnTag.setId(j + 1);
listBtnTag.add(btnTag);
final int index = j;
btnTag.setOnClickListener(new OnClickListener() {
private boolean stateChanged = true;
private boolean flag;
private LinearLayout yelloDetailsLinearLayout1;
private LinearLayout innerYello1;
@SuppressLint("NewApi")
public void onClick(View v) {
yelloDetailsLinearLayout1 = new LinearLayout(mContext);
// letterLinear.setPadding(5, 5, 5, 5);
yelloDetailsLinearLayout1.setOrientation(LinearLayout.HORIZONTAL);
yelloDetailsLinearLayout1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
innerYello1 = new LinearLayout(mContext);
// letterLinear.setPadding(5, 5, 5, 5);
innerYello1.setOrientation(LinearLayout.HORIZONTAL);
innerYello1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// displayYello(btnTag.getText().toString());
List<String> sorted = new ArrayList<String>();
sorted = SortedList(listBtnTag.get(index).getText()
.toString(), yellonameList);
if (sorted.size() == 0) {
Toast.makeText(mContext, "No Yello Page Found",
Toast.LENGTH_SHORT).show();
} else {
for (int i = 0; i < sorted.size(); i++) {
LinearLayout rowletter1 = new LinearLayout(mContext);
rowletter1.setOrientation(LinearLayout.VERTICAL);
rowletter1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
rowletter1.setPadding(10, 0, 0, 0);
tvde = new TextView(mContext);
tvde.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
btnNumber = new Button(mContext);
btnNumber.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
String[] spits = sorted.get(i).toString().split("\,");
tvde.setText("Your Selected=" + spits[0].toString());
btnNumber.setText(spits[1].toString());
rowletter1.addView(tvde);
rowletter1.addView(btnNumber);
innerYello1.addView(rowletter1);
flag=true;
}
yelloDetailsLinearLayout1.addView(innerYello1);
/*********** remove the previously added mYelloDetailsLinearLayout here******/
horizontalScrollView.removeView(mYelloDetailsLinearLayout);
/*********** add the newly created mYelloDetailsLinearLayout here******/
mYelloDetailsLinearLayout = yelloDetailsLinearLayout1;
horizontalScrollview.addView(mYelloDetailsLinearLayout);
}
for (int i = 0; i < listBtnTag.size(); i++) {
if (v.getId() == index) {
// reset background to default;
listBtnTag.get(index).setBackgroundResource(
R.drawable.circel_off);
} else {
listBtnTag.get(index).setBackgroundResource(
R.drawable.circel_on);
}
stateChanged = !stateChanged;
}
}
});
rowletter.addView(btnTag);
}
在此解决方案中,您基本上用新的子布局替换了旧的子布局。因此,HorizontalScrollView 将始终只有一个子视图,您不会收到任何错误。但我仍然建议尽可能多地依赖布局文件,因为它更容易且维护成本低。我还没有在我的系统上尝试过 运行 这个。不过我觉得应该可以。
注意:我想我的问题是:如果第一次,我点击任何字母,它会在水平视图中加载数据,但第二次,如果我点击下一个字母,尝试添加数据以前视图中的项目,所以我得到
java.lang.IllegalStateException: HorizontalScrollView can host only one direct child
at android.widget.HorizontalScrollView.addView(HorizontalScrollView.java:213)
我尝试了很多次,但每次点击不同的字母时,都无法删除旧数据视图和新数据视图。
我在 textview 中有 A 到 Z 字母,就像一个标签。
A B C D........X Y Z.
horizontalview with no data if no select
如果我点击或select A,那么。 在水平视图中显示相关数据。有圆形背景。
A B C.......X Y Z
Apple Aeroplane Argo
5 kg 100 9 and many more
如果我点击或select D,那么。 在水平视图中显示相关数据。有圆形背景。
A B C.......X Y Z
Duck Dog
5 kg 12 and many more
但是无法显示这样的时间,点击D时如果出现错误
01-12 16:21:19.031: E/AndroidRuntime(16246): java.lang.IllegalStateException: HorizontalScrollView can host only one direct child
01-12 16:21:19.031: E/AndroidRuntime(16246): at android.widget.HorizontalScrollView.addView(HorizontalScrollView.java:213)
以及我在我的代码中做了什么,请你给点建议。
for (int j = 0; j < letters.length; j++) {
btnTag = new PlanTextView(mContext);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
btnTag.setText(letters[j].toString());
btnTag.setPadding(6, 6, 6, 6);
btnTag.setTextSize(20);
btnTag.setId(j + 1);
listBtnTag.add(btnTag);
final int index = j;
btnTag.setOnClickListener(new OnClickListener() {
private boolean stateChanged = true;
private boolean flag;
private LinearLayout yelloDetailsLinearLayout1;
private LinearLayout innerYello1;
@SuppressLint("NewApi")
public void onClick(View v) {
yelloDetailsLinearLayout1 = new LinearLayout(mContext);
// letterLinear.setPadding(5, 5, 5, 5);
yelloDetailsLinearLayout1.setOrientation(LinearLayout.HORIZONTAL);
yelloDetailsLinearLayout1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
innerYello1 = new LinearLayout(mContext);
// letterLinear.setPadding(5, 5, 5, 5);
innerYello1.setOrientation(LinearLayout.HORIZONTAL);
innerYello1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// displayYello(btnTag.getText().toString());
List<String> sorted = new ArrayList<String>();
sorted = SortedList(listBtnTag.get(index).getText()
.toString(), yellonameList);
if (sorted.size() == 0) {
Toast.makeText(mContext, "No Yello Page Found",
Toast.LENGTH_SHORT).show();
} else {
for (int i = 0; i < sorted.size(); i++) {
LinearLayout rowletter1 = new LinearLayout(mContext);
rowletter1.setOrientation(LinearLayout.VERTICAL);
rowletter1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
rowletter1.setPadding(10, 0, 0, 0);
tvde = new TextView(mContext);
tvde.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
btnNumber = new Button(mContext);
btnNumber.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
String[] spits = sorted.get(i).toString().split("\,");
tvde.setText("Your Selected=" + spits[0].toString());
btnNumber.setText(spits[1].toString());
rowletter1.addView(tvde);
rowletter1.addView(btnNumber);
innerYello1.addView(rowletter1);
flag=true;
}
yelloDetailsLinearLayout1.addView(innerYello1);
horizontalScrollview.addView(yelloDetailsLinearLayout1);
}
for (int i = 0; i < listBtnTag.size(); i++) {
if (v.getId() == index) {
// reset background to default;
listBtnTag.get(index).setBackgroundResource(
R.drawable.circel_off);
} else {
listBtnTag.get(index).setBackgroundResource(
R.drawable.circel_on);
}
stateChanged = !stateChanged;
}
}
});
rowletter.addView(btnTag);
}
您想实现的功能可以通过更简单的方式实现。我的解决方案假设上面的按钮数量始终是 A 到 Z,并且您必须显示的数据始终具有相同的格式。
而不是 subclassing LinearLayout
,而是像这样创建一个 xml 文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:id="@+id/horizontal_scrollview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:alignParentTop="true" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/a_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a" /> <!-- declare a as A in string.xml -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b" />
<!-- do this till Z -->
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/horizontal_scrollview"
android:layout_alignParentBottom="true">
<!-- your layout to show data here. Showing a sample value for reference -->
<TextView
android:id="@+id/ursel_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/your_selected"/> <!-- declare your_selected as Your selected in string.xml -->
</RelativeLayout>
</RelativeLayout>
现在在这个 Fragment/Activity 的 class 中转到 onCreate 方法
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
Button aButton = (Button) this.findViewById(R.id.a_button);
final TextView urSelTextView = (TextView) this.findViewById(R.id.ursel_textview);
//set the Alphabets click listeners to set the values of data fields accordingly
aButton.setOnClickListener(new View.OnClickListener(){
@Override onClick(View v) {
//do things to set data's layout to show data relevant for A button
urSelTextView.setText("set A's data here.");
}
});
}
创建 26 个这样的侦听器,并根据单击的按钮为 urSelTextView 设置适当的数据。您还可以让 Activity 实现 OnClickListener
并将所有代码写在一个由 activity 实现的公共 onClick
函数中。我还没有测试过这段代码。它可能需要一些改进。但总的来说,你应该从中得到灵感。
EDIT:以下是根据您的要求修改后的代码。我没试过,但我觉得应该可以
在您的 class 中将 yelloDetailsLinearLayout1 存储为成员变量
public class YourClass extends Activity {
private LinearLayout mYelloDetailsLinearLayout; //stores the currently added layout
//later in your code
for (int j = 0; j < letters.length; j++) {
btnTag = new PlanTextView(mContext);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
btnTag.setText(letters[j].toString());
btnTag.setPadding(6, 6, 6, 6);
btnTag.setTextSize(20);
btnTag.setId(j + 1);
listBtnTag.add(btnTag);
final int index = j;
btnTag.setOnClickListener(new OnClickListener() {
private boolean stateChanged = true;
private boolean flag;
private LinearLayout yelloDetailsLinearLayout1;
private LinearLayout innerYello1;
@SuppressLint("NewApi")
public void onClick(View v) {
yelloDetailsLinearLayout1 = new LinearLayout(mContext);
// letterLinear.setPadding(5, 5, 5, 5);
yelloDetailsLinearLayout1.setOrientation(LinearLayout.HORIZONTAL);
yelloDetailsLinearLayout1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
innerYello1 = new LinearLayout(mContext);
// letterLinear.setPadding(5, 5, 5, 5);
innerYello1.setOrientation(LinearLayout.HORIZONTAL);
innerYello1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// displayYello(btnTag.getText().toString());
List<String> sorted = new ArrayList<String>();
sorted = SortedList(listBtnTag.get(index).getText()
.toString(), yellonameList);
if (sorted.size() == 0) {
Toast.makeText(mContext, "No Yello Page Found",
Toast.LENGTH_SHORT).show();
} else {
for (int i = 0; i < sorted.size(); i++) {
LinearLayout rowletter1 = new LinearLayout(mContext);
rowletter1.setOrientation(LinearLayout.VERTICAL);
rowletter1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
rowletter1.setPadding(10, 0, 0, 0);
tvde = new TextView(mContext);
tvde.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
btnNumber = new Button(mContext);
btnNumber.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
String[] spits = sorted.get(i).toString().split("\,");
tvde.setText("Your Selected=" + spits[0].toString());
btnNumber.setText(spits[1].toString());
rowletter1.addView(tvde);
rowletter1.addView(btnNumber);
innerYello1.addView(rowletter1);
flag=true;
}
yelloDetailsLinearLayout1.addView(innerYello1);
/*********** remove the previously added mYelloDetailsLinearLayout here******/
horizontalScrollView.removeView(mYelloDetailsLinearLayout);
/*********** add the newly created mYelloDetailsLinearLayout here******/
mYelloDetailsLinearLayout = yelloDetailsLinearLayout1;
horizontalScrollview.addView(mYelloDetailsLinearLayout);
}
for (int i = 0; i < listBtnTag.size(); i++) {
if (v.getId() == index) {
// reset background to default;
listBtnTag.get(index).setBackgroundResource(
R.drawable.circel_off);
} else {
listBtnTag.get(index).setBackgroundResource(
R.drawable.circel_on);
}
stateChanged = !stateChanged;
}
}
});
rowletter.addView(btnTag);
}
在此解决方案中,您基本上用新的子布局替换了旧的子布局。因此,HorizontalScrollView 将始终只有一个子视图,您不会收到任何错误。但我仍然建议尽可能多地依赖布局文件,因为它更容易且维护成本低。我还没有在我的系统上尝试过 运行 这个。不过我觉得应该可以。