6

हाय मेरे पास एक टैबड गतिविधि है, और पहले टैब में मैं सर्वर से डेटा प्राप्त करता हूं और इसे कार्ड दृश्यों के भीतर रीसाइक्लर्व में दिखाता हूं। सर्वर से डेटा लाने के लिए मैं वॉली लाइब्रेरी का उपयोग करता हूं। मैं डेटा लोड करने के लिए रीफ्रेश करने के लिए पुल को कार्यान्वित करना चाहता हूं (इसलिए जब भी मैं इसे खींचूं नेटवर्क को अनुरोध करना है)। और जब भी मैं टैब के बीच स्विच करता हूं, तब भी मैं नेटवर्क अनुरोध को अक्षम करना चाहता हूं (क्योंकि जब मैं अपने ऐप में टैब फोकस बदलता हूं तो यह डेटा लाने शुरू होता है) मैं नेटवर्क अनुरोध केवल 1 बार करना चाहता हूं (जब उपयोगकर्ता पहली बार लॉग इन करता है) और फिर दूसरों को ताज़ा करने के लिए केवल खींचकर अनुरोध किया जाता है।रीसाइक्लर्विव एंड्रॉइड रीफ्रेश करने के लिए खींचें

यहाँ मेरी टुकड़ा जहाँ मैं recyclerview है और डेटा बताते है:

public class Tab1History extends Fragment 
{ 

private RecyclerView recyclerView; 
private CespiteAdapter adapter; 
UserSessionManager session; 

private static final String URL_DATA = "http://mydata.php"; 

private List<CespiteOgg> cespiteOggList; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) 
{ 

    View rootView = inflater.inflate(R.layout.tab1history, container, false); 

    recyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view); 
    recyclerView.setHasFixedSize(true);//every item of the RecyclerView has a fix size 
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 

    cespiteOggList = new ArrayList<>(); 

    loadRecyclerViewData(); 

      return rootView; 
} 

private void loadRecyclerViewData() 
{ 
    // Session class instance 
    session = new UserSessionManager(getActivity()); 
    //get user data from session 
    HashMap<String, String> user = session.getUserDetails(); 
    //get name 
    String name = user.get(UserSessionManager.KEY_NAME); 
    // get username 
    final String usernameUtente = user.get(UserSessionManager.KEY_USERNAME); 

    final ProgressDialog progressDialog = new ProgressDialog(getActivity()); 
    progressDialog.setMessage("Carimento dati..."); 
    progressDialog.show(); 

    StringRequest stringRequest = new StringRequest(Request.Method.POST, 
      URL_DATA, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String s) { 
        progressDialog.dismiss(); 
        try { 
         JSONObject jsonObject = new JSONObject(s); 
         JSONArray array = jsonObject.getJSONArray("dates"); 

         for(int i=0; i<array.length(); i++) 
         { 
          JSONObject o = array.getJSONObject(i); 
          CespiteOgg item = new CespiteOgg(
            o.getString("CodNumInventario"), 
            o.getString("Nome"), 
            o.getString("DtCatalogazione"), 
            o.getString("CodIdA"), 
            o.getString("username") 
          ); 
          cespiteOggList.add(item); 
         } 

         adapter = new CespiteAdapter(cespiteOggList, getActivity()); 
         recyclerView.setAdapter(adapter); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        progressDialog.dismiss(); 
       } 
      }) 
      { 
       @Override 
       protected Map<String, String> getParams() throws AuthFailureError { 
        Map<String, String> params = new HashMap<String, String>(); 
        params.put("Username", usernameUtente); 
        return params; 
       } 
      }; 

    RegisterRequest.getmInstance(getActivity()).addToRequestque(stringRequest); 

} 
} 

और यह एडाप्टर है:

public class CespiteAdapter extends RecyclerView.Adapter<CespiteAdapter.ViewHolder> 
{ 


private List<CespiteOgg> cespiteOggList; 
private Context context; 

public CespiteAdapter(List<CespiteOgg> cespiteOggList, Context context) { 

    this.cespiteOggList = cespiteOggList; 
    this.context = context; 
} 

public class ViewHolder extends RecyclerView.ViewHolder 
{ 

