我有19个水平线性布局,垂直排列在另一个线性布局中。他们几乎是空的,并且onclick显示文本。我正在使用
设置布局动画parentOne.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
出于某种原因,前7个布局似乎打开onClick比第8个慢一点。只有一瞬间,但是当我打开它时,我已经让其他人看看它,以确保我不会疯狂。我会发布整个文件,但它是巨大的,超过了这个上允许的最大文本。所以这里是肉和土豆。那么,为什么我的布局开放速度比其他布局慢?数字8-19立即打开onClick和1-7有一个明显的小延迟。
Java On Create
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.tacticalfieldcarestudy);
LinearLayout parentOne = (LinearLayout) findViewById(R.id.TFCLayoutOne);
LinearLayout parentTwo = (LinearLayout) findViewById(R.id.TFCLayoutTwo);
LinearLayout parentTwoBullets = (LinearLayout)findViewById(R.id.TFCLayoutTwoBullets);
LinearLayout parentThree = (LinearLayout)findViewById(R.id.TFCLayoutThree);
LinearLayout parentThreeBullets = (LinearLayout)findViewById(R.id.TFCLayoutThreeBullets);
LinearLayout parentFour = (LinearLayout)findViewById(R.id.TFCLayoutFour);
LinearLayout parentFourBullets = (LinearLayout)findViewById(R.id.TFCLayoutFourBullets);
LinearLayout parentFive = (LinearLayout)findViewById(R.id.TFCLayoutFive);
LinearLayout parentFiveBullets = (LinearLayout)findViewById(R.id.TFCLayoutFiveBullets);
LinearLayout parentSix = (LinearLayout)findViewById(R.id.TFCLayoutSix);
LinearLayout parentSixBullets = (LinearLayout)findViewById(R.id.TFCLayoutSixBullets);
LinearLayout parentSeven = (LinearLayout)findViewById(R.id.TFCLayoutSeven);
LinearLayout parentSevenBullets = (LinearLayout)findViewById(R.id.TFCLayoutSevenBullets);
LinearLayout parentEight = (LinearLayout)findViewById(R.id.TFCLayoutEight);
LinearLayout parentEightBullets = (LinearLayout)findViewById(R.id.TFCLayoutEightBullets);
LinearLayout parentNine = (LinearLayout)findViewById(R.id.TFCLayoutNine);
LinearLayout parentNineBullets = (LinearLayout)findViewById(R.id.TFCLayoutNineBullets);
LinearLayout parentTen = (LinearLayout)findViewById(R.id.TFCLayoutTen);
LinearLayout parentTenBullets = (LinearLayout)findViewById(R.id.TFCLayoutTenBullets);
LinearLayout parentEleven = (LinearLayout)findViewById(R.id.TFCLayoutEleven);
LinearLayout parentTwelve = (LinearLayout)findViewById(R.id.TFCLayoutTwelve);
LinearLayout parentThirteen = (LinearLayout)findViewById(R.id.TFCLayoutThirteen);
LinearLayout parentThirteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutThirteenBullets);
LinearLayout parentFourteen = (LinearLayout)findViewById(R.id.TFCLayoutFourteen);
LinearLayout parentFifteen = (LinearLayout)findViewById(R.id.TFCLayoutFifteen);
LinearLayout parentFifteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutFifteenBullets);
LinearLayout parentSixteen = (LinearLayout)findViewById(R.id.TFCLayoutSixteen);
LinearLayout parentSixteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutSixteenBullets);
LinearLayout parentSeventeen = (LinearLayout)findViewById(R.id.TFCLayoutSeventeen);
LinearLayout parentSeventeenBullets = (LinearLayout)findViewById(R.id.TFCLayoutSeventeenBullets);
LinearLayout parentEighteen = (LinearLayout)findViewById(R.id.TFCLayoutEighteen);
LinearLayout parentEighteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutEighteenBullets);
LinearLayout parentNineteen = (LinearLayout)findViewById(R.id.TFCLayoutNineteen);
LinearLayout parentNineteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutNineteenBullets);
parentOne.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTwo.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTwoBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentThree.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentThreeBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFour.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFourBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFive.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFiveBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSix.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSixBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSeven.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSevenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEight.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEightBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentNine.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentNineBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEleven.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTwelve.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentThirteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentThirteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFourteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFifteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFifteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSixteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSixteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSeventeen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSeventeenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEighteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEighteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentNineteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentNineteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
}
7种Java方法中的一种,在onClick上有不必要的延迟
private boolean mOpenOne = false;
public void toggleOne(View view){
TextView stepOne = (TextView)findViewById(R.id.stepOne);
ImageView toggle = (ImageView)findViewById(R.id.toggleStepOne);
if(mOpenOne){
stepOne.setText("Step 1:");
toggle.setImageResource(R.drawable.open);
}
else{
stepOne.setText("1. Casualties with an altered mental status should be disarmed immediately.");
toggle.setImageResource(R.drawable.close);
}
mOpenOne = !mOpenOne;
}
带有意外延迟的XML
<LinearLayout
android:id="@+id/TFCLayoutOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:animateLayoutChanges="true"
android:onClick="toggleOne"
android:background="#606060"
>
<ImageView
android:id="@+id/toggleStepOne"
android:src="@drawable/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="@+id/stepOne"
android:text="Step 1:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="0dp"
style="@style/Bullet"
/>
</LinearLayout>
Java第8种方法,可以毫不拖延地工作
private boolean mOpenEight = false;
public void toggleEight(View view){
TextView stepEight = (TextView)findViewById(R.id.stepEight);
ImageView toggle = (ImageView)findViewById(R.id.toggleStepEight);
if(mOpenEight){
stepEight.setText("Step 8:");
TextView stepEightBulletOne = (TextView)findViewById(R.id.stepEightBulletOne);
TextView stepEightBulletTwo = (TextView)findViewById(R.id.stepEightBulletTwo);
TextView stepEightBulletThree = (TextView)findViewById(R.id.stepEightBulletThree);
TextView stepEightBulletFour = (TextView)findViewById(R.id.stepEightBulletFour);
TextView stepEightBulletFive = (TextView)findViewById(R.id.stepEightBulletFive);
TextView stepEightBulletSix = (TextView)findViewById(R.id.stepEightBulletSix);
stepEightBulletOne.setText("");
stepEightBulletTwo.setText("");
stepEightBulletThree.setText("");
stepEightBulletFour.setText("");
stepEightBulletFive.setText("");
stepEightBulletSix.setText("");
stepEightBulletOne.getLayoutParams().height = 0;
stepEightBulletTwo.getLayoutParams().height = 0;
stepEightBulletThree.getLayoutParams().height = 0;
stepEightBulletFour.getLayoutParams().height = 0;
stepEightBulletFive.getLayoutParams().height = 0;
stepEightBulletSix.getLayoutParams().height = 0;
toggle.setImageResource(R.drawable.open);
}
else{
stepEight.setText("8. Prevention of hypothermia");
TextView stepEightBulletOne = (TextView)findViewById(R.id.stepEightBulletOne);
TextView stepEightBulletTwo = (TextView)findViewById(R.id.stepEightBulletTwo);
TextView stepEightBulletThree = (TextView)findViewById(R.id.stepEightBulletThree);
TextView stepEightBulletFour = (TextView)findViewById(R.id.stepEightBulletFour);
TextView stepEightBulletFive = (TextView)findViewById(R.id.stepEightBulletFive);
TextView stepEightBulletSix = (TextView)findViewById(R.id.stepEightBulletSix);
stepEightBulletOne.setText("a. Minimize casualty's exposure to the elements. Keep protective gear on or with the casualty if feasible.");
stepEightBulletTwo.setText("b. Replace wet clothing with dry if possible. Get the casualty onto an insulated surface as soon as possible.");
stepEightBulletThree.setText("c. Apply the Ready-Heat Blanket from the Hypothermia Prevention and Management Kit (HPMK) to the casualty's torso (not directly on the skin) and cover the casualty with the Heat-Reflective Shell (HRS).");
stepEightBulletFour.setText("d. If an HRS is not available, the previously recommended combination of the Blizzard Survival Blanket and the Ready Heat blanket may also be used.");
stepEightBulletFive.setText("e. If the items mentioned above are not available, use dry blankets, poncho liners, sleeping bags, or anything that will retain heat and keep the casualty dry.");
stepEightBulletSix.setText("f. Warm fluids are preferred if IV fluids are required.");
stepEightBulletOne.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletTwo.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletThree.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletFour.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletFive.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletSix.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
toggle.setImageResource(R.drawable.close);
}
mOpenEight = !mOpenEight;
}
其中一个布局的XML,没有延迟
<LinearLayout
android:id="@+id/TFCLayoutEightBullets"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#A0A0A0"
android:animateLayoutChanges="true"
android:onClick="toggleEight"
>
<LinearLayout
android:id="@+id/TFCLayoutEight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:animateLayoutChanges="true"
android:background="#A0A0A0"
>
<ImageView
android:id="@+id/toggleStepEight"
android:src="@drawable/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="@+id/stepEight"
android:text="Step 8:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="0dp"
style="@style/Bullet"
/>
</LinearLayout>
<TextView
android:id="@+id/stepEightBulletOne"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="@style/BulletIndented"
/>
<TextView
android:id="@+id/stepEightBulletTwo"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="@style/BulletIndented"
/>
<TextView
android:id="@+id/stepEightBulletThree"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="@style/BulletIndented"
/>
<TextView
android:id="@+id/stepEightBulletFour"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="@style/BulletIndented"
/>
<TextView
android:id="@+id/stepEightBulletFive"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="@style/BulletIndented"
/>
<TextView
android:id="@+id/stepEightBulletSix"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="@style/BulletIndented"
/>
</LinearLayout>
答案 0 :(得分:0)
Well I figured it out. It looks as though it was a processing issue. If I had to guess I would say that each time I called a method it would search through the giant list of linearlayouts in the onCreate method for the animation to open it.
In my onCreate method I define a CRAP TON of linear layouts as such
LinearLayout parentOne = (LinearLayout) findViewById(R.id.TFCLayoutOne);
parentOne.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
The Fix:
I separated ALL of them and placed them all in their respective onClick methods. Now, when I click them they actually open faster than my other pages that are setup similarly, so I will be changing those as well. My java methods were:
private boolean mOpenOne = false;
public void toggleOne(View view){
TextView stepOne = (TextView)findViewById(R.id.stepOne);
ImageView toggle = (ImageView)findViewById(R.id.toggleStepOne);
if(mOpenOne){
stepOne.setText("Step 1:");
toggle.setImageResource(R.drawable.open);
}
else{
stepOne.setText("1. Casualties with an altered mental status should be disarmed immediately.");
toggle.setImageResource(R.drawable.close);
}
mOpenOne = !mOpenOne;
}
I have changed them to:
private boolean mOpenOne = false;
public void toggleOne(View view){
TextView stepOne = (TextView)findViewById(R.id.stepOne);
ImageView toggle = (ImageView)findViewById(R.id.toggleStepOne);
LinearLayout parentOne = (LinearLayout) findViewById(R.id.TFCLayoutOne);
parentOne.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
if(mOpenOne){
stepOne.setText("Step 1:");
toggle.setImageResource(R.drawable.open);
}
else{
stepOne.setText("1. Casualties with an altered mental status should be disarmed immediately.");
toggle.setImageResource(R.drawable.close);
}
mOpenOne = !mOpenOne;
}
Hope this helps others!