Я использую фрагменты (frag_inventory.xml), и внутри них 2 фрагмента, как вы можете видеть на картинке ниже.Левая часть - это фрагмент с RecycleView.
Что я хочу сделать, так это то, что когда я выбираю опцию из RecycleView из опций, скажем, Categories - это то, что CategoryFragment будет отображатьсяна правой стороне фрагмента.
У меня есть следующий код, работающий в прошлом с ListView (см. фрагмент ниже).Но когда я добавляю его в свой InventoryRecyclerViewAdapter.java (из я хочу вставить код интервал), getFragmentManager()
станет красным.
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (position){
case 0:
ProductsFragment productsFragment = new ProductsFragment();
fragmentTransaction.replace(R.id.inventorylist_fragmentcontainer, productsFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
Не могли бы вы посоветовать мнена что делать с этим?Кроме того, я хочу, чтобы выбранный RecycleView выделялся при нажатии.Спасибо
InventoryRecyclerViewAdapter.java
public class InventoryRecyclerViewAdapter extends RecyclerView.Adapter<InventoryRecyclerViewAdapter.ViewHolder>{
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<Integer> mIcon = new ArrayList<>();
private ArrayList<String> mLabel = new ArrayList<>();
private Context context;
public InventoryRecyclerViewAdapter(Context context, ArrayList<Integer> mIcon, ArrayList<String> mLabel) {
this.mIcon = mIcon;
this.mLabel = mLabel;
this.context = context;
}
//responsible for inflating the view
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.customlayout_inventorylist, viewGroup, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull InventoryRecyclerViewAdapter.ViewHolder viewHolder, final int i) {
Log.d(TAG, "onBindViewHolder: called.");
Glide.with(context)
.asBitmap()
.load(mIcon.get(i))
.into(viewHolder.icon);
viewHolder.label.setText(mLabel.get(i));
viewHolder.customLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
----------------------------------------
I want to insert a code here
----------------------------------------
});
}
@Override
public int getItemCount() {
return mLabel.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
ImageView icon;
TextView label;
LinearLayout customLayout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
icon = itemView.findViewById(R.id.inventorylist_icon);
label = itemView.findViewById(R.id.inventorylist_title);
customLayout = itemView.findViewById(R.id.inventoryoptions_layout);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
}
}
public interface onInventoryListener{
void onInventoryClick(int position);
}
InventoryListFragment.java
public class InventoryListFragment extends Fragment implements InventoryRecyclerViewAdapter.onInventoryListener{
private static final String TAG = "InventoryListFragment";
//variables
private ArrayList<Integer> mIcon = new ArrayList<>();
private ArrayList<String> mLabel = new ArrayList<>();
public InventoryListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_inventory_list, container, false);
Log.d(TAG, "onCreateView: started");
initImageBitmaps(view);
return view;
}
private void initImageBitmaps(View view){
Log.d(TAG, "initImageBitmaps: preparing bitmaps");
mIcon.add(R.drawable.ic_product);
mLabel.add("Products");
mIcon.add(R.drawable.ic_customer);
mLabel.add("Services");
mIcon.add(R.drawable.ic_category);
mLabel.add("Categories");
mIcon.add(R.drawable.ic_tag);
mLabel.add("Discounts");
initRecyclerView(view);
}
private void initRecyclerView(View view){
RecyclerView recyclerView = view.findViewById(R.id.inventorylist_recycleview);
recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
.build()); //adding a divider into the recyclerview list
InventoryRecyclerViewAdapter adapter = new InventoryRecyclerViewAdapter(getActivity(), mIcon, mLabel);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
//method created from OnInventoryListener from InventoryListFragment.java
//handles the onclick for the recycleview items
@Override
public void onInventoryClick(int position) {
}
фрагментinventory.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.InventoryFragment"
android:orientation="horizontal"
android:id="@+id/inventory_content">
<fragment
android:id="@+id/inventorylist_fragment"
android:name="com.example.devcash.Fragments.InventoryListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/fragment_inventory_list">
</fragment>
<View
style="@style/Divider"
android:layout_width="1dp"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/inventorylist_fragmentcontainer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"/>
</LinearLayout>