Create “loading” animation easily

后端 未结 3 1729
感动是毒
感动是毒 2020-12-12 03:26

I have an android listView with an image in each item. It takes a while till the image is loaded. How can I add a \"loading\" animation every time the image is

相关标签:
3条回答
  • 2020-12-12 04:10

    I have added this in my xml

           <ProgressBar
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:indeterminate="true" 
            android:layout_centerInParent="true"/>
    

    I can stop it on PostExecute() but I'm not sure how to make Picasso stop it itself.

    0 讨论(0)
  • 2020-12-12 04:15

    Use following code:

    MainActivity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
    
        ArrayList<HashMap<String, String>> itemsList = new ArrayList<HashMap<String, String>>();
    
        ListView listView = (ListView) findViewById(R.id.listView);
    
        for (int i = 0; i < 5; i++) {
    
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
    
            // adding each child node to HashMap key =&gt; value
            map.put("key1", "value1");
            map.put("key2", "value2");
    
            // adding HashList to ArrayList
            itemsList.add(map);
        }
    
        ListAdapter listAdapter = new ListAdapter(this, itemsList);
        listView.setAdapter(listAdapter);
    }
    

    list_row.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:src="@drawable/ic_launcher" />
    
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_toEndOf="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    </RelativeLayout>
    

    ListAdapter.java

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.list_row, null);
    
        ImageView imageView = (ImageView) vi.findViewById(R.id.imageView);
        final ProgressBar progressBar = (ProgressBar) vi.findViewById(R.id.progressBar);
    
        //This line shows progressBar again for recycled view
        progressBar.setVisibility(View.VISIBLE);
    
        Picasso.with(activity.getApplicationContext()).load(imagePath).resize(100, 100)
                .into(imageView, new Callback() {
                    @Override
                    public void onSuccess() {
                        progressBar.setVisibility(View.GONE);
                    }
    
                    @Override
                    public void onError() {
                        //error
                    }
                });
    
        return vi;
    }
    

    Don't forget to add import for Picasso Callback

    import com.squareup.picasso.Callback;
    
    0 讨论(0)
  • 2020-12-12 04:17

    You can use this library. It work and you can custom your wheels ! :) https://github.com/Todd-Davies/ProgressWheel

    0 讨论(0)
提交回复
热议问题