简体   繁体   English

从ApI打开Fragment OnClick on Recyclerview项目

[英]Open Fragment OnClick on Recyclerview item from ApI

I am getting data from API in recycleview using AQuery but now i want to open fragment from API onclick on recyclerview item so how can i implement this. 我正在使用AQuery在recycleview中从API获取数据,但是现在我想从API onclick上打开recyclerview项目的片段,那么我该如何实现这一点。 I want to do same as Instagram app,like on home page when we click on name we get all the details of users on another fragment. 我想做与Instagram应用程序相同的操作,就像在主页上单击名称时一样,在另一个片段上获取用户的所有详细信息。

//CouponFragment.java
public class CouponFragment extends Fragment implements CouponList.OnActionCompleted{
    private RecyclerView recyclerView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        recyclerView = new RecyclerView(getActivity());
        return recyclerView;
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ArrayList<String> coupons = new ArrayList<>();
        coupons.add("Tamil");
        coupons.add("English");
        coupons.add("Malay");
        coupons.add("Chinese");
        recyclerView.setAdapter(new CouponList(coupons,CouponFragment.this));
    }
    @Override
    public void OnClick(Coupon coupon){
        //new fragment
        CouponDetails couponDetails  = new CouponDetails();
        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.home_container, couponDetails);
        //R.id.home_container  is your FrameLayout id
        transaction.addToBackStack("couponDetails");
        transaction.commit();
    }
}

cardview_coupon_info.xml cardview_coupon_info.xml

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/tools"
    android:foreground="?android:attr/selectableItemBackground"
    android:transitionName="coupon_info_card"
    android:id="@+id/coupon_info_card"
    android:clickable="true"
    android:layout_margin="@dimen/item_margin"
    card_view:cardElevation="6dp"
    card_view:cardCornerRadius="4dp">

    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/coupon_description"
            android:gravity="start"
            android:layout_margin="4dp" />

</android.support.v7.widget.CardView>

CouponList.java (Recyclerview adapter) CouponList.java(Recyclerview适配器)

public class CouponList extends RecyclerView.Adapter<CouponList.ViewHolder> {
    private ArrayList<String> coupons;
    private OnActionCompleted callback;

    public CouponList(ArrayList<String> coupons,OnActionCompleted callback)
    {
        this.coupons = coupons;
        this.callback = callback;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_coupon_info,parent,false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.description.setText(coupons.get(position);
    }

    @Override
    public int getItemCount() {
        return coupons.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView description;
        public ViewHolder(View itemView) {
            super(itemView);
            description = (TextView) itemView.findViewById(R.id.coupon_description);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            String coupon = coupons.get(getAdapterPosition());
            callback.OnClick(coupon);
        }

    }

    public interface OnActionCompleted {
        public void OnClick(Coupon coupon);
    }
}

You can use this in Activity as well for easy implementation for multiple widget clicks in recycler view item :) 您也可以在“活动”中使用它,以便在回收器视图项目中轻松实现多个小部件单击:)

Do the following in your recycler view item onClick. 在回收者视图项目onClick中执行以下操作。

FragmentManager fm = getFragmentManager();// If you're in an activity.
FragmentManager fm = getSupportFragmentManager();// If you're already inside another fragment
YourFragment yfObj = new YourFragment();
fm.beginTransaction().replace(R.id.fragmentContainer, yfObj).commit();

Here, fm is the FragmentManager object, with which only you can conduct a fragment transaction, such as loading a new fragment. 在这里,fm是FragmentManager对象,只有您可以使用它执行片段事务,例如加载新片段。

yfObj is the object of the fragment class that you want to load. yfObj是要加载的片段类的对象。

R.id.fragmentContainer is the id of the container layout you have declared in your XML file, where you want to load the fragment. R.id.fragmentContainer是您在XML文件中声明的容器布局的ID,您想在其中加载片段。

Hope this helps ! 希望这可以帮助 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM