debugging SliderView 中 的 TextView 显示 文本 和 文本 值 错误 [ 更改 第 ( n + 2 ) 个 textView 后 , 第 n 个 TextView 返回 其 初始 化 值 ]

7kjnsjlb  于 2022-11-14  发布在  其他
关注(0)|答案(1)|浏览(111)

我在SliderView中有一个textView,如下所示
activitity_slider.xml

<androidx.viewpager.widget.ViewPager
        android:id="@+id/slideViewPager"
        android:layout_width="match_parent"

slide_layout.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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_gravity="center"

    android:background="@color/white">
    <LinearLayout
        android:id="@+id/HareketEkranı"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="10dp"
        android:orientation="vertical"
        >

        <androidx.cardview.widget.CardView
            android:id="@+id/hareket"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="10dp"
            app:cardBackgroundColor="@color/white"
            app:cardCornerRadius="10dp">
            <ImageView
                android:id="@+id/HareketResmi"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_marginTop="20dp"
                android:layout_marginBottom="20dp"
                android:contentDescription="Hareket"/>
                //android:src="@drawable/ic_armcircle"
            <com.jjoe64.graphview.GraphView
                android:id="@+id/haraketdatasi"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:weightSum="2">
                    <TextView
                        android:id="@+id/textView"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:gravity="left"
                        android:text="Hareket"
                        android:layout_weight="1"
                        android:textColor="@color/darkTextColor"
                        android:textSize="24sp" />

                    <TextView
                        android:id="@+id/HareketAdi"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:textAlignment="gravity"
                        android:gravity="right"
                        android:text="Armcircle"
                        android:textColor="@color/red"
                        android:layout_weight="1"
                        android:textSize="24sp" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:weightSum="2">

                    <TextView
                        android:id="@+id/TekrarYazisiSlide"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom"
                        android:text="Tekrar"
                        android:layout_weight="1"
                        android:textColor="@color/darkTextColor"
                        android:textSize="24sp" />

                    <TextView
                        android:id="@+id/TekrarSayisiSlide"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom|end"
                        android:text="TekrarSayisi"
                        android:textAlignment="gravity"
                        android:gravity="right"
                        android:layout_weight="1"
                        android:textColor="@color/red"
                        android:textSize="24sp" />
                <TextView
                    android:id="@+id/kesme"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom|end"
                    android:text="/"
                    android:layout_weight="1"
                    android:textColor="@color/black"
                    android:textSize="24sp" />
                    <TextView
                        android:id="@+id/TekrarhedefiSlide"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom|end"
                        android:text="25"
                        android:layout_weight="1"
                        android:textColor="@color/black"
                        android:textSize="24sp" />
            </LinearLayout>
        </androidx.cardview.widget.CardView>
    </LinearLayout>
</RelativeLayout>

更新!

SliderAdapter.java

package gymholix.assistx;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

public class SliderAdapter extends PagerAdapter {

    Context context;
    LayoutInflater layoutInflater;

    public SliderAdapter(Context context){
        this.context = context;
    }

    //Arrays
    public int[] slide_images = {
            R.drawable.ic_armcircle,
            R.drawable.ic_ropejump,
            R.drawable.ic_jumpingjack,
            R.drawable.ic_burpee,
            R.drawable.ic_squat
    };
    public String[] slide_headings = {
            "ArmCircle",
        "RopeJump",
        "JumpingJack",
        "Burpee",
        "Squat"
};
/*public String[] tekrar_sayisi = {
    "0",
    "0",
    "0",
    "0",
    "0"*/
public int[] tekrar_sayisi = {
        1,
        2,
        3,
        4,
        5
};


@Override
public int getCount() {
    return slide_headings.length;
}

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    return view == (RelativeLayout) object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View view =layoutInflater.inflate(R.layout.slide_layout, container, false);

    ImageView slideImageView = (ImageView) view.findViewById(R.id.HareketResmi);
    TextView slideHeading = (TextView) view.findViewById(R.id.HareketAdi);
    TextView slideTekraSayisi = (TextView) view.findViewById(R.id.TekrarSayisiSlide);

    slideImageView.setImageResource((slide_images[position]));
    slideHeading.setText(slide_headings[position]);
    slideTekraSayisi.setText(String.valueOf(tekrar_sayisi[position]));


    container.addView(view);

    return view;
}

@Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {

    }

}

Slider.Java

