Перестаёт работать OnClickListener Android Java
Всё изначально работает, но когда запускаю анимацию (в отдельном потоке) анимация проигрывается, но после этого на любые клики приложение не реагируют, но на свайпы реагирует. Пробовал запускать анимацию по клику, до запуска клики обрабатываются, после нет.
Код анимации
class AnimateClass extends Thread{
float prog1;
DonutProgress thisProg;
int speed = 30;
AnimateClass(DonutProgress circleProgress){
prog1 = circleProgress.getProgress();
thisProg = circleProgress;
circleProgress.setProgress(0);
}
public void run(){
for(int i = 0;i<=prog1;i++)
{
thisProg.setProgress(i);
thisProg.setText(String.valueOf(i));
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(i>80)
speed = 5;
else if(i>50)
speed = 10;
else if(i>30)
speed = 20;
else if(i>10)
speed = 25;
}
}
}
код tabbed Activity
public class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private PageViewModel pageViewModel;
DonutProgress c1,c2,c3;
public static PlaceholderFragment newInstance(int index) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle bundle = new Bundle();
bundle.putInt(ARG_SECTION_NUMBER, index);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageViewModel = ViewModelProviders.of(this).get(PageViewModel.class);
int index = 1;
if (getArguments() != null) {
index = getArguments().getInt(ARG_SECTION_NUMBER);
}
pageViewModel.setIndex(index);
}
@Override
public View onCreateView(
@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_main, container, false);
final TextView textView = root.findViewById(R.id.section_label);
SharedPreferences sharedPreferences = getContext().getSharedPreferences("myPref",getContext().MODE_PRIVATE);
int index = 1;
if (getArguments() != null) {
index = getArguments().getInt(ARG_SECTION_NUMBER);
}
c1 = root.findViewById(R.id.donut_progress1);
c2 = root.findViewById(R.id.donut_progress2);
c3 = root.findViewById(R.id.donut_progress3);
c1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animate();
}
});
//Animate();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef;
String refIdintificator = String.valueOf(index);
refIdintificator += "_";
refIdintificator += String.valueOf(sharedPreferences.getInt("userZodiac",0));
myRef = database.getReference(refIdintificator);
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Retrieve latest value
String message = dataSnapshot.getValue(String.class);
textView.setText(message);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Error handling
}
});
return root;
}
void Animate(){
AnimateClass animateClass1 = new AnimateClass(c1);
AnimateClass animateClass2 = new AnimateClass(c2);
AnimateClass animateClass3 = new AnimateClass(c3);
animateClass1.start();
animateClass2.start();
animateClass3.start();
}
class AnimateClass extends Thread{
float prog1;
DonutProgress thisProg;
int speed = 30;
AnimateClass(DonutProgress circleProgress){
prog1 = circleProgress.getProgress();
thisProg = circleProgress;
circleProgress.setProgress(0);
}
public void run(){
for(int i = 0;i<=prog1;i++)
{
thisProg.setProgress(i);
thisProg.setText(String.valueOf(i));
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(i>80)
speed = 5;
else if(i>50)
speed = 10;
else if(i>30)
speed = 20;
else if(i>10)
speed = 25;
}
}
}
}
код MainActivity
package com.stenleone.horoscope;
public class MainActivity extends AppCompatActivity {
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
Intent intentBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
sharedPreferences = getSharedPreferences("myPref",MODE_PRIVATE);
editor = sharedPreferences.edit();
intentBack = new Intent(this,StartActivity.class);
int[] logo = new int[]{R.drawable.coserog_logo, R.drawable.vodolei_logo,R.drawable.fish_logo,
R.drawable.oven_logo,R.drawable.telec_logo,R.drawable.brother_logo,R.drawable.rack_logo,
R.drawable.leologo,R.drawable.deva_logo,R.drawable.vecu_logo,R.drawable.scorpio_logo,
R.drawable.strelec_logo};
ImageView horoscope = findViewById(R.id.horoscope_logo);
horoscope.setBackgroundResource(logo[sharedPreferences.getInt("userZodiac",0)]);
}
public void ClickLogo(View v){
editor.putInt("nowDate",2);
editor.commit();
startActivity(intentBack);
finish();
}
}
вёрстка фрагмента
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.PlaceholderFragment">
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/constraintLayout"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1" />
<ScrollView
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp">
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.github.lzyzsd.circleprogress.DonutProgress
app:donut_finished_stroke_width="5dp"
app:donut_unfinished_stroke_width="5dp"
app:donut_circle_starting_degree="90"
android:padding="5dp"
app:donut_finished_color="@color/colorRed"
app:donut_unfinished_color="@color/colorRedLight"
app:donut_progress="20"
android:id="@+id/donut_progress1"
android:layout_width="60dp"
android:layout_height="60dp"
app:donut_text_color="@color/colorRed"
app:donut_text="20" />
<TextView
android:textColor="@color/colorRed"
android:textSize="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="HEALTH" />
</LinearLayout>
<LinearLayout
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.github.lzyzsd.circleprogress.DonutProgress
app:donut_finished_stroke_width="5dp"
app:donut_unfinished_stroke_width="5dp"
app:donut_circle_starting_degree="90"
android:padding="5dp"
app:donut_finished_color="@color/colorRose"
app:donut_unfinished_color="@color/colorRoseLight"
app:donut_progress="40"
android:id="@+id/donut_progress2"
android:layout_width="60dp"
android:layout_height="60dp"
app:donut_text_color="@color/colorRose"
app:donut_text="40"/>
<TextView
android:textColor="@color/colorRose"
android:textSize="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="LOVE" />
</LinearLayout>
<LinearLayout
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.github.lzyzsd.circleprogress.DonutProgress
app:donut_text_color="@color/colorAccent"
app:donut_finished_stroke_width="5dp"
app:donut_unfinished_stroke_width="5dp"
app:donut_unfinished_color="@color/colorAccentLight"
app:donut_circle_starting_degree="90"
app:donut_text="60"
android:padding="5dp"
app:donut_finished_color="@color/colorAccent"
app:donut_progress="60"
android:id="@+id/donut_progress3"
android:layout_width="60dp"
android:layout_height="60dp"/>
<TextView
android:textColor="@color/colorAccent"
android:textSize="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="CAREER" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="simple horoscope text simple horoscope text simple horoscope text
simple horoscope text simple horoscope text simple horoscope text simple horoscope text simple
horoscope text simple horoscope text" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Compatibility"
android:textColor="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/rack_logo" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="zodiac" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/vecu_logo" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="zodiac" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/telec_logo" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="zodiac" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="0dp"
android:text="Colors"
android:textColor="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="10dp"
android:background="@drawable/shape_button" />
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="10dp"
android:background="@drawable/shape_button" />
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="10dp"
android:background="@drawable/shape_button" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="0dp"
android:text="Lucky Numbers"
android:textColor="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="10dp"
android:background="@drawable/shape_no_lay"
android:text="34" />
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="10dp"
android:background="@drawable/shape_no_lay"
android:text="23" />
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="10dp"
android:background="@drawable/shape_no_lay"
android:text="2" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</ScrollView>
Ответы (1 шт):
Автор решения: Stanley
→ Ссылка
вместо всех танцев в отдельном потоке с бубном использовал аниматор в главном потоке и всё работает на ура, если кому нужен код:
ObjectAnimator imageViewObjectAnimator = ObjectAnimator.ofFloat(c1 ,
"progress", 0f, c1.getProgress());
imageViewObjectAnimator.setDuration(1000); // miliseconds
imageViewObjectAnimator.start();