Android列表视图多次基本适配器getView fire?

时间:2016-02-01 05:41:40

标签: android android-layout

这是我的XML

<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;
    }

4 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分:0)

我认为您应该使用View Holder设计模式使您能够访问每个列表项视图而无需查找,从而节省了宝贵的处理器周期。具体来说,它避免了在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;
    }

答案 2 :(得分:0)

不要将listview的高度作为wrap_content。当您将高度设置为wrap_content时,listview将首先填充少量列表项以确定listview的实际高度

答案 3 :(得分:0)

由于ListView中的内部缓存机制,Android将为每个可见项触发两次getView方法。当您打开包含ListView的Activity / Fragment / View时,它仅在第一次发生。有关更多信息,请参阅Google ScrapViews ListView请求。

要解决您的问题,您只需检查

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

或使用Google的ViewHolder模式。

相关问题