Java RecylerView没有在碎片中支撑
在我的片段中,recyclerview没有出现。当我尝试在我的活动中填充结果时,正如预期的那样,但是在我的片段中,当我启动recyclerview时,它没有显示空白。 是,我的适配器中有getItemCount(它已在使用同一适配器的othr活动中工作) 我的片段初始化Java RecylerView没有在碎片中支撑,java,android,android-fragments,Java,Android,Android Fragments,在我的片段中,recyclerview没有出现。当我尝试在我的活动中填充结果时,正如预期的那样,但是在我的片段中,当我启动recyclerview时,它没有显示空白。 是,我的适配器中有getItemCount(它已在使用同一适配器的othr活动中工作) 我的片段初始化 public static ProfileSearchFragment newInstance(String searchText,String algoliaKey, String applicationId) {
public static ProfileSearchFragment newInstance(String searchText,String algoliaKey, String applicationId) {
ProfileSearchFragment fragment = new ProfileSearchFragment();
Bundle args = new Bundle();
args.putString(SearchText, searchText);
args.putString(AlgoliaKey, algoliaKey);
args.putString(ApplicationId, applicationId);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mSearchText = getArguments().getString(SearchText);
mAlgoliaKey = getArguments().getString(AlgoliaKey);
mApplicationId = getArguments().getString(ApplicationId);
}
}
已在onCreateView和onViewCreated中进行了尝试:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_profile_search, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RecyclerView recyclerView = view.findViewById(R.id.profile_search_recyclerview);
Log.d(TAG, "onCreateView: getting the Searchtext hopefully " + mSearchText + ":" + mAlgoliaKey);
mUserImage = new ArrayList<>();
mUserName = new ArrayList<>();
mUserId = new ArrayList<>();
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
linearLayoutManager.setSmoothScrollbarEnabled(true);
recyclerView.setLayoutManager(linearLayoutManager);
userSearchAdapter = new UserSearchAdapter(getContext(),mUserImage,mUserName,mUserId);
recyclerView.setAdapter(userSearchAdapter);
final Client client = new Client(mApplicationId, mAlgoliaKey);
final Index index = client.getIndex("Users");
if (mSearchText!=null){
Query query = new Query(mSearchText)
.setAttributesToRetrieve("user_username", "user_image_url", "user_id")
.setHitsPerPage(50);
index.searchAsync(query, new CompletionHandler() {
@Override
public void requestCompleted(JSONObject content, AlgoliaException error) {
try {
JSONArray hits = content.getJSONArray("hits");
List<String> list = new ArrayList<>();
for (int i = 0; i < hits.length(); i++) {
JSONObject jsonObject = hits.getJSONObject(i);
mUserName.add(jsonObject.getString("user_username"));
Log.d(TAG, "requestCompleted: Userrrrr: " + jsonObject.getString("user_username"));
mUserImage.add(jsonObject.getString("user_image_url"));
mUserId.add(jsonObject.getString("user_id"));
Log.d(TAG, "requestCompleted: Size: " + mUserImage.size());
userSearchAdapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
谁能帮我指出错误吗?谢谢
我的适配器:
public class UserSearchAdapter extends RecyclerView.Adapter<UserSearchAdapter.SearchViewHolder> {
private static final String TAG = "UserSearchAdapter";
private Context mContext;
private ArrayList<String> mUserImage = new ArrayList<>();
private ArrayList<String> mUserName = new ArrayList<>();
private ArrayList<String> mUserId = new ArrayList<>();
public UserSearchAdapter(Context mContext, ArrayList<String>
mUserImage, ArrayList<String> mUserName, ArrayList<String> mUserId) {
this.mContext = mContext;
this.mUserImage = mUserImage;
this.mUserName = mUserName;
this.mUserId = mUserId;
}
@NonNull
@Override
public SearchViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.user_item,parent,false);
SearchViewHolder holder = new SearchViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull final SearchViewHolder holder, final int position) {
Glide.with(mContext)
.load(mUserImage.get(position))
.placeholder(R.drawable.ic_profile)
.into(holder.image_profile);
holder.btn_follow.setVisibility(View.VISIBLE);
holder.username.setText(mUserName.get(position));
}
@Override
public int getItemCount() {
return mUserName.size();
}
class SearchViewHolder extends RecyclerView.ViewHolder{
private TextView username,fullname;
private CircleImageView image_profile;
private Button btn_follow;
public SearchViewHolder(@NonNull View itemView) {
super(itemView);
username = itemView.findViewById(R.id.username);
fullname = itemView.findViewById(R.id.fullname);
image_profile = itemView.findViewById(R.id.image_profile);
btn_follow = itemView.findViewById(R.id.btn_follow);
}
}
公共类UserSearchAdapter扩展了RecyclerView.Adapter{
私有静态最终字符串标记=“UserSearchAdapter”;
私有上下文;
private ArrayList mUserImage=new ArrayList();
private ArrayList mUserName=new ArrayList();
private ArrayList mUserId=new ArrayList();
公共UserSearchAdapter(上下文mContext,ArrayList
(博物馆名称、美术馆名称、美术馆名称){
this.mContext=mContext;
this.mUserImage=mUserImage;
this.mUserName=mUserName;
this.mUserId=mUserId;
}
@非空
@凌驾
public SearchViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
视图=布局更平坦。从(mContext)。充气(R.layout.user\u项,父项,false);
SearchViewHolder=新的SearchViewHolder(视图);
报税表持有人;
}
@凌驾
public void onBindViewHolder(@NonNull final SearchViewHolder,final int位置){
使用(mContext)滑动
.load(mUserImage.get(位置))
.占位符(R.drawable.ic_配置文件)
.插入(保持架图像_剖面);
holder.btn_follow.setVisibility(View.VISIBLE);
holder.username.setText(mUserName.get(position));
}
@凌驾
public int getItemCount(){
返回mUserName.size();
}
类SearchViewHolder扩展了RecyclerView.ViewHolder{
私有文本视图用户名,全名;
私有CircleImageView图像_配置文件;
私人按钮btn_follow;
公共SearchViewHolder(@NonNull View itemView){
超级(项目视图);
username=itemView.findviewbyd(R.id.username);
fullname=itemView.findviewbyd(R.id.fullname);
image\u profile=itemView.findviewbyd(R.id.image\u profile);
btn_follow=itemviewbyd(R.id.btn_follow);
}
}
我的日志打印:
2020-06-26 17:56:36.0143620-3620/com.shivaconsulting.memesapp D/ProfileSearchFragment:requestCompleted:Size:1
2020-06-26 17:56:36.0143620-3620/com.shivaconsulting.memesapp D/ProfileSearchFragment:requestCompleted:userrrrrr:Algolia2
2020-06-26 17:56:36.0143620-3620/com.shivaconsulting.memesapp D/ProfileSearchFragment:requestCompleted:Size:2
2020-06-26 17:56:36.0143620-3620/com.shivaconsulting.memesapp D/ProfileSearchFragment:requestCompleted:userrrrrr:giraffee
2020-06-26 17:56:36.0143620-3620/com.shivaconsulting.memesapp D/ProfileSearchFragment:requestCompleted:Size:3
2020-06-26 17:56:36.0143620-3620/com.shivaconsulting.memesapp D/ProfileSearchFragment:requestCompleted:userrrrrr:zumba
2020-06-26 17:56:36.0143620-3620/com.shivaconsulting.memesapp D/ProfileSearchFragment:requestCompleted:Size:4
请检查您的适配器实现。
userSearchAdapter.notifyDataSetChanged();
在for循环内被多次调用。请将其移到for循环外。一旦数据集准备好。更新适配器,然后通知更改。目前,您刚刚创建了一个空的列表
。您没有在任何地方使用它。如果我理解正确,您将用所需的列表填充此列表em并将其传递给适配器以供显示。您正在使用空数组列表初始化RV适配器,稍后在回调方法中获取数据时
请求已完成
您没有将数据设置到适配器
RV适配器中应有一种方法,如
userSearchAdapter.updateData(<updated data>)
在适配器上已尝试移动userSearchAdapter.notifyDataSetChanged();在循环之外,但仍然没有填充。我没有任何适配器实现。我只是将edittext从我的mainactivity发送到fragmentSo…你的回收器适配器使用的项目列表在哪里?为什么不发布它的代码?请求真的完成了吗?@Shark我想添加适配器的代码会很模糊。很抱歉,没有发布问题简要地编辑了我的问题并添加了此日志打印的适配器代码,对吗?
log.d(标记,“requestCompleted:Size:+mUserImage.Size());
在notifyDataSetChanged()上设置断点时会发生什么
并检查适配器中的三个列表?它们是否为非空?是的,这些是日志打印,其阵列大小已附加屏幕截图以进行更多澄清oooo…似乎一切都正常?是否是一些愚蠢的XML问题导致了recyclerview的显示被剪切/中断?谢谢,我将再次尝试方法是在updateData()方法本身内部调用notifyDataSetChanged()
userSearchAdapter.updateData(<updated data>)
userSearchAdapter.notifyDataSetChanged()