如何添加轻扫到刷新列表视图

问题描述:

我在新闻应用程序的工作,我想通过在MainActivity
这种向下滑动,每一次更新ListView是我下面的MainActivity代码:如何添加轻扫到刷新列表视图

package com.infinitystone.mani.news; 

import android.app.LoaderManager; 
import android.app.LoaderManager.LoaderCallbacks; 
import android.content.Context; 
import android.content.Loader; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.customtabs.CustomTabsIntent; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.List; 

public class MainActivity extends AppCompatActivity implements LoaderCallbacks<List<News>> { 

    public static final String LOG_TAG = MainActivity.class.getName(); 

    private static final int NEWS_LOADER_ID = 1; 

    private TextView mEmptyView; 

    private NewsAdapter mAdapter; 

    private static final String GUARDIAN_REQUEST_URL = "http://content.guardianapis.com/search?order-by=newest&show-fields=thumbnail&page-size=20&api-key=2f3badbb-4a58-44b8-9800-9ee2a0f445f9"; 

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

     ListView newsList = (ListView) findViewById(R.id.list); 

     mEmptyView = (TextView) findViewById(R.id.empty_view); 
     newsList.setEmptyView(mEmptyView); 

     mAdapter = new NewsAdapter(this, new ArrayList<News>()); 
     newsList.setAdapter(mAdapter); 

     newsList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       News currentNews = mAdapter.getItem(position); 
       Uri uri = Uri.parse(currentNews.getUrl()); 

       CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); 

       builder.setToolbarColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary)); 
       builder.setSecondaryToolbarColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimaryDark)); 

       builder.setShowTitle(true); 

       final Bitmap backButton = BitmapFactory.decodeResource(getResources(), R.drawable.ic_arrow_back_white_24dp); 
       builder.setCloseButtonIcon(backButton); 

       CustomTabsIntent customTabsIntent = builder.build(); 
       customTabsIntent.launchUrl(MainActivity.this, uri); 

      } 
     }); 

     ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 

     NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 

     if(networkInfo != null && networkInfo.isConnected()){ 
      LoaderManager loaderManager = getLoaderManager(); 

      loaderManager.initLoader(NEWS_LOADER_ID, null, this); 
     } 

     else { 
      View loadingIndicator = findViewById(R.id.loading_indicator); 
      loadingIndicator.setVisibility(View.GONE); 

      mEmptyView.setText(R.string.no_internet); 
     } 
    } 

    @Override 
    public Loader<List<News>> onCreateLoader(int i, Bundle bundle) { 
     // Create a new loader for the given URL 
     return new NewsLoader(this, GUARDIAN_REQUEST_URL); 
    } 

    @Override 
    public void onLoadFinished(Loader<List<News>> loader, List<News> news) { 
     View loadingIndicator = findViewById(R.id.loading_indicator); 
     loadingIndicator.setVisibility(View.GONE); 

     mEmptyView.setText(R.string.no_news); 

     mAdapter.clear(); 

     if (news != null && !news.isEmpty()) { 
      mAdapter.addAll(news); 
     } 
    } 

    @Override 
    public void onLoaderReset(Loader<List<News>> loader) { 
     // Loader reset, so we can clear out our existing data. 
     mAdapter.clear(); 
    } 
} 

我已经在我的activity_main.xml文件
加入android.support.v4.widget.SwipeRefreshLayout我怎样才能实现在Java代码中SwipeRefreshLayout?为获取新闻资料
代码:

package com.infinitystone.mani.news; 


import android.text.TextUtils; 
import android.util.Log; 

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

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.nio.charset.Charset; 
import java.util.ArrayList; 
import java.util.List; 

import static com.infinitystone.mani.news.MainActivity.LOG_TAG; 

public class QueryUtils { 


    private QueryUtils() { 

    } 

    public static List<News> fetchNewsData(String requestUrl) { 
     URL url = createUrl(requestUrl); 

     String jsonResponse = null; 
     try { 
      jsonResponse = makeHttpRequest(url); 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "Problem making the HTTP request.", e); 
     } 

     List<News> news = extractFeaturesFromJson(jsonResponse); 

