使用 ViewHolder 的 Android 列表视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24865169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:52:27  来源:igfitidea点击:

Android listview using ViewHolder

androidandroid-listviewandroid-arrayadapterandroid-viewholder

提问by Mark

I have a problem. I'm attempting to change an icon in my listview after it has been clicked. It works correctly although are not modified only the clicked icons but also those who are not displayed. For example if I click the icon in the first item of the listview, also the fifth icon changes. This behaviour is repeated for all the following items (every five items of the listview). This is my getView method:

我有个问题。单击后,我试图更改列表视图中的图标。尽管不仅修改了单击的图标,还修改了未显示的图标,但它可以正常工作。例如,如果我单击列表视图第一项中的图标,第五个图标也会更改。对于以下所有项目(列表视图的每五个项目)重复此行为。这是我的 getView 方法:

   public class AlphabeticalAdapter extends ArrayAdapter<String>
   {
       int layoutResourceId; 
       private final Context context;
       private List<String> data;
       private ProgressDialog mProgressDialog;
       private ImageView downloadImageButton;


       public AlphabeticalAdapter(Context context, int resource, List<String> data){
           super(context, resource, data);
           this.layoutResourceId = resource;
           this.context = context;
           this.data = data;    
       }

       public View getView(int position, View convertView, ViewGroup parent) {

          // View rowView = convertView;
           final ViewHolder viewHolder;

           if (convertView == null) {

               LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


               convertView  = inflater.inflate(R.layout.catalogslist_single_row, parent, false);

           viewHolder = new ViewHolder();

           viewHolder.catlogTitle=(TextView)convertView.findViewById(R.id.txtTitle);
           viewHolder.icon=(ImageView)convertView.findViewById(R.id.imageView2); 
           viewHolder.downloadImageButton=(ImageView)convertView.findViewById(R.id.downloadImageButton);

           //downloadImageButton = (ImageView)rowView.findViewById(R.id.downloadImageButton);

           viewHolder.position = position;


           viewHolder.downloadImageButton.setOnClickListener(new OnClickListener() {
               @Override  
               public void onClick(View v) {
                     System.out.println("DOWNLOAD PRESSED");

                     viewHolder.downloadImageButton = (ImageView)v.findViewById(R.id.downloadImageButton);
                     viewHolder.downloadImageButton.setImageResource(R.drawable.icon_ok);
                     viewHolder.downloadImageButton.setTag("downloaded");
                     //rowView.setTag("downloaded");


                 }
             });



           convertView.setTag(viewHolder);

           }

           else{
               viewHolder= (ViewHolder)convertView.getTag(); 
           }

           viewHolder.catlogTitle.setText(data.get(position));
           viewHolder.catlogTitle.setTypeface(regularDin);
           viewHolder.icon.setImageResource(R.drawable.cata);


           if(viewHolder.downloadImageButton.getTag() == "downloaded"){
             downloadImageButton = (ImageView)convertView.findViewById(R.id.downloadImageButton);
             downloadImageButton.setImageResource(R.drawable.icon_ok);
           }
           else{
               downloadImageButton = (ImageView)convertView.findViewById(R.id.downloadImageButton);
                 downloadImageButton.setImageResource(R.drawable.icon_download);
           }


           viewHolder.position = position;

        return convertView;

       } //close getView 

...

...

And this is my ViewHolder class:

这是我的 ViewHolder 类:

      static class ViewHolder{
       ImageView downloadImageButton;
       TextView catlogTitle;
       ImageView icon;
       int position;
   }

回答by Divyang Metalia

Change your code at below. I think you're missing that.

在下面更改您的代码。我认为你错过了这一点。

public class AlphabeticalAdapter extends ArrayAdapter<String> {
    int layoutResourceId;
    private final Context context;
    private List<String> data;
    private List<String> tags;
    private ProgressDialog mProgressDialog;
    private ImageView downloadImageButton;

    public AlphabeticalAdapter(Context context, int resource, List<String> data) {
        super(context, resource, data);
        this.layoutResourceId = resource;
        this.context = context;
        this.data = data;
        tags = new ArrayList<String>();
        int size = data.size();
        for (int i = 0; i < size; i++) {
            tags.add("tag");
        }
    }

    static class ViewHolder {
        ImageView downloadImageButton;
        TextView catlogTitle;
        ImageView icon;
        int position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

        // View rowView = convertView;
        final ViewHolder viewHolder;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // convertView = inflater.inflate(R.layout.catalogslist_single_row,
            // parent, false);
            viewHolder = new ViewHolder();
            viewHolder.position = position;
            viewHolder.downloadImageButton
                    .setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            System.out.println("DOWNLOAD PRESSED");
                            viewHolder.downloadImageButton.setTag("downloaded");
                            tags.add(position, "downloaded");
                        }
                    });
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.catlogTitle.setText(data.get(position));
        viewHolder.catlogTitle.setTypeface(regularDin);
        viewHolder.icon.setImageResource(R.drawable.cata);

        if (tags.get(position) == "downloaded") {
            downloadImageButton.setImageResource(R.drawable.icon_ok);
        } else {
            downloadImageButton.setImageResource(R.drawable.icon_download);
        }

        viewHolder.position = position;
        return convertView;
    } // close getView
}

