【问题标题】:Change specific row item layout in listView更改 listView 中的特定行项布局
【发布时间】:2013-03-19 17:37:20
【问题描述】:

我创建了一个 listView,我可以将图像和文本放入 listView 并动态添加行到 listView。

但是我不明白如何实现我可以更改列表视图中的特定行项目布局?

例如,在活动开始时,我将用 5 行初始化 listView,并且当触发发生时(几分钟后)我想更改例如第四个 listView 项(仅更改第四个条目的布局,所有其他条目保持原始布局)

非常感谢

private  ListView lv;
private List<String> text = new ArrayList<String>();
private List<Bitmap> listImages;
private String freindsId;

ArrayList<String> text_sort = new ArrayList<String>();
ArrayList<Bitmap> image_sort = new ArrayList<Bitmap>();


private MyCustomAdapter adapter;
  adapter =new MyCustomAdapter(text,listImages);
  lv.setAdapter(adapter);

class MyCustomAdapter extends BaseAdapter{
          public   List<String> text_array = new ArrayList<String>();
          public   List<Bitmap> image_array = new ArrayList<Bitmap>();
          public int getCount(){
               return text_array.size();
          }

          MyCustomAdapter(List<String> text, List<Bitmap> image)
          {
           text_array = text;
           image_array = image;
          }
          public long getItemId(int position){
               return position;
          }
          public String getItem(int position){
               return null;
          }
          public View getView(final int position, View convertView, ViewGroup parent) {
            LayoutInflater inflate = getLayoutInflater();
            View v = inflate.inflate(R.layout.listview, parent, false);
            final ImageView image = (ImageView) v.findViewById(R.id.ImageView01);
            TextView txtUnderImage =(TextView) v.findViewById(R.id.TextView01);

             if(listImages.get(position) != null) {
                         Bitmap bitmap = image_array.get(position);
                        image.setImageBitmap(bitmap);
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                        final byte[] byteArray = stream.toByteArray();

                         image.setOnClickListener(new View.OnClickListener() {

                            public void onClick(View v) {
                                // Switching to Register screen
                             Intent i = new Intent(getApplicationContext(), itemSaleActivity.class);
                             i.putExtra("picture", byteArray);
                            startActivity(i);


                                }
                        });
                         txtUnderImage.setText(text_array.get(position));
                         image.setVisibility(View.VISIBLE);
              } else {
                         image.setVisibility(View.GONE);
                         image.setImageBitmap(null);
                          image.setVisibility(View.VISIBLE);
          }
         return v;

        }
         public void addObject(String text, Bitmap bitmap) {
                text_array.add(text);
                image_array.add(bitmap);
            notifyDataSetChanged();
         } 

         public void addFirst(String text, Bitmap bitmap) {

                image_array.add(0,bitmap);
                text_array.add(0,text);

                notifyDataSetChanged();
             } 
         public void removeObject(String text,Bitmap bitmap) {

                int indexToRemove = image_array.indexOf(bitmap);
                image_array.remove(indexToRemove);
                text_array.remove(indexToRemove);
                notifyDataSetChanged();
         }
         public void deleteAll() {
             image_array.clear();
             text_array.clear();
             notifyDataSetChanged();
         }
    }

【问题讨论】:

    标签: java android


    【解决方案1】:

    尝试像这样获取您对特定位置的看法

    getChildAt(position) 你会得到你的看法,

    并更改行的布局并调用 notifyDataSetChanged()。

    如果您要突出显示选定的行,请使用此 Inflate ListView row from OnClickListener in Android?

    当事件触发您的适配器的更改数据并在适配器对象上调用 notifyDataSetChanged()。

    【讨论】:

    • 嗨,谢谢你的回答,但我不明白你的意思。如何更改 listView 中特定行的布局?
    • 非常感谢您的回答,我是否需要调用 notifyDataSetChanged() - 我认为 notifyDataSetChanged 方法仅适用于存储在适配器中的数据...当 listView 更改时是否有任何方法可以调用?
    • 为了用高亮显示的行刷新你的列表,你需要调用 notifyDataSetChanged() 否则它不会生效,直到列表刷新
    • 谢谢,当我使用 getChildAt 方法获取视图时,如何更改行的布局?
    • @AdirRahamim 您将使用 getchildAt(position) 获得一个视图,并且您可以对视图执行任何操作,例如更改背景、大小等。
    【解决方案2】:

    对第四行项目进行更改。在您的适配器上调用 notifyDataSetChanged()。

    notifyDataSetChanged() 通知附加的观察者底层数据已更改,任何反映数据集的视图都应自行刷新。

    notifyDataSetChanged() 刷新您的列表视图。

    【讨论】:

    • 我应该如何获取和更改第四个 listView 项目布局?
    【解决方案3】:

    使用getChildAt() 方法访问您要更改的任何行。例如listView.getChildAt(4)

    【讨论】:

    • 谢谢,然后我应该如何更改特定的行布局?我是否需要调用 notifyDataSetChanged() - 我认为 notifyDataSetChanged 方法仅适用于存储在 listView 中的数据...当 listView 更改时是否有任何方法可以调用?
    • getChildAt 将返回列表视图中该位置的任何内容。如果它是 textview 或类似的东西,您可以直接进行更改。如果它是另一个布局,那么您将必须获得该布局的特定子级。您不需要在更改时调用任何额外的方法
    • 嗨,我想更改特定行项中的另一个布局...我应该如何使用 getChildAt() 方法实现它?非常感谢
    • 请帮助我了解如何将 listView 行项目更改为另一个布局?我仍然不明白我该如何实现它?非常感谢
    【解决方案4】:

    此处描述的方法将查看整个列表,而您只需要更新可见的视图,其余更改将在“重绘”时得到注意。

    因此,请查看THIS LINK 提供的答案,它提供了一个解决方案,您只需要识别视图的位置,并且只有在该视图可见时才能更新视图。

    引用那个答案 -

    int iFirst = getFirstVisiblePosition();
    int iLast = getLastVisiblePosition();
    
    if ( indexToChange >= numberOfRowsInSection() ) {
        Log.i( "MyApp", "Invalid index. Row Count = " + numberOfRowsInSection() );
    }
    else {      
        if ( ( index >= iFirst ) && ( index <= iLast ) ) {
            // get the view at the position being updated - need to adjust index based on first in the list
            View vw = getChildAt( sysvar_index - iFirst );
            if ( null != vw ) {
                // get the text view for the view
                TextView tv = (TextView) vw.findViewById(com.android.myapp.R.id.scrollingListRowTextView );
                if ( tv != null ) {
                    // update the text, invalidation seems to be automatic
                    tv.setText( "Item = " + myAppGetItem( index ) + ". Index = " + index + ". First = " + iFirst + ". Last = " + iLast );
                }
            }
        }
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多