我想制作一个列表,其中每一行都有一个每秒都会更改的imageView,例如listItem 1有一个在两个drawable之间更改的imageView,而listItem 2的imageview在两个不同于listItem1的drawable之间发生变化。我的代码将所有listItems imageViews更改为相同的drawables。我怎样才能实现我的尝试?
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 :(得分:0)
将视图定义为适配器构造函数中的数组
...
imgView = new ImageView[adapter.size()];
我建议使用不同类型的图像重置适配器。 还可以创建自定义视图并处理customView类中的更改背景。 如果你想要2组视图,那么创建2个ImageView的customView扩展。 查看本教程以创建自定义视图 http://www.vogella.com/tutorials/AndroidCustomViews/article.html
答案 1 :(得分: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);
}