回答by kupsef

There are as many convertViewsas many row visible in the same time in your ListView(the system reuses it). So you actually have 5 convertView, and because of that you have 5 ImageViewfor the icons. The problem is that you use those ImageView's tag to store the "downloaded" information. That is 5 state, and that is why you see every fifth row downloaded while you scroll in the list.

convertViews在您的ListView(系统重用它)中同时有尽可能多的行可见。所以你实际上有 5 convertView,因此你有 5 个ImageView图标。问题是你使用那些 ImageView 的标签来存储“下载”的信息。那是 5 个状态,这就是为什么当您在列表中滚动时,您会看到每第五行下载一次。

I guess now you see that it won't work. You need to store the downloaded state for every item, so you have to change the underlying List<String>to List<ListItem>, where ListItemcan store the downloaded state for the actual row.

我想现在你看到它行不通了。您需要存储每个项目的下载状态,因此您必须将底层更改List<String>List<ListItem>,其中ListItem可以存储实际行的下载状态。

After that, all you have to do is to update the convertView's ImageView(in getView()) to show the correct icon.

之后,您所要做的就是更新convertView's ImageView(in getView()) 以显示正确的图标。

回答by kupsef

You can try this

你可以试试这个

public class CustomArrayAdapter extends ArrayAdapter {

// declare your custom list with type;
private List<YourModelClass> allData = new ArrayList<YourModelClass>();

public CustomArrayAdapter(@NonNull Context context, List<YourModelClass> allData) {
    super(context, R.layout.your_layout, allData); // add your_layout.xml
    this.allData = allData;
}

class ViewHolder {
    TextView name, phone; // declare your your_layout.xml view type
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewHolder holder = new ViewHolder();
    if (convertView == null) {

        convertView = inflater.inflate(R.layout.your_layout, parent, false); // inflate your_layout.xml

        //initialize your your_layout.xml view
        holder.name = convertView.findViewById(R.id.tv_item_name);
        holder.phone = convertView.findViewById(R.id.tv_item_phone);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    //set value into your_layout.xml
    holder.name.setText(allData.get(position).getName());
    holder.phone.setText(allData.get(position).getNumber());

    return convertView;
}

}

}

回答by Rakesh

Change your code like this. Add null check with convertView before your try block.

像这样更改您的代码。在 try 块之前使用 convertView 添加空检查。

    final MenuItem   menuItem = getItem(position);
    View view = convertView;
    final ViewHolder viewHolder;

if (convertView == null) {

    LayoutInflater inflater;

        inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.menu_item, parent, false);
        viewHolder = new ViewHolder();
//      viewHolder.half = (TextView) view.findViewById(R.id.half);
        viewHolder.name = (TextView) view.findViewById(R.id.name);
        viewHolder.description = (TextView) view.findViewById(R.id.description);
        viewHolder.price = (TextView) view.findViewById(R.id.price);
        viewHolder.add = (Button) view.findViewById(R.id.add);
        viewHolder.selectedView = view.findViewById(R.id.selectedView);
        viewHolder.remove = (Button) view.findViewById(R.id.remove);
        viewHolder.total = (TextView) view.findViewById(R.id.itemTotal);
        viewHolder.quantity = (TextView) view.findViewById(R.id.quantity);
        view.setTag(viewHolder);
}else{
    viewHolder= (ViewHolder)convertView.getTag(); 
}

回答by shilpa Todakar

public class AndroidListViewActivity extends ListActivity 
{
   private ListView listView;
   private String names[] = {
        "HV CAPACITOR",
        "LV CAPACITORCSS",

         };

private String desc[] = {
        "The Powerful Hypter Text Markup Language 5",
        "Cascading Style Sheets",

};


private Integer imageid[] = {
        R.drawable.hv_capacitor,
        R.drawable.lv_capacitor,

};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   // setContentView(R.layout.capacitor_layout);
    // storing string resources into Array
    String[] product_name = getResources().getStringArray(R.array.product_name);
    // Binding Array to ListAdapter
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, product_name));

    ListView lv = getListView();

    // listening to single list item on click

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            // selected item
            String product = ((TextView) view).getText().toString();

            // Launching new Activity on selecting single List Item
            Intent i = new Intent(getApplicationContext(), SingleListItem.class);
            // sending data to new activity
            i.putExtra("product", product);
            startActivity(i);

        }
    });


    CapacitorList capacitorList = new CapacitorList(this, names, desc, imageid);
    listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(capacitorList);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        // Launching new Activity on selecting single List Item
        Intent i = new Intent(getApplicationContext(), CapacitorList.class);
        // sending data to new activity

       // startActivity(i);

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(getApplicationContext(), "You Clicked " + names[i], Toast.LENGTH_SHORT).show();
        }
    });

}

    @Override
    public boolean onCreateOptionsMenu (Menu menu){
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected (MenuItem item){
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);


    }
}