Привет, появилась проблема не могу передать изображение во фрагмент. У меня есть адаптер RecyclerView
ProductAdapter
, есть Фрагмент в котором собственно создается список(заголовок и картинка),есть класс Product
и есть фрагмент, который будет отображать нажатую картинку.не могу понять как и откуда вытягивать определенную картинку. Думаю что из адаптера но сама картинка задается во фрагменте.
Это код самого адаптера
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {
private Context mCtx;
private List<Product> productList;
public ProductAdapter(Context mCtx, List<Product> productList) {
this.mCtx = mCtx;
this.productList = productList;
}
private Fragment fragment;
@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.list_layout, null);
return new ProductViewHolder(view);
}
@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
Product product = productList.get(position);
holder.textViewTitle.setText(product.getTitle());
holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(product.getImage()));
}
@Override
public int getItemCount() {
return productList.size();
}
public class ProductViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textViewTitle, textViewShortDesc, textViewRating, textViewPrice;
Button buttonImage;
public ImageView getImageView() {
return imageView;
}
public void setImageView(ImageView imageView) {
this.imageView = imageView;
}
public ProductViewHolder(final View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.textViewTitle);
imageView = itemView.findViewById(R.id.imageView);
buttonImage = itemView.findViewById(R.id.buttonImage);
buttonImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentBigPicture fragmentBigPicture = new FragmentBigPicture();
FragmentManager fragmentManager = ((Activity) mCtx).getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment)
.commit();
}
});
}
}
}
это код класса
public class Product {
//private int id;
private String title;
/* private String shortdesc;
private double rating;
private double price;*/
private int image;
public String getTitle() {
return title;
}
public Product(int image) {
this.image = image;
}
public Product(String title, int image) {
this.title = title;
this.image = image;
}
public int getImage() {
return image;
}}
Это код фрагмента (не обращайте внимания на реализацию создания списка, она будет осуществлена с помощью "парсинга" сайта)
public class FragmentImport extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
List<Product> productList;
//the recyclerview
RecyclerView recyclerView;
private OnFragmentInteractionListener mListener;
public FragmentImport() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FragmentImport.
*/
// TODO: Rename and change types and number of parameters
public static FragmentImport newInstance(String param1, String param2) {
FragmentImport fragment = new FragmentImport();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_import, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
productList = new ArrayList<>();
productList.add(
new Product("Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra)",
R.drawable.macbook));
productList.add(
new Product(
"Dell Inspiron 7000 Core i5 7th Gen - (8 GB/1 TB HDD/Windows 10 Home)",
R.drawable.dellinspiron));
productList.add(
new Product(
"Microsoft Surface Pro 4 Core m3 6th Gen - (4 GB/128 GB SSD/Windows 10)",
R.drawable.surface));
ProductAdapter adapter = new ProductAdapter(getActivity(), productList);
recyclerView.setAdapter(adapter);
return view;
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
Это код фрагмента который будет изображение вытягивать
public class FragmentBigPicture extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
ImageView imageView2;
private OnFragmentInteractionListener mListener;
public FragmentBigPicture() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FragmentBigPicture.
*/
// TODO: Rename and change types and number of parameters
public static FragmentBigPicture newInstance(String param1, String param2) {
FragmentBigPicture fragment = new FragmentBigPicture();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_big_picture, container, false);
imageView2 = (ImageView) view.findViewById(R.id.imageView2);
FragmentImport fImport = new FragmentImport();
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}}
Очень надеюсь, что мне сможете помочь, несколько дней думал как это решить.
getAdapterPosition()
и...