    public CardView cv; 
    public TextView txtNumInventario; 
    public TextView txtNomeCespite; 
    public TextView txtDtCatalogazione; 
    public TextView txtAula; 
    public TextView txtNomeUser; 


    ViewHolder(View itemView) 
    { 

     super (itemView); 
     //cv = (CardView) itemView.findViewById(R.id.cardView); 
     txtNumInventario = (TextView) itemView.findViewById(R.id.txtNumeroInventario); 
     txtNomeCespite = (TextView) itemView.findViewById(R.id.txtNomeCespite); 
     txtDtCatalogazione = (TextView) itemView.findViewById(R.id.txtDataCatalogazione); 
     txtAula = (TextView) itemView.findViewById(R.id.txtAula); 
     txtNomeUser= (TextView) itemView.findViewById(R.id.txtNomeUser); 


    } 
} 



@Override 
public CespiteAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
{ 

    LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 

    View cespiteView = inflater.inflate(R.layout.cespite_card_view, parent, false); 

    return new ViewHolder(cespiteView); 
} 

@Override 
public void onBindViewHolder(ViewHolder holder, int position) 
{ 

    CespiteOgg cespiteOgg = cespiteOggList.get(position); 

    holder.txtNumInventario.setText(cespiteOgg.getNumInventario()); 
    holder.txtNomeCespite.setText(cespiteOgg.getNomeCespite()); 
    holder.txtDtCatalogazione.setText(cespiteOgg.getDtCatalogazione()); 
    holder.txtAula.setText(cespiteOgg.getAula()); 
    holder.txtNomeUser.setText(cespiteOgg.getNomeUser()); 

} 

@Override 
public int getItemCount() 
{ 

    return cespiteOggList.size(); 
} 

} 
+0

जांच के लिए इस लिंक https://developer.android.com/ पूर्ण कोड प्राप्त प्रशिक्षण/कड़ी चोट/एड-स्वाइप-interfa ce.html –

उत्तर

15

आप ProgressDialog के बजाय एंड्रॉयड SwipeRefreshLayout विजेट का उपयोग कर सकते हैं


इस सवाल का जवाब दिया गया।

नीचे दिए गए चरणों का पालन करें अपने Tab1history टुकड़ा में SwipeRefreshLayout एकीकृत करने के लिए:

1. अपने लेआउट tab1history में, एक रूट लेआउट के रूप में जोड़ SwipeRefreshLayout और RecyclewrView इसके अंदर जगह।

// tab1history.xml 
<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.SwipeRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/swipe_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/my_recycler_view" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

</android.support.v4.widget.SwipeRefreshLayout> 

2. अपने Tab1History टुकड़ा में, सर्वर से डेटा लोड करने के रूप में नीचे SwipeRefreshLayout का उपयोग करें:

// Tab1History.java 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.widget.SwipeRefreshLayout; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.android.volley.Request; 
import com.android.volley.Response; 
import com.android.volley.toolbox.StringRequest; 

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

import java.util.ArrayList; 


public class Tab1History extends Fragment implements SwipeRefreshLayout.OnRefreshListener { 

    SwipeRefreshLayout mSwipeRefreshLayout; 
    private RecyclerView recyclerView; 
    private CespiteAdapter adapter; 
    UserSessionManager session; 

    private static final String URL_DATA = "http://mydata.php"; 