     return news; 
    } 

    private static URL createUrl(String stringUrl) { 
     URL url = null; 
     try { 
      url = new URL(stringUrl); 
     } catch (MalformedURLException e) { 
      Log.e(LOG_TAG, "Problem building the URL ", e); 
     } 
     return url; 
    } 

    private static String makeHttpRequest(URL url) throws IOException { 
     String jsonResponse = ""; 

     // If the URL is null, then return early. 
     if (url == null) { 
      return jsonResponse; 
     } 

     HttpURLConnection urlConnection = null; 
     InputStream inputStream = null; 
     try { 
      urlConnection = (HttpURLConnection) url.openConnection(); 
      urlConnection.setReadTimeout(10000 /* milliseconds */); 
      urlConnection.setConnectTimeout(15000 /* milliseconds */); 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.connect(); 

      // If the request was successful (response code 200), 
      // then read the input stream and parse the response. 
      if (urlConnection.getResponseCode() == 200) { 
       inputStream = urlConnection.getInputStream(); 
       jsonResponse = readFromStream(inputStream); 
      } else { 
       Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode()); 
      } 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "Problem retrieving the earthquake JSON results.", e); 
     } finally { 
      if (urlConnection != null) { 
       urlConnection.disconnect(); 
      } 
      if (inputStream != null) { 
       // Closing the input stream could throw an IOException, which is why 
       // the makeHttpRequest(URL url) method signature specifies than an IOException 
       // could be thrown. 
       inputStream.close(); 
      } 
     } 
     return jsonResponse; 
    } 

    private static String readFromStream(InputStream inputStream) throws IOException { 
     StringBuilder output = new StringBuilder(); 
     if (inputStream != null) { 
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8")); 
      BufferedReader reader = new BufferedReader(inputStreamReader); 
      String line = reader.readLine(); 
      while (line != null) { 
       output.append(line); 
       line = reader.readLine(); 
      } 
     } 
     return output.toString(); 
    } 

    private static List<News> extractFeaturesFromJson(String newsJSON) { 

     if (TextUtils.isEmpty(newsJSON)) { 
      return null; 
     } 

     List<News> news = new ArrayList<>(); 

     try { 
      JSONObject rootObject = new JSONObject(newsJSON); 

      JSONObject responseObject = rootObject.getJSONObject("response"); 

      JSONArray resultsArray = responseObject.getJSONArray("results"); 

      for (int i = 0; i < resultsArray.length(); i++) { 
       JSONObject currentNews = resultsArray.getJSONObject(i); 

       String title = currentNews.getString("webTitle"); 

       String titleUrl = currentNews.getString("webUrl"); 

       String date = currentNews.getString("webPublicationDate"); 

       JSONObject fields = currentNews.getJSONObject("fields"); 

       String thumbnail = fields.getString("thumbnail"); 

       News news1 = new News(title, thumbnail, date, titleUrl); 

       news.add(news1); 
      } 
     } catch (JSONException e) { 
      Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e); 
     } 

     return news; 
    } 
} 
+3

的可能的复制[如何实现Android的上拉,以刷新(https://stackoverflow.com/questions/4583484/how-to-implement-android-pull-to-refresh) – akhilesh0707

+1

请搜索谷歌之前问这样的问题 –

+0

我确实搜索谷歌,但没有什么帮助我。我是菜鸟 – mani

尝试this响应刷新手势,它应该帮助。

+0

这个回应没有太大的帮助 – mani

+1

这应该是一个评论 –

+0

@Nilesh对不起,没有足够的声誉 – Mukesh

as per my above comment使用SwipeRefreshLayout

的SwipeRefreshLayout应该用于每当用户可以通过垂直滑动手势刷新的图的内容。实例化此视图的活动应添加一个OnRefreshListener,以便在刷新刷新手势完成时得到通知。 SwipeRefreshLayout每次手势再次完成时都会通知监听者;

不是设置setOnRefreshListenerSwipeRefreshLayout

void setOnRefreshListener (SwipeRefreshLayout.OnRefreshListener listener)

设置侦听器时刷新通过滑动手势触发通知。

示例代码

SwipeRefreshLayout mSwipeRefreshView; 
mSwipeRefreshView = (SwipeRefreshLayout) findViewById(R.id.homeScreenSwipeRefreshView); 

mSwipeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
      @Override 
      public void onRefresh() { 

       // make your api request here 
      } 
     }); 

当你的API请求complted不是使用setRefreshing(false)

setRefreshing(boolean refreshing) 

通知刷新状态已经改变的部件。

mSwipeRefreshView.setRefreshing(false);

+0

我如何使API请求在onRefresh()方法? – mani

+0

@mani你在哪里让你请求的API –

+0

它给了我android.os.NetworkOnMainThreadException' – mani

得到一个关于你刷卡刷新布局。