package gymholix.assistx;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Slider extends AppCompatActivity {

    private LinearLayout mDotLayout;
    private String asd;
    private int asdf;

    TextView DenemeSayiCek;
    int position;
    View view;
    int count;
    View viewFix;
    Context context;
    //Sensors---------------------------------------------------------------------------------------
    private Accelerometer accelerometer;
    private Gyroscope gyroscope;

    public double[] acc={3.00,2.00,1.00};
    public double[] gyr={3.00,2.00,1.00};

    //Sensors---------------------------------------------------------------------------------------
    //Main------------------------------------------------------------------------------------------
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Antreman antreman = new Antreman();
        context = this;
        setContentView(R.layout.activity_slider);

        ViewPager AktifHareket = findViewById(R.id.slideViewPager);
        mDotLayout = findViewById(R.id.dotsLayout);
        SliderAdapter sliderAdapter = new SliderAdapter(this);
        AktifHareket.setAdapter((sliderAdapter));

        addDotsIndicator();

        //Saydir Buton------------------------------------------------------------------------------
        TextView Deneme = findViewById(R.id.deneme);
        TextView Deneme4 = findViewById(R.id.deneme4);
    

    TextView Deneme3 = findViewById(R.id.deneme3);
            TextView Deneme2 = findViewById(R.id.deneme2);
            Button Saydir = findViewById(R.id.SaydirSlide);
            Button Saydir2 = findViewById(R.id.Saydir2Slide);
            Saydir.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                position = AktifHareket.getCurrentItem();
                view = AktifHareket.getChildAt(position);
                count = AktifHareket.indexOfChild(view);
                viewFix = AktifHareket.getChildAt(count);
                DenemeSayiCek = viewFix.findViewById(R.id.TekrarSayisiSlide);
                asd = DenemeSayiCek.getText().toString();
                asdf = Integer.parseInt(asd);
                asdf++;
                DenemeSayiCek.setText(String.valueOf(asdf));
                Deneme.setText(String.valueOf(asd));
                Deneme2.setText(String.valueOf(position));
                Deneme3.setText(String.valueOf(asdf));
                Deneme4.setText(String.valueOf(SliderAdapter.POSITION_NONE));

            }
        });

        //Saydir Buton------------------------------------------------------------------------------
        //ImageButton-------------------------------------------------------------------------------
        ImageView logoImage = findViewById(R.id.logo);
        logoImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                antreman.AntremanSayfasi(context);
            }
        });
        antreman.AntremanSayfasi(context);//Açılışta Sayfayı açsın diye
        //ImageButton-------------------------------------------------------------------------------
        //Sensors-----------------------------------------------------------------------------------

        accelerometer = new Accelerometer(this);
        gyroscope = new Gyroscope(this);
        accelerometer.setListner(new Accelerometer.Listner() {
            @Override
            public void onTranslation(float ax, float ay, float az) {
                setAccValue(ax, ay, az);
                Antreman.GetSensorValues.OnAccelerometerChangeValues = acc;
            }
        });
        gyroscope.setListner(new Gyroscope.Listner() {
            @Override
            public void onRotation(float gx, float gy, float gz) {
                setGyroValue(gx, gy, gz);
                Antreman.GetSensorValues.OnGyroscopeChangeValues = gyr;
                //OnSensorChangeValues.add(1, String.valueOf(gyr) );
            }
        });
/*        Saydir2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });*/
        //Sensors-----------------------------------------------------------------------------------
    }
    //Sensors---------------------------------------------------------------------------------------
    protected void onResume(){
        super.onResume();

        accelerometer.register();
        gyroscope.register();
    }
    protected void onPouse(){
        super.onPause();
        accelerometer.unregister();
        gyroscope.unregister();
    }
    //Sensors---------------------------------------------------------------------------------------
    //SensorsValueGetAndSet-------------------------------------------------------------------------
    public double[] setAccValue(float ac, float bc, float cc){
        this.acc[0] = ac;
        this.acc[1] = bc;
        this.acc[2] = cc;
        return acc;
    }
    public double[] setGyroValue(float qq, float wq, float eq){
        this.gyr[0] = qq;
        this.gyr[1] = wq;
        this.gyr[2] = eq;
        return gyr;
    }
    public void getAccValue(float ac, float bc, float cc){
        ac = (float) acc[0];
        bc = (float) acc[1];
        cc = (float) acc[2];
    }
    public void getGyroValue(float qq, float wq, float eq){
        qq = (float) gyr[0];
        wq = (float) gyr[1];
        eq = (float) gyr[2];
    }
    //SensorsValueGetAndSet-------------------------------------------------------------------------
    public void addDotsIndicator(){
        TextView[] mDots = new TextView[3];
        for(int i = 0; i < mDots.length; i++){
            mDots[i] = new TextView(this);
            mDots[i].setText(Html.fromHtml("&#8226;"));
            mDots[i].setTextSize(35);
            mDots[i].setTextColor(getResources().getColor(R.color.colorPrimaryDark));
            mDotLayout.addView(mDots[i]);
        }
    }
    //----------------------------------------------------------------------------------------------
}

这段代码的奇怪之处在于运行按钮时,第一个寻呼机和第二个寻呼机中的文本值会正确更改,但当它到达第三个寻呼机时,它会在更改值时重置第一个寻呼机显示的值,但getText()方法继承了正确的值,但文本被冻结,无法再更改,之后其他页面的值也无法更改,但getText()方法仍然可以正常工作并获得正确的值。
任何想法都会加快我的调试过程thanx无论如何。

sd2nnvve

sd2nnvve1#

添加此剪切解决了问题,必须为寻呼机定义边框

@Override
protected void onCreate(Bundle savedInstanceState) {
        ...
    int size = sliderAdapter.slide_headings.length;
    AktifHareket.setOffscreenPageLimit(size);

相关问题