单击RecycleView中的项目时,如何在片段内调用和显示片段?

时间:2019-07-07 06:58:13

标签: android android-fragments

我正在使用片段(fragment_inventory.xml),里面是2个片段,如下图所示。左侧是带有RecycleView的片段。

我想做的是,当我从RecycleView中的选项中单击一个选项时,假设 Categories 是CategoryFragment将显示在Fragment的右侧。

我有以下这段代码与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在单击时突出显示。谢谢

enter image description here 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) {

}

fragmentinventory.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>

2 个答案:

答案 0 :(得分:0)

您必须使用将在适配器的构造函数中传递的方法来创建接口。

interface OnItemClickListener {
    void onClick(SomeData data);
}

在recyclerView的构造函数中,添加对此的引用

public InventoryRecyclerViewAdapter(Context context, ArrayList<Integer> mIcon, ArrayList<String> mLabel, OnItemClickListener listener) {
    this.mIcon = mIcon;
    this.mLabel = mLabel;
    this.context = context;
    this.listener = listener;
}

在viewHolder中,设置一个onClickListener(将android。*作为软件包的一个)

viewHolder.customLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.onClick(/* PASS YOUR DATA HERE */);
});

现在,在您的活动或片段中创建适配器时,实现侦听器并覆盖onClick(SomeData data)方法。现在,从那里调用片段管理器,它将起作用。

答案 1 :(得分:0)

已经解决了这个问题。

代替这个

FragmentManager fragmentManager = getFragmentManager();

我刚刚做到了

`FragmentManager fragmentManager = ((AppCompatActivity)v.getContext()).getSupportFragmenteManager(`);