-4

This is my 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>

This is 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;
    }
Kathir
  • 4,359
  • 3
  • 17
  • 29

4 Answers4

0

You have to use the setTag() and getTag()

like these

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;
    }
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
0

I think you should use View Holder design pattern enables you to access each list item view without the need for the look up, saving valuable processor cycles. Specifically, it avoids frequent call of findViewById() during ListView scrolling, and that will make it smooth.

Refer below code for Demo:

 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;
    }
Anjali Tripathi
  • 1,477
  • 9
  • 28
0

Don't give listview's height as wrap_content. When you the height as wrap_content, the listview will first populate few list items to determine the actual height of the listview

Ankit Aggarwal
  • 5,317
  • 2
  • 30
  • 53
0

Due to inner caching mechanism in ListView, Android will be fire two times getView method for each visible item. It happens only for first time, when you open Activity/Fragment/View that contains a ListView. For more information refer to google with ScrapViews ListView request.

To solve your problem you need just check

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

or use ViewHolder pattern from Google.

Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119