【问题标题】:How can i refresh this app with SwipeRefreshLayout?如何使用 SwipeRefreshLayout 刷新此应用程序?
【发布时间】:2016-11-03 23:21:44
【问题描述】:

我正在关注如何在 中制作画廊的教程,我想使用SwipeRefreshLayout 来更新图像,但我真的不知道如何。这是代码,我该如何解决?

MainActivity.java
package info.androidhive.glide.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;

import org.json.JSONArray;  
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import info.androidhive.glide.R;
import info.androidhive.glide.adapter.GalleryAdapter;
import info.androidhive.glide.app.AppController;
import info.androidhive.glide.model.Image;

public class MainActivity extends AppCompatActivity {

private String TAG = MainActivity.class.getSimpleName();
private static final String endpoint =         "http://url.com/examp.php";
private ArrayList<Image> images;
private ProgressDialog pDialog;
private GalleryAdapter mAdapter;
private RecyclerView recyclerView;

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

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

pDialog = new ProgressDialog(this);
images = new ArrayList<>();
mAdapter = new GalleryAdapter(getApplicationContext(), images);

RecyclerView.LayoutManager mLayoutManager = new   GridLayoutManager(getApplicationContext(), 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);

/* recyclerView.addOnItemTouchListener(new    GalleryAdapter.RecyclerTouchListener(getApplicationContext(), recyclerView, new    GalleryAdapter.ClickListener() {
    @Override
    public void onClick(View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("images", images);
        bundle.putInt("position", position);

        FragmentTransaction ft =   getSupportFragmentManager().beginTransaction();
        SlideshowDialogFragment newFragment =    SlideshowDialogFragment.newInstance();
        newFragment.setArguments(bundle);
        newFragment.show(ft, "slideshow");
    }

    @Override
    public void onLongClick(View view, int position) {

    }
}));*/

fetchImages();
}

private void fetchImages() {

pDialog.setMessage("Downloading json...");
pDialog.show();

JsonArrayRequest req = new JsonArrayRequest(endpoint,
        new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                Log.d(TAG, response.toString());
                pDialog.hide();

                images.clear();
                for (int i = 0; i < response.length(); i++) {
                    try {
                        JSONObject object = response.getJSONObject(i);
                        Image image = new Image();
                        image.setName(object.getString("name"));

                        JSONObject url = object.getJSONObject("url");
                        image.setSmall(url.getString("small"));
                        image.setMedium(url.getString("medium"));
                        image.setLarge(url.getString("large"));
                        image.setTimestamp(object.getString("timestamp"));

                        images.add(image);

                    } catch (JSONException e) {
                        Log.e(TAG, "Json parsing error: " + e.getMessage());
                    }
                }

                mAdapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e(TAG, "Error: " + error.getMessage());
        pDialog.hide();
    }
});

// Adding request to request queue
AppController.getInstance().addToRequestQueue(req);
}
}

这里是activity_main.xml

Activity_main.xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

    标签: android-studio java json android-studio


    【解决方案1】:

    这是一个很好的例子,解释了如何Swipe to Refresh

    您必须将整个 xml 包装在 android.support.v4.widget.SwipeRefreshLayout 中。在onCreate中初始化SwipeRefreshLayout,为它添加一个监听器并在它的方法里面:onRefresh()你调用你的方法:fetchImages();

    不要忘记在您的方法一中使用yourSwipeLayoutName.setRefreshing(false); 网络请求已结束,以便刷新知道何时停止。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-23
      • 1970-01-01
      • 1970-01-01
      • 2022-08-16
      • 2017-04-19
      相关资源
      最近更新 更多