how to create context menu in RecyclerView
using addOnItemTouchListener
when user long click?
I have code like this
public class AllDataFragment extends Fragment{
List<GetDataAdapterRiwayat> GetDataAdapter1;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewlayoutManager;
private static final String TAG = MainActivity.class.getSimpleName();
RecyclerView.Adapter recyclerViewadapterRiwayat;
String FIXURL = "http://192.168.43.139/AndroidFileUpload/";
String GET_JSON_DATA_HTTP_URL = FIXURL+"GetAllData.php";
String JSON_ID_TEMPAT_PERCETAKAN = "id_tempat_percetakan";
String JSON_NAMA_PERCETAKAN = "nama_percetakan";
String JSON_LATITUDE = "latitude";
String JSON_LONGITUDE = "longitude";
String JSON_GAMBAR = "gambar";
String JSON_STATUS = "status";
String JSON_EMAIL1 = "email1";
String JSON_EMAIL2 = "email2";
String JSON_EMAIL3 = "email3";
SessionManager session;
RequestQueue requestQueue ;
String email;
public AllDataFragment() {
// Required empty public constructor
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GetDataAdapter1 = new ArrayList<>();
recyclerView = (RecyclerView) getView().findViewById(R.id.recyclerview2);
recyclerView.setHasFixedSize(true);
recyclerViewlayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(recyclerViewlayoutManager);
JSON_DATA_WEB_CALL();
Log.d(TAG, "acoba nih1 : ");
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity().getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
@Override
public void onClick(View view, int position) {
GetDataAdapterRiwayat GetDataAdapterRiwayat = GetDataAdapter1.get(position);
Log.d(TAG, "acoba nih3 : ");
Intent myIntent = new Intent(getActivity(), DetailRiwayatActivity.class);
myIntent.putExtra("Gambar",GetDataAdapterRiwayat.getGambar());// getDataAdapter1 in your case
myIntent.putExtra("IdTempatPercetakan",GetDataAdapterRiwayat.getIdTempatPercetakan());// getDataAdapter1 in your case
myIntent.putExtra("NamaPercetakan",GetDataAdapterRiwayat.getNamaPercetakan());// getDataAdapter1 in your case
myIntent.putExtra("Latitude",GetDataAdapterRiwayat.getLatitude());// getDataAdapter1 in your case
myIntent.putExtra("Longitude",GetDataAdapterRiwayat.getLongitude());// getDataAdapter1 in your case
myIntent.putExtra("Status",GetDataAdapterRiwayat.getStatus());// getDataAdapter1 in your case
myIntent.putExtra("Email1",GetDataAdapterRiwayat.getEmail1());// getDataAdapter1 in your case
myIntent.putExtra("Email2",GetDataAdapterRiwayat.getEmail2());// getDataAdapter1 in your case
myIntent.putExtra("Email3",GetDataAdapterRiwayat.getEmail3());// getDataAdapter1 in your case
startActivity(myIntent);
}
@Override
public void onLongClick(View view, int position) {
}
}));
}
@Override
public boolean onMenuItemClick(MenuItem item) {
// Menu Item Clicked!
return true;
}
public void JSON_DATA_WEB_CALL() {
setSessionManager();
CustomJsonArrayRequest request = new CustomJsonArrayRequest (GET_JSON_DATA_HTTP_URL + "?&email="+email,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSON_PARSE_DATA_AFTER_WEBCALL(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(request);
}
public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){
for(int i = 0; i<array.length(); i++) {
GetDataAdapterRiwayat GetDataAdapter2 = new GetDataAdapterRiwayat();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetDataAdapter2.setIdTempatPercetakan(json.getString(JSON_ID_TEMPAT_PERCETAKAN));
GetDataAdapter2.setNamaPercetakan(json.getString(JSON_NAMA_PERCETAKAN));
GetDataAdapter2.setLatitude(json.getString(JSON_LATITUDE));
GetDataAdapter2.setLongitude(json.getString(JSON_LONGITUDE));
GetDataAdapter2.setGambar(json.getString(JSON_GAMBAR));
GetDataAdapter2.setStatus(json.getString(JSON_STATUS));
GetDataAdapter2.setEmail1(json.getString(JSON_EMAIL1));
GetDataAdapter2.setEmail2(json.getString(JSON_EMAIL2));
GetDataAdapter2.setEmail3(json.getString(JSON_EMAIL3));
} catch (JSONException e) {
e.printStackTrace();
}
GetDataAdapter1.add(GetDataAdapter2);
}
recyclerViewadapterRiwayat = new RecyclerViewAdapterRiwayat(GetDataAdapter1, getActivity());
recyclerView.setAdapter(recyclerViewadapterRiwayat);
}
private void setSessionManager() {
session = new SessionManager(getActivity().getApplicationContext());
session.checkLogin();
// get user data from session
HashMap<String, String> user = session.getUserDetails();
// email
email = user.get(SessionManager.KEY_EMAILUSER);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_all_data, container, false);
}
@Override
public void onClick(View v) {
}
}