簡體   English   中英

我想將 Fragment 類中的變量用於 Home 類

[英]I want to use variable in Fragment class to Home class

我想在片段類中使用 postRecyclerView 變量到家庭類..

因為我想在添加帖子時滾動位置。 但是在家庭課上我必須定義recyclerview。 所以我計划使用片段類中的 recyclerview 變量。

這是家庭類代碼。

myRef.setValue(post).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {

            popupClickProgress.setVisibility(View.INVISIBLE);
            popupAddBtn.setVisibility(View.VISIBLE);
            // illegal 오류 해결 수정
            if(popAddPost!=null&&popAddPost.isShowing()){
                popAddPost.dismiss();
            }
            FragmentManager fm = getSupportFragmentManager();
            HomeFragment frag1 = (HomeFragment)fm.findFragmentById(R.id.postRecylerView);
            frag1.



        }
    });

並在片段類中

public class HomeFragment extends Fragment {



private OnFragmentInteractionListener mListener;
private int position;
private int ant;


RecyclerView postRecyclerView ;
PostAdapter postAdapter;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
List<Post> postList;


public HomeFragment() {
    // Required empty public constructor
}

public static HomeFragment newInstance(String param1, String param2) {
    HomeFragment fragment = new HomeFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    LinearLayoutManager lin = new LinearLayoutManager(getActivity());
    lin.setStackFromEnd(true);
    lin.setReverseLayout(true);
    View fragmentView = inflater.inflate(R.layout.fragment_home, container, false);
    postRecyclerView = fragmentView.findViewById(R.id.postRV);
    postRecyclerView.setLayoutManager(lin);
    postRecyclerView.setHasFixedSize(true);
    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference("Posts");
    return fragmentView;
}

我不知道為什么我不能接近 postRecyclerView 變量??

你能給我建議嗎?

因此,根據我的理解,您正試圖將該變量(From Home.java)的值傳遞到位於片段內的回收器視圖上? 是這樣嗎?

您要做的是在類之間傳遞數據。 我對 Java 有點生疏,所以這可能不是最好的方法,但請試一試並回復我。

class yourClass {

    private myClassA classAInstance;

    public yourClass(myClassA a)
    {
        classAInstance = a;
    }

    void I_AM_a_button_press()  // function
    {
        classAInstance.printVariable();
    }
}

我還建議您查看有關此問題的博客文章,它將對您有很大幫助。 祝你好運。

http://www.thetopsites.net/article/54159971.shtml

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM