2016-10-25 2 views
0

API에서 recQuery를 사용하여 데이터를 가져오고 있습니다 만, 이제는 API를 사용하여 recyclerview 항목에서 onclick을 열고 싶습니다. 어떻게 구현할 수 있습니까? Instagram 앱과 동일하게하고 싶습니다. 홈 페이지에서 이름을 클릭하면 다른 조각에있는 사용자의 모든 세부 정보를 얻을 수 있습니다.Open Fragment API에서 Recyclerview 항목을 클릭하십시오.

+0

단순히 _FragmentTransaction_을 사용할 수 있습니다. 그러나 어댑터 클래스의 오픈 조각은 권장되는 방법이 아니므로이를 위해 리스너를 구현해야합니다. – Piyush

답변

0

리사이클 러보기 항목 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(); 

, FM은 당신이 이러한 새로운 조각을로드 등의 단편 트랜잭션을 수행 할 수있는과 FragmentManager 객체이다.

yfObj는로드하려는 프래그먼트 클래스의 객체입니다.

R.id.fragmentContainer는 조각을로드하려는 XML 파일에서 선언 한 컨테이너 레이아웃의 ID입니다.

희망이 도움이됩니다.

+0

내가 RecyclerViewAdapter 또는 MainFragment 클래스에 OnClickListner를 넣어야하고 MainFragment에 API를 넣을 위치는 어디입니까? – User16

+0

어댑터에서 리사이클 러 뷰 아이템 레이아웃의 루트 레이아웃의 오브젝트를 생성하고 거기에서 클릭을 처리 할 수 ​​있습니다. –

+0

API의 경우, 액티비티/프래그먼트에서 API를 호출하고 API에서 데이터를로드 할 수 있다고 추측합니다. 그런 다음 해당 데이터를 어댑터 클래스에 공급하여 리사이클 러 뷰를로드하십시오. –

1
//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 당신은 재활용보기 항목에서 여러 위젯을 클릭 쉽게 구현을위한뿐만 아니라 활동이 사용할 수

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); 
    } 
} 

(Recyclerview 어댑터) :)

관련 문제