2016-02-01 45 views
-4

這是我的XML Android列表視圖基礎適配器getView多次觸發?

<RelativeLayout 
    android:id="@+id/preview_header" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/socialbottom" 
    android:padding="10dp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Preview" 
     android:textColor="@color/white" 
     android:textStyle="bold" /> 

    <ImageButton 
     android:id="@+id/close_preview_btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:background="@drawable/close_btn" /> 
</RelativeLayout> 


<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/empty_view" 
    android:layout_below="@+id/preview_header"> 

    <ListView 
     android:id="@+id/preview_dialog_list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="8dp" 
     android:scrollbars="none"></ListView> 
</RelativeLayout> 

<View 
    android:id="@+id/empty_view" 
    style="@style/Space" 
    android:layout_width="match_parent" 
    android:layout_above="@+id/preview_add_more_btn"></View> 

<Button 
    android:id="@+id/preview_add_more_btn" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/empty_view1" 
    android:background="@color/content_text" 
    android:drawableLeft="@drawable/plus_icon" 
    android:drawablePadding="50dp" 
    android:gravity="center_horizontal" 
    android:padding="8dp" 
    android:paddingLeft="300dp" 
    android:text="@string/add_more" 
    android:textColor="@color/white" /> 

<View 
    android:id="@+id/empty_view1" 
    style="@style/Space" 
    android:layout_width="match_parent" 
    android:layout_above="@+id/footer_preview"></View> 

<RelativeLayout 
    android:id="@+id/footer_preview" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:orientation="vertical"> 

    <EditText 
     android:id="@+id/preview_input_timeline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentStart="true" 
     android:background="@color/acticty_textbox" 
     android:hint="What&apos;s up, admin?" 
     android:inputType="textMultiLine" 
     android:padding="8dp" /> 


    <Button 
     android:id="@+id/preview_post_btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/preview_input_timeline" 
     android:layout_centerHorizontal="true" 
     android:layout_gravity="center_horizontal" 
     android:background="@drawable/savebox" 
     android:padding="8dp" 
     android:text="Post" 
     android:textAllCaps="true" 
     android:textColor="@color/white" 
     android:textStyle="bold" /> 
</RelativeLayout> 

這是JAVA

public View getView(int position, View convertView, ViewGroup parent) { 
     convertView = mInflater.inflate(R.layout.preview_list_view, null); 
     ImageButton removeBtn = (ImageButton) convertView.findViewById(R.id.remove_preview); 
     ImageView imageView = (ImageView) convertView.findViewById(R.id.preview_image); 
     VideoView videoView = (VideoView) convertView.findViewById(R.id.preview_video); 
     Log.e("video sizzzessssssss", String.valueOf(imagesList.size())); 
     if (SocialActivity.MEDIA_TYPE_IMAGE == previewType) { 
      videoView.setVisibility(View.INVISIBLE); 
      imageView.setVisibility(View.VISIBLE); 
      String path = imagesList.get(position); 
      File imgFile = new File(path); 
      if (imgFile.exists()) { 
       // Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 
       // imageView.setImageBitmap(myBitmap); 
       Bitmap d = new BitmapDrawable(context.getResources(), imgFile.getAbsolutePath()).getBitmap(); 
       int nh = (int) (d.getHeight() * (512.0/d.getWidth())); 
       Bitmap scaled = Bitmap.createScaledBitmap(d, 512, nh, true); 
       imageView.setImageBitmap(scaled); 

      } 


     } else { 
      imageView.setVisibility(View.INVISIBLE); 
      videoView.setVisibility(View.VISIBLE); 
      videoView.setVideoPath(imagesList.get(position)); 
      MediaController mediaControls = new MediaController(SocialActivity.socialActivity); 
      videoView.setMediaController(mediaControls); 
      videoView.start(); 
      videoView.pause(); 
      Log.e("path video", imagesList.get(position)); 
     } 
     removeBtn.setOnClickListener(new ListCustomClickEvents(callback, position)); 

     return convertView; 
    } 
+0

什麼問題? –

+0

我不知道你的問題是什麼。 –

+0

getview方法多次觸發@ ssh – kathir

回答

0

您必須使用setTag()getTag()

喜歡這些

public class ViewHolder { 


     //Declare yours all component here 

     // like below example 
     private ImageView profile_iv; 
     private TextView name_tv; 


    } 

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

///Firstly check the convertView is null or not like below 
     final ViewHolder _viewHolder; 
     if (convertView == null) { 


      _viewHolder = new ViewHolder(); 
      LayoutInflater _layInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = _layInflater.inflate(R.layout.connection_friend_item, null); 

     ///Finds yours layout items id here like below example 

      _viewHolder.profile_iv = (ImageView) convertView.findViewById(R.id.profile_iv); 
      _viewHolder.name_tv = (TextView) convertView.findViewById(R.id.name_tv); 



      convertView.setTag(_viewHolder); 


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

     ////Set the data in the layout's item as below 

     _viewHolder.name_tv.setText(imagesList.get(position)); 
     _viewHolder.profile_iv.setImageBitmap(imagesList.get(position)); 


     return convertView; 
    } 
0

我認爲你應該使用視圖座的設計模式使您能夠訪問每個項目視圖,而不需要對查找,節省了寶貴的處理器週期。具體來說,它避免了在ListView滾動期間頻繁調用findViewById(),並且這會使其平滑。

請參考下代碼演示:

public static class ViewHolder{ 

     public TextView aliasTextView; 
     public TextView numTextView; 
     public TextView statusTextView; 
     public RelativeLayout mainLayout; 
     public NetworkImageView _profileImageView; 
    } 

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

     View v = convertView; 
     final ViewHolder holder; 
     LayoutInflater inflater = (LayoutInflater)_context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     DisputeListBean bean = _ArrayList.get(position); 

     if(convertView == null) 
     { 
      holder = new ViewHolder(); 
      v = inflater.inflate(R.layout.dispute_row, null,false); 
      holder.mainLayout = (RelativeLayout)v.findViewById(R.id.mainLayout); 
      holder.aliasTextView = (TextView)v.findViewById(R.id.user_name); 
      holder.statusTextView = (TextView)v.findViewById(R.id.user_status); 
      holder._profileImageView = (NetworkImageView)v.findViewById(R.id.profile_pic); 
      v.setTag(holder); 
     }else { 
      holder = (ViewHolder) v.getTag(); 
     } 

     holder.aliasTextView.setText(bean.getAlias_name().toUpperCase()); 

     return v; 
    } 
0

不要給ListView的高度WRAP_CONTENT。當你的高度爲wrap_content時,listview將首先填充少數列表項目以確定列表視圖的實際高度

0

由於ListView中的內部緩存機制,Android會爲每個可見項目觸發兩次getView方法。它只發生在第一次,當你打開包含ListView的Activity/Fragment/View時。欲瞭解更多信息,請參閱谷歌與ScrapViews ListView請求。

解決你的問題,你只需要檢查

if(convertView == null) 
    convertView = inflate... 

或谷歌使用ViewHolder模式。