1

I'm having an issue using SwipeRefreshLayout, the functionality works perfectly but the spinner won't show. I've already used all the provided solutions out there but still nothing.

I don't like to use XML at all so I basically code everything and avoid it. I have a class extending SwipeRefreshLayout where I create the listView, add the adapter and configure the refresh action etc. My minimum API Level is 16.

If you guys need any further details or code please let me know and I'll be glad to provide them, any help is appreciated!

My activity

public class MainActivity extends Activity {

    private static MainActivity mainActivity = null;
    private MainView mainView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mainView = new MainView(this);
        setContentView(mainView);
    }

    public static MainActivity getMainActivity() {
        return mainActivity;
    }
}

My main view:

public class MainView extends SwipeRefreshLayout {

    private ListView listView;

    public MainView(Context context) {
        super(context);

        final String [] objects = new String[] {"panda", "horse", "fox"};

        listView = new ListView(context);
        final ArrayAdapter arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, objects);
        listView.setAdapter(arrayAdapter);
        addView(listView);

        this.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                objects[0] = "Big panda!";
                arrayAdapter.notifyDataSetChanged();
                setRefreshing(false);
            }
        });
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width = r - l;
        int height = b - t;

        listView.layout(0, 0, width, height);
    }
}
adrastious
  • 63
  • 8
  • 2
    please post the relevant code – kandroidj Jan 05 '16 at 23:53
  • Added the code I use, not full class but the part that matters. – adrastious Jan 06 '16 at 00:43
  • Update: this hasn't been solved, tried many solutions but seems not to work. – adrastious Jan 11 '16 at 22:09
  • I am having trouble reproducing your issue, i set up a new project and in my activity i created this SwipeRefreshLayout custom just like you and i am getting the refresh indicator every time. And you say that you are not seeing the progress view? is there something funky you are doing when adding this to your parent view? see my posted answer below with a simple demo – kandroidj Jan 11 '16 at 22:35
  • Edited the code to have everything to reproduce as simple as possible. – adrastious Jan 13 '16 at 02:14
  • Just to update, no one has been able to tell me what's wrong. I've switched to use a header on the listview to fake a pull to refresh. – adrastious Jan 20 '16 at 06:24

2 Answers2

0

Hard to tell without seeing any code, but my gut tells me you need to make sure that the appropriate progress view is linked and you are setting the visibility to VISIBLE and INVISIBLE at the right times.

H. Bhonsle
  • 136
  • 5
  • I tried setting the listView to invisible when the refresh happens, thing is the circular loader doesn't even show. – adrastious Jan 06 '16 at 00:43
  • I believe you need to actually call "setOnRefreshListener" on the listview. Check out http://stackoverflow.com/questions/26858692/swiperefreshlayout-setrefreshing-not-showing-indicator-initially. – H. Bhonsle Jan 06 '16 at 00:54
  • I can't set a refresh listener on the listview, I actually did it on the class and that part is working, the only weird thing is the loading is not showing. – adrastious Jan 06 '16 at 01:06
  • Your class should not extend SwipeRefreshLayout. Follow the guide here https://www.bignerdranch.com/blog/implementing-swipe-to-refresh/ – H. Bhonsle Jan 06 '16 at 01:09
  • Added the XML to my project and didn't work, I know can see the layout bounds show that the spinner is there but has no width or height, is there any way I can change that? – adrastious Jan 06 '16 at 05:42
  • Try taking an xml snapshot using Android tools and see what is going on. Maybe there is another blank view accidentally on top or something. I don't know what would be forcing the spinner to lack width and height, that should be embedded in the swiperefreshlayout code and should not need to be modified at all. – H. Bhonsle Jan 06 '16 at 08:49
0

Here is a simple demo, with working progress on a coded SwipeRefreshLayout each time you Swipe it will add an item to the list

my Activity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RelativeLayout container = (RelativeLayout) findViewById(R.id.container);

        CustomSwipeRefresh swipeRefresh = new CustomSwipeRefresh(this);
        container.addView(swipeRefresh);
        swipeRefresh.refreshData();
    }
}

with layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container"
    tools:context="com.divshark.customswiperefresh.MainActivity">


</RelativeLayout>

Then i created CustomSwipeRefresh :

public class CustomSwipeRefresh extends SwipeRefreshLayout {

    private static final String TAG = CustomSwipeRefresh.class.getSimpleName();

    private static int MIN_LIST = 50;

    private List<String> mItems = new ArrayList<>();
    private Context mContext;

    private ListView listView;
    private ListAdapter mAdapter;

    public CustomSwipeRefresh(Context context){
        super(context);

        this.mContext = context;


        mAdapter = new ListAdapter(mContext, mItems);

        listView = new ListView(context);
        listView.setAdapter(mAdapter);
        listView.setBackgroundColor(Color.WHITE);
        listView.setDivider(null);
        listView.setDividerHeight(0);
        addView(listView);

        setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {

                setRefreshing(true);

                postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        onRefreshData(true);

                        Log.d(TAG,"--- FINISHED REFRESHING ---");
                        setRefreshing(false);
                    }
                }, 2000);

            }
        });

    }

    public void refreshData(){
        onRefreshData(false);
    }

    /**
     * Called when the data is refreshed in the list
     */
    private void onRefreshData(boolean addItem){

        if(addItem){
            MIN_LIST++;
        }

        mItems.clear();

        for(int i = 0; i < MIN_LIST; i++){
            mItems.add("New item "+ Integer.toString(i + 1));
        }

        mAdapter.notifyDataSetChanged();
    }


    public class ListAdapter extends BaseAdapter{

        public List<String> mItems;
        public Context mContext;
        public LayoutInflater mInflater;

         class Holder{
            AppCompatTextView mTvItem;
        }

        public ListAdapter(Context context, List<String> items){

            this.mContext = context;
            this.mItems = items;
            this.mInflater = LayoutInflater.from(mContext);

        }

        @Override
        public int getCount() {
            return mItems.size();
        }

        @Override
        public Object getItem(int position) {
            return mItems.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

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

            Holder holder ;

            if(convertView == null){
                holder = new Holder();
                convertView = mInflater.inflate(R.layout.item_row, parent, false);

                holder.mTvItem = (AppCompatTextView) convertView.findViewById(R.id.item);

                convertView.setTag(holder);
            }else{
                holder = (Holder) convertView.getTag();
            }

            String item = mItems.get(position);
            if(item != null){
                holder.mTvItem.setText(item);
            }

            return convertView;
        }
    }
}

and item layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent" android:padding="16dp">
<android.support.v7.widget.AppCompatTextView
    android:id="@+id/item"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" />
</LinearLayout>

Hope this helps!

kandroidj
  • 13,784
  • 5
  • 64
  • 76
  • I tested your code and it's working, I edited my question and added all the code as I have it so you can see how the spinner won't be showing and if you can find the issue I'll be saved! – adrastious Jan 13 '16 at 02:12