我正在创建一个包含滚动视图,按钮面板和一些Textview的布局。 使用可重用视图包含文本视图和按钮面板。 scrollView被编码到活动上。
问题是scrollview阻止OnClickListener工作。我知道它是scrollview,好像我删除它听众工作。我已经阅读了scrollview和OnClickListener之间的冲突,并假设这是造成这种情况的原因。有谁知道这个问题的任何解决方案。
我没有发布我的代码,因为有大量的代码要发布,我正在寻找关于这个问题的一般性评论,或者有任何有经验的人,而不是我特定问题的解决方案。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".BoResults" >
<include layout="@layout/buttonpanel"/>
<include layout="@layout/results"/>
<HorizontalScrollView
android:paddingTop="400dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/include1"
android:layout_alignLeft="@+id/include1">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="100dp"
android:layout_height="60dip"
android:background="@drawable/drawable2"
android:text="Pressure" />
<TextView
android:id="@+id/textView2"
android:layout_width="100dp"
android:layout_height="60dip"
android:background="@drawable/drawable2"
android:text="Solution Gas-Oil Ratio" />
<TextView
android:id="@+id/textView3"
android:layout_width="100dp"
android:layout_height="60dip"
android:background="@drawable/drawable2"
android:text="Oil Density" />
<TextView
android:id="@+id/textView4"
android:layout_width="100dp"
android:layout_height="60dip"
android:background="@drawable/drawable2"
android:text="Oil FVF" />
<TextView
android:id="@+id/textView5"
android:layout_width="100dp"
android:layout_height="60dip"
android:background="@drawable/drawable2"
android:text="Oil Viscosity" />
<TextView
android:id="@+id/textView6"
android:layout_width="100dp"
android:layout_height="60dip"
android:background="@drawable/drawable2"
android:text="Oil Compressibility" />
<TextView
android:id="@+id/textView7"
android:layout_width="100dp"
android:layout_height="60dip"
android:background="@drawable/drawable2"
android:text="Free Gas SG" />
<TextView
android:id="@+id/textView8"
android:layout_width="100dp"
android:layout_height="60dip"
android:background="@drawable/drawable2"
android:text="Free Gas Z-factor" />
<TextView
android:id="@+id/textView9"
android:layout_width="100dp"
android:layout_height="60dip"
android:background="@drawable/drawable2"
android:text="Free Gas FVF" />
<TextView
android:id="@+id/textView10"
android:layout_width="100dp"
android:layout_height="60dip"
android:background="@drawable/drawable2"
android:text="Free Gas Compressibility" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</HorizontalScrollView>
</RelativeLayout>
代码:
public void setup(){
LinearLayout layout = (LinearLayout)findViewById(R.id.buttonLayout);
button1 = (Button) findViewById(R.id.buttonLayout).findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.buttonLayout).findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.buttonLayout).findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.buttonLayout).findViewById(R.id.button4);
applyListener(layout,listener);
Log.d("","run");
}
OnClickListener listener = new OnClickListener() {
public void onClick(View view) {
//conditions();
switch (view.getId()){
case R.id.button1:
Intent intent1=new Intent(ButtonHandler.this, MainActivity.class);
startActivity(intent1);
break;
case R.id.button2:
if(MainActivity.fuel.equals("Black Oil")){
Intent intent2=new Intent(ButtonHandler.this, BoInputs.class);
startActivity(intent2);}
if(MainActivity.fuel.equals("Dry Gas")){
Intent intent2=new Intent(ButtonHandler.this, DgInput.class);
startActivity(intent2);}
break;
case R.id.button3:
if(MainActivity.fuel.equals("Black Oil")){
Intent intent3=new Intent(ButtonHandler.this, BoResults.class);
startActivity(intent3);}
if(MainActivity.fuel.equals("Dry Gas")){
Intent intent3=new Intent(ButtonHandler.this, DgResults.class);
startActivity(intent3);
}
break;
case R.id.button4:
if(MainActivity.fuel.equals("Black Oil")){
Intent intent4=new Intent(ButtonHandler.this, BoPlots.class);
startActivity(intent4);}
if(MainActivity.fuel.equals("Dry Gas")){
Intent intent4=new Intent(ButtonHandler.this, DgPlots.class);
startActivity(intent4);
}
break;
}
}
};
protected static void applyListener(View child, OnClickListener listener) {
if (child == null)
return;
if (child instanceof ViewGroup) {
applyListener((ViewGroup) child, listener);
}
else if (child != null) {
if(child.getId() == R.id.button1) {
child.setOnClickListener(listener);
}
if(child.getId() == R.id.button2) {
child.setOnClickListener(listener);
}
if(child.getId() == R.id.button3) {
child.setOnClickListener(listener);
}
if(child.getId() == R.id.button4) {
child.setOnClickListener(listener);
}
}
}
private static void applyListener(ViewGroup parent, OnClickListener listener) {
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
if (child instanceof ViewGroup) {
applyListener((ViewGroup) child, listener);
} else {
applyListener(child, listener);
}
}
}