【问题标题】:How to make a listView with changing images如何使用更改图像制作 listView
【发布时间】:2017-07-02 09:43:45
【问题描述】:

我想创建一个列表,其中每一行都有一个每秒更改一次的 imageView,例如 listItem 1 有一个在 2 个可绘制对象之间更改的 imageView,而 listItem 2 有一个在与 listItem1 不同的 2 个可绘制对象之间更改的 imageview。我的代码将所有 listItems imageViews 更改为相同的可绘制对象。我怎样才能实现我正在尝试的目标?

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>

【问题讨论】:

    标签: java android listview


    【解决方案1】:

    在适配器构造函数中将视图定义为数组

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

    我建议使用不同类型的图像重置适配器。 还可以在 customView 类中创建自定义视图并处理不断变化的背景。 如果您想要 2 组视图,则创建 ImageView 的 2 个 customView 扩展。 查看本教程以创建自定义视图 http://www.vogella.com/tutorials/AndroidCustomViews/article.html

    【讨论】:

      【解决方案2】:

      提示

      在自定义列表视图的列表项布局(行布局)中,您可以保留两张图片,其中一张为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);
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-01
        • 2016-07-21
        • 2012-04-18
        • 1970-01-01
        • 1970-01-01
        • 2016-10-31
        • 2016-04-22
        • 1970-01-01
        相关资源
        最近更新 更多