2017-07-02 16 views
2

各行に1秒ごとに変化するimageViewがあるリストを作成したいと思います。たとえば、listItem 1には2つのdrawableとlistItem 2の間で変更されるimageViewがあります。 listItem1とは異なるドロアブル。私のコードは、すべてのlistItems imageViewsを同じdrawableに変更します。どのように私は何をしようとすることができますか?画像を変更してlistViewを作成する方法

public class GymWorkoutList extends AppCompatActivity { 

String[] name = {"Push up", "Side Plank"}; 
String[] sets = {"5", "4"}; 
String[] reps = {"12", "10"}; 
int[] img = {}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_gym_workout_list); 

    final ListAdapter gymExerListAdapter = new GymWorkoutListAdapter(this, name, reps, sets, img); 
    ListView gymList2 = (ListView)findViewById(R.id.ListaExerciciosGym2); 
    gymList2.setAdapter(gymExerListAdapter); 

}} 

アダプタ

public class GymWorkoutListAdapter extends ArrayAdapter<String> { 

public static int[] imgArray = {R.drawable.pushup1,R.drawable.pushup2}; 
public static int[] imgArray2 = {R.drawable.sideplank1,R.drawable.sideplank2}; 
int[] img={}; 
String[] sets={}; 
String[] reps={}; 
String[] name={}; 
Context c; 
LayoutInflater inflater; 


public GymWorkoutListAdapter(@NonNull Context context, String[] name, String[] sets, String[] reps, int[] img) { 
    super(context, R.layout.activity_gym_workout_list_adapter,name); 

    this.name=name; 
    this.c=context; 
    this.sets=sets; 
    this.reps=reps; 
    this.img=img; 
} 

public class views 
{ 
    TextView nameText; 
    TextView setsText; 
    TextView repsText; 
    ImageView imgView; 
} 

@NonNull 
@Override 
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { 


    View customView = convertView; 
    if(customView == null){ 
     inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     customView = inflater.inflate(R.layout.activity_gym_workout_list_adapter, parent, false); 
     customView.setMinimumHeight(parent.getHeight()/getCount());} 

    final views holder = new views(); 
    holder.nameText = (TextView) customView.findViewById(R.id.exerText); 
    holder.setsText = (TextView) customView.findViewById(R.id.numSets); 
    holder.repsText = (TextView) customView.findViewById(R.id.numReps); 
    holder.imgView = (ImageView) customView.findViewById(imageView); 


    holder.nameText.setText(name[position]); 
    holder.setsText.setText(sets[position]); 
    holder.repsText.setText(reps[position]); 

    final Handler handler = new Handler(); 
    Runnable runnable = new Runnable() { 
     int i=0; 
     public void run() { 
      holder.imgView.setImageResource(imgArray[i]); 
      i++; 
      if(i>imgArray.length-1) 
      { 
       i=0; 
      } 
      handler.postDelayed(this, 650); 
     } 

    }; 
    handler.postDelayed(runnable, 1000); 

    Runnable runnable2 = new Runnable() { 
     int i = 0; 

     public void run() { 
      holder.imgView.setImageResource(imgArray2[i]); 
      i++; 
      if (i > imgArray2.length - 1) { 
       i = 0; 
      } 
      handler.postDelayed(this, 650); 
     } 
    }; 
    handler.postDelayed(runnable2, 1000); 

    return customView; 
}} 

カスタム行XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.andre.fitness.Workouts.GymWorkoutListAdapter"> 

<ImageView 
    android:id="@+id/imageView" 
    android:layout_width="195dp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="10dp" 
    android:layout_height="97dp" 
    app:srcCompat="@drawable/pushup1" 
    tools:layout_editor_absoluteX="1dp" 
    tools:layout_editor_absoluteY="-2dp" /> 

<TextView 
    android:id="@+id/sets" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Sets:" 
    android:textColor="#000000" 
    android:layout_marginTop="21dp" 
    android:layout_below="@+id/exerText" 
    android:layout_alignLeft="@+id/exerText" 
    android:layout_alignStart="@+id/exerText" /> 

<TextView 
    android:id="@+id/reps" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Reps:" 
    android:textColor="#000000" 
    android:layout_below="@+id/sets" 
    android:layout_alignLeft="@+id/sets" 
    android:layout_alignStart="@+id/sets" 
    android:layout_marginTop="16dp" /> 

<TextView 
    android:id="@+id/numSets" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" 
    android:layout_alignBaseline="@+id/reps" 
    android:layout_alignBottom="@+id/reps" 
    android:layout_alignLeft="@+id/numReps" 
    android:layout_alignStart="@+id/numReps" /> 

<TextView 
    android:id="@+id/numReps" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" 
    android:layout_alignBaseline="@+id/sets" 
    android:layout_alignBottom="@+id/sets" 
    android:layout_toRightOf="@+id/exerText" 
    android:layout_toEndOf="@+id/exerText" /> 

<TextView 
    android:id="@+id/exerText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:text="TextView" 
    android:layout_marginLeft="22dp" 
    android:layout_marginStart="22dp" 
    android:layout_alignTop="@+id/imageView" 
    android:layout_toRightOf="@+id/imageView" 
    android:layout_toEndOf="@+id/imageView" /> 

</RelativeLayout> 

答えて

0

アダプタコンストラクタ

... 
imgView = new ImageView[adapter.size()]; 

の配列としてビューを定義し、Iは画像の異なるソートとアダプタをリセットすることをお勧め。 また、カスタムビューを作成し、カスタムビュークラス内の背景の変更を処理します。 2つのビューセットが必要な場合は、ImageViewの2つのcustomView拡張を作成します。カスタムビューのカスタムリストビューのリスト項目(列のレイアウト)のレイアウトで http://www.vogella.com/tutorials/AndroidCustomViews/article.html

0

ヒント

を作成するための チェックアウトこのチュートリアルを、あなたは、視認性と、あなたのイメージの両方を維持することができます一方はVISIBLE、その他はGONEとなります。これで、イメージを変更するときに、ビューの可視性をプログラムで変更できます。

これは私が話していることです。

カスタム行のあなたのレイアウトは次のようになりことができます。

<RelativeLayout> 
     <ImageView 
     android:visibility="gone" 
     android:id="@+id/img1" 
     android:layout_alignParentLeft="true" /> 

     <ImageView 
      android:id="@+id/img2" 
      android:layout_alignParentRight="true"/> 

</RelativeLayout> 

と可視性を変更する:

public View getView(int position, View convertView, ViewGroup parent) { 
    img2.setVisibility(VIEW.GONE); 
    img1.setVisibility(VIEW.VISABLE); 
} 
関連する問題