[英]Pass recycler item from one fragment to another
我必須將從數據庫中獲得的回收者視圖項目傳遞給另一個片段。
public class FragLoad extends Fragment {
EditText g_type;
EditText l_capacity, date, quotation;
Button search;
RecyclerView recyclerView;
List<RPostLoad> rPostLoads;
SupportPlaceAutocompleteFragment source,destination;
private static final String URL_RecyclerLoad="http://192.168.43.38/internship_project/android_web_services/RecyclerLoad.php";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_load, container, false);
g_type=view.findViewById(R.id.goodstype);
l_capacity=view.findViewById(R.id.loadCap);
date=view.findViewById(R.id.txtDate);
quotation=view.findViewById(R.id.txtQuotations);
search=view.findViewById(R.id.postSearch);
recyclerView=view.findViewById(R.id.rvload);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
recyclerView.setHasFixedSize(true);
rPostLoads=new ArrayList<>();
source= (SupportPlaceAutocompleteFragment) getChildFragmentManager().findFragmentById(R.id.place_autocomplete_fragment2);
destination= (SupportPlaceAutocompleteFragment) getChildFragmentManager().findFragmentById(R.id.place_autocomplete_fragment1);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rvLoad();
}
});
return view;
}
private void rvLoad() {
Map<String, String> params = new HashMap<>();
params.put("g_type", g_type.getText().toString());
AndroidNetworking.post("http://192.168.43.38/internship_project/android_web_services/RecyclerLoad.php").addBodyParameter(params).setTag("Login").setPriority(Priority.LOW).build().getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d("TAG", "Response" + response.toString());
JSONArray array=response.getJSONArray("Data");
for (int i=0;i<array.length();i++){
JSONObject product=array.getJSONObject(i);
rPostLoads.add(new RPostLoad(
product.getString("owner_name"),
product.getString("g_type"),
product.getString("truck_cap")
));
}
RPostLoadAdp adp=new RPostLoadAdp(getActivity(),rPostLoads);
recyclerView.setAdapter(adp);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onError(ANError anError) {
Toast.makeText(getActivity(), "Server time out please try later", Toast.LENGTH_SHORT).show();
}
});
}
}
上面的代碼是從數據庫中獲取回收器項目的類,需要將回收器視圖項目發送到另一個片段。 問題是我是android的初學者,所以我不知道如何發送它。 我必須在其中接收回收站項目的片段為空,因此我沒有在此處發布它。
您有一個Activity
一個Fragment
和一個RecyclerView
然后您的Activity
打開Fragment
然后在片段內部實現RecylcerView
要在Activity
和RecyclerView
之間進行通信,您需要有一個Listener
創建此Interface
(偵聽器):
public interface OnSomeThingHappens {
public void onEvent(); // you can use some argument for the method
}
然后在您的Activity中實現接口並為您的片段創建一個方法,以將偵聽器從Activity傳遞到片段,然后將其從片段傳遞到RecyclerView
的適配器。 您的活動將是這樣的:
public class ActivityMain extends AppCompatActivity implements OnSomeThingHappens{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YourFragment yourFragment = (YourFragment) getFragmentManager().findFragmentById(R.id.test_fragment);
yourFragment.setOnSomeThingListener(this); // this indication the listener that is implemented in ActivityMain
}
@Override
public void onEvent() {
// to do your logic
}
}
並在您的Fragment
當您實現RPostLoadAdp
時,使用adapter中的setter方法將偵聽器對象適配器發送到適配器。
那么您將可以訪問偵聽器,並且可以在需要時調用偵聽器的onEvent()
方法,並在ActivityMain
中調用該方法。 現在,您可以在ActivityMain
此方法中執行邏輯(將數據發送到其他片段或..),希望此幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.