Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the jump caused by the optimization of ListView picture download

Shulou Source: shulou.com Published: 2022-06-02 04:30:45 09月20日 Update

This article mainly explains "how to solve the jump caused by ListView picture download optimization", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to solve the jump caused by the optimization of ListView picture download.

Public class MainActivity extends Activity {

HashMap map = new HashMap ()

Private ListView listview

@ Override

Protected void onCreate (Bundle savedInstanceState) {

Super.onCreate (savedInstanceState)

SetContentView (R.layout.activity_main)

Listview = (ListView) findViewById (R.id.listView1)

MyApapter myApapter = new MyApapter ()

Listview.setAdapter (myApapter)

}

Class User

{

ImageView p_w_picpathview

ProgressBar pb

TextView tv

Button button

}

Class MyApapter extends BaseAdapter

{

@ Override

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

View view = null

User user = new User ()

LayoutInflater inflater = getLayoutInflater ()

If (convertView==null)

{

View = inflater.inflate (R.layout.itmp, null)

User.p_w_picpathview = (ImageView) view.findViewById (R.id.p_w_picpathView1)

User.pb = (ProgressBar) view.findViewById (R.id.progressBar1)

User.button = (Button) view.findViewById (R.id.button1)

User.tv = (TextView) view.findViewById (R.id.textView1)

View.setTag (user)

} else

{

View = convertView

User = (User) view.getTag ()

}

/ / download pictures

/ / set the status of the image before it starts downloading

User.p_w_picpathview.setImageResource (R.drawable.ic_launcher)

Bitmap bitmap = map.get (position)

If (bitmap==null)

{

MyAsyncTask myp_w_picpathview = new MyAsyncTask (user.p_w_picpathview,position,user.pb)

Myp_w_picpathview.execute ("http://192.168.56.1:8080/service/qq"+position+".png");"

} else

{

User.p_w_picpathview.setImageBitmap (bitmap)

}

Return view

}

@ Override

Public int getCount () {

/ / TODO Auto-generated method stub

Return 10

}

@ Override

Public Object getItem (int position) {

/ / TODO Auto-generated method stub

Return null

}

@ Override

Public long getItemId (int position) {

/ / TODO Auto-generated method stub

Return 0

}

}

Class MyAsyncTask extends AsyncTask

{

Int position

ImageView p_w_picpathview

ProgressBar pb

Public MyAsyncTask (ImageView paired picpathview.int position,ProgressBar pb) {

This.p_w_picpathview = p_w_picpathview

This.position = position

This.pb = pb

}

@ Override

Protected Bitmap doInBackground (String... Params) {

Bitmap bitmap = null

Try {

URL url = new URL (params [0])

URLConnection connection = url.openConnection ()

InputStream is = connection.getInputStream ()

Bitmap = BitmapFactory.decodeStream (is)

} catch (MalformedURLException e) {

/ / TODO Auto-generated catch block

E.printStackTrace ()

} catch (IOException e) {

/ / TODO Auto-generated catch block

E.printStackTrace ()

}

Return bitmap

}

@ Override

Protected void onPostExecute (Bitmap result) {

/ / get the first line visible on the current screen

Int fvb = listview.getFirstVisiblePosition ()

Int lvp = listview.getLastVisiblePosition ()

If (position > = fvb&&position

Tags: Picture picture download content learning practical deeper line interest practical practical screen easy to operate method more friends status website channel query Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Tech Info Shulou Information Shulou Technology Xiaomi