gpt4 book ai didi

java - 从 android studio 中的 onCreateview 调用新方法中的 View

转载 作者:行者123 更新时间:2023-12-01 18:09:55 27 4
gpt4 key购买 nike

我试图用我的新方法从我的充气机中调用“ View ”,但它不起作用,它说未解决。如果我将 setUpRecyclerView 下的整个代码移动到 onCreateView 中,它就可以工作。有没有办法让它按原样工作,这样我就可以保持 onCreatView 整洁?所有代码都在一个 fragment 中。我也尝试过使用 getView.findViewById 但这也不起作用。

'''

package com.example.hostapp;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;

public class HomeFragment extends Fragment {

private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference categoryref = db.collection("categories");
private categoryCardAdapter adapter;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);




return view;
}

public void setUpRecyclerView (){
Query query = categoryref.orderBy("name",Query.Direction.DESCENDING);

FirestoreRecyclerOptions<categoryCard> options = new FirestoreRecyclerOptions.Builder<categoryCard>()
.setQuery(query, categoryCard.class)
.build();

adapter = new categoryCardAdapter(options);

RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_category);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
recyclerView.setAdapter(adapter);
}

@Override
public void onStart() {
super.onStart();
adapter.startListening();
}

@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
}

'''

这是有效的代码:

package com.example.hostapp;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;

public class HomeFragment extends Fragment {

private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference categoryref = db.collection("categories");
private categoryCardAdapter adapter;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);


Query query = categoryref.orderBy("name",Query.Direction.DESCENDING);

FirestoreRecyclerOptions<categoryCard> options = new FirestoreRecyclerOptions.Builder<categoryCard>()
.setQuery(query, categoryCard.class)
.build();

adapter = new categoryCardAdapter(options);

**RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_category);**
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
recyclerView.setAdapter(adapter);

return view;
}

public void setUpRecyclerView (){

}

@Override
public void onStart() {
super.onStart();
adapter.startListening();
}

@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
}

最佳答案

您没有在第一个代码示例中调用 setupRecyclerView() 方法。您可以在返回 View 之前从 createView() 方法调用它,也可以从 onViewCreated() 方法调用该方法。我更喜欢第二种选择。

关于java - 从 android studio 中的 onCreateview 调用新方法中的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60485866/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com