private SwipeRefreshLayout swipeRefreshLayout; 

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

    swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh); 

    swipeRefreshLayout.setOnRefreshListener(this); 

    // ... your other codes... 

} 

现在,让你的活动实现监听器。

public class MainActivity extends AppCompatActivity implements LoaderCallbacks<List<News>>, SwipeRefreshLayout.OnRefreshListener { 

    // ... 

} 

最后实现这些方法

@Override 
    public void onRefresh(){ 
    swipeRefreshLayout.setRefreshing(true); 

    newsList = fetchNewsData(GUARDIAN_REQUEST_URL); 
    mAdapter.notifyDataSetChanged(); 

    swipeRefreshLayout.setRefreshing(false); 

    } 

编辑:

这里是MainActivity的完整代码。请使用这个,让我知道这是否工作。

import android.app.LoaderManager; 
import android.app.LoaderManager.LoaderCallbacks; 
import android.content.Context; 
import android.content.Loader; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.customtabs.CustomTabsIntent; 
import android.support.v4.content.ContextCompat; 
import android.support.v4.widget.SwipeRefreshLayout; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.List; 

public class MainActivity extends AppCompatActivity implements LoaderCallbacks<List<News>>, SwipeRefreshLayout.OnRefreshListener { 

    public static final String LOG_TAG = MainActivity.class.getName(); 

    private static final int NEWS_LOADER_ID = 1; 

    private TextView mEmptyView; 

    private NewsAdapter mAdapter; 

    private static final String GUARDIAN_REQUEST_URL = "http://content.guardianapis.com/search?order-by=newest&show-fields=thumbnail&page-size=20&api-key=2f3badbb-4a58-44b8-9800-9ee2a0f445f9"; 

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

     ListView newsList = (ListView) findViewById(R.id.list); 

     mEmptyView = (TextView) findViewById(R.id.empty_view); 
     newsList.setEmptyView(mEmptyView); 

     mAdapter = new NewsAdapter(this, new ArrayList<News>()); 
     newsList.setAdapter(mAdapter); 

     swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh); 

     swipeRefreshLayout.setOnRefreshListener(this); 

     newsList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       News currentNews = mAdapter.getItem(position); 
       Uri uri = Uri.parse(currentNews.getUrl()); 

       CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); 

       builder.setToolbarColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary)); 
       builder.setSecondaryToolbarColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimaryDark)); 

       builder.setShowTitle(true); 

       final Bitmap backButton = BitmapFactory.decodeResource(getResources(), R.drawable.ic_arrow_back_white_24dp); 
       builder.setCloseButtonIcon(backButton); 

       CustomTabsIntent customTabsIntent = builder.build(); 
       customTabsIntent.launchUrl(MainActivity.this, uri); 

      } 
     }); 

     updateNewsList(); 


    } 

    private void updateNewsList(){ 
     ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 

     NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 

     if(networkInfo != null && networkInfo.isConnected()){ 
      LoaderManager loaderManager = getLoaderManager(); 

      loaderManager.initLoader(NEWS_LOADER_ID, null, this); 
     } 

     else { 
      View loadingIndicator = findViewById(R.id.loading_indicator); 
      loadingIndicator.setVisibility(View.GONE); 

      mEmptyView.setText(R.string.no_internet); 
     } 
    } 

    @Override 
    public Loader<List<News>> onCreateLoader(int i, Bundle bundle) { 
     // Create a new loader for the given URL 
     return new NewsLoader(this, GUARDIAN_REQUEST_URL); 
    } 

    @Override 
    public void onLoadFinished(Loader<List<News>> loader, List<News> news) { 
     View loadingIndicator = findViewById(R.id.loading_indicator); 
     loadingIndicator.setVisibility(View.GONE); 

     mEmptyView.setText(R.string.no_news); 

     mAdapter.clear(); 

     if (news != null && !news.isEmpty()) { 
      mAdapter.addAll(news); 
     } 
    } 

    @Override 
    public void onLoaderReset(Loader<List<News>> loader) { 
     // Loader reset, so we can clear out our existing data. 
     mAdapter.clear(); 
    } 


    @Override 
    public void onRefresh() { 
     swipeRefreshLayout.setRefreshing(true); 
     updateNewsList(); 
     swipeRefreshLayout.setRefreshing(false); 

    } 
} 
+0

如何刷新'refreshList();'方法中的新闻列表? – mani

+0

不帮助兄弟。 – mani

+0

请看最新的变化。 –