    private List<CespiteOgg> cespiteOggList; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) 
    { 

     View rootView = inflater.inflate(R.layout.tab1history, container, false); 


     recyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view); 
     recyclerView.setHasFixedSize(true);//every item of the RecyclerView has a fix size 
     recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 

     cespiteOggList = new ArrayList<>(); 


     // SwipeRefreshLayout 
     mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container); 
     mSwipeRefreshLayout.setOnRefreshListener(this); 
     mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary, 
       android.R.color.holo_green_dark, 
       android.R.color.holo_orange_dark, 
       android.R.color.holo_blue_dark); 

     /** 
     * Showing Swipe Refresh animation on activity create 
     * As animation won't start on onCreate, post runnable is used 
     */ 
     mSwipeRefreshLayout.post(new Runnable() { 

      @Override 
      public void run() { 

       mSwipeRefreshLayout.setRefreshing(true); 

       // Fetching data from server 
       loadRecyclerViewData(); 
      } 
     }); 

     return rootView; 
    } 

    /** 
    * This method is called when swipe refresh is pulled down 
    */ 
    @Override 
    public void onRefresh() { 

     // Fetching data from server 
     loadRecyclerViewData(); 
    } 

    private void loadRecyclerViewData() 
    { 
     // Showing refresh animation before making http call 
     mSwipeRefreshLayout.setRefreshing(true); 

     // Session class instance 
     session = new UserSessionManager(getActivity()); 
     //get user data from session 
     HashMap<String, String> user = session.getUserDetails(); 
     //get name 
     String name = user.get(UserSessionManager.KEY_NAME); 
     // get username 
     final String usernameUtente = user.get(UserSessionManager.KEY_USERNAME); 


     StringRequest stringRequest = new StringRequest(Request.Method.POST, 
       URL_DATA, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String s) { 

         try { 
          JSONObject jsonObject = new JSONObject(s); 
          JSONArray array = jsonObject.getJSONArray("dates"); 

          for(int i=0; i<array.length(); i++) 
          { 
           JSONObject o = array.getJSONObject(i); 
           CespiteOgg item = new CespiteOgg(
             o.getString("CodNumInventario"), 
             o.getString("Nome"), 
             o.getString("DtCatalogazione"), 
             o.getString("CodIdA"), 
             o.getString("username") 
           ); 
           cespiteOggList.add(item); 
          } 

          adapter = new CespiteAdapter(cespiteOggList, getActivity()); 
          recyclerView.setAdapter(adapter); 

         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 

         // Stopping swipe refresh 
         mSwipeRefreshLayout.setRefreshing(false); 

        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 

         // Stopping swipe refresh 
         mSwipeRefreshLayout.setRefreshing(false); 
        } 
       }) 
     { 
      @Override 
      protected Map<String, String> getParams() throws AuthFailureError { 
       Map<String, String> params = new HashMap<String, String>(); 
       params.put("Username", usernameUtente); 
       return params; 
      } 
     }; 

     RegisterRequest.getmInstance(getActivity()).addToRequestque(stringRequest); 

    } 
} 

आशा इस ठीक से काम करेंगे।

+0

धन्यवाद आदमी, एक आकर्षण की तरह काम करता है! – arst3k

0

सूची दृश्य को रीफ्रेश करने के लिए खींचें & एंड्रॉइड स्टूडियो में रीसाइक्लर देखें उदाहरण - SwipeRefreshLayout.I आशा है कि यह कोड ठीक से काम करेगा आप इस कोड को आजमा सकते हैं।

बेसिक पुल/SwipeRefreshLayout XML कोड को रिफ्रेश करने के लिए:

<android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/simpleSwipeRefreshLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     < Add View's Here..../> 
</android.support.v4.widget.SwipeRefreshLayout> 

नीचे आप बुनियादी पुल के कदम विवरण द्वारा कोड डाउनलोड कर सकते हैं, को देखने के अंतिम आउटपुट और चरण उदाहरण ताज़ा करने के लिए।उदाहरण के साथ http://abhiandroid.com/materialdesign/pulltorefresh

0

स्रोत कोड

https://drive.google.com/open?id=1qjJ_to-1knVNaJB4T3U_L_p_YYNvgAeZ

APK

https://drive.google.com/open?id=1MxQZwjIXgR2jgDkUW1mbFrTLMSaQQisC

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener { 


    SwipeRefreshLayout mSwipeRefreshLayout; 


     // SwipeRefreshLayout 
     mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container); 

     mSwipeRefreshLayout.setOnRefreshListener(this); 
//  mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary, 
     mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, 
       android.R.color.holo_green_dark, 
       android.R.color.holo_orange_dark, 
       android.R.color.holo_blue_dark); 

     /** 
     * Showing Swipe Refresh animation on activity create 
     * As animation won't start on onCreate, post runnable is used 
     */ 
     mSwipeRefreshLayout.post(new Runnable() { 

      @Override 
      public void run() { 

       if(mSwipeRefreshLayout != null) { 
        mSwipeRefreshLayout.setRefreshing(true); 
       } 
       // TODO Fetching data from server 
       fetchContacts(); 
      } 
     }); 



    @Override 
    public void onRefresh() { 
     fetchContacts(); 
    } 
संबंधित मुद्दे