我使用AQuery在recycleview中从API获取数据,但现在我想在recyclerview项目上打开来自API onclick的片段,以便我如何实现它。 我想和Instagram应用程序一样,就像在主页上点击名称一样,我们可以获得用户在另一个片段上的所有细节。
答案 0 :(得分:2)
//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
<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适配器)
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);
}
}
您也可以在Activity中使用它,以便在回收站视图项中轻松实现多个窗口小部件点击:)
答案 1 :(得分:0)
在回收站视图项目中执行以下操作:
CREATE_NEW_PROCESS_GROUP
这里,fm是FragmentManager对象,只有你可以进行片段事务,例如加载一个新的片段。
yfObj是要加载的片段类的对象。
R.id.fragmentContainer是您在XML文件中声明的容器布局的ID,您要在其中加载片段。
希望这有帮助!