Android Recyclerview布局在片段中不起作用
我面临一个关于片段中的Android Recyclerview布局在片段中不起作用,android,android-fragments,android-recyclerview,recyclerview-layout,Android,Android Fragments,Android Recyclerview,Recyclerview Layout,我面临一个关于片段中的RecyclerView的问题。 我将我的回收视图从活动移动到片段。 移动到碎片后。 它显示了错误 无适配器连接;跳过布局 这是我的密码 public class LatestProperty extends Fragment { View customView; private List<PropertyInfo> propertyInfoList=new ArrayList<>(); private RecyclerVie
RecyclerView
的问题。
我将我的回收视图
从活动
移动到片段
。
移动到碎片后。
它显示了错误
无适配器连接;跳过布局
这是我的密码
public class LatestProperty extends Fragment {
View customView;
private List<PropertyInfo> propertyInfoList=new ArrayList<>();
private RecyclerView recyclerView;
private PropertyAdapter propertyAdapter;
View view;
ProgressDialog progressDialog;
String areaType;
private String imagePath="abc";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.total_properties, container, false);
recyclerView = (RecyclerView)view.findViewById(R.id.recycler_view);
progressDialog=new ProgressDialog(getActivity(),R.style.MyAlertDialogStyle);
propertyAdapter=new PropertyAdapter(getActivity(),propertyInfoList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(propertyAdapter);
preparePropertyData();
return view;
}
public void preparePropertyData()
{
progressDialog.setMessage("Loading Latest Properties...");
progressDialog.setCancelable(false);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
Config config=new Config();
Volley.newRequestQueue(getActivity()).add(new StringRequest(config.getLatestPropertyData
, new Response.Listener<String>() {
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONArray jsonArray=new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
String area=jsonArray.getJSONObject(i).getString("area");
area=area.replace("null","NA");
String Gallery= jsonArray.getJSONObject(i).getString("gallery");
String[] dot=Gallery.split(";");
String stringPrice=jsonArray.getJSONObject(i).getString("price");
stringPrice=stringPrice.replace(",","").replace("null","0");
Long price=Long.parseLong(stringPrice);
String areaType=jsonArray.getJSONObject(i).getString("area_type").replace("null","");
PropertyInfo propertyInfo=new PropertyInfo(jsonArray.getJSONObject(i).getString("title"),jsonArray.getJSONObject(i).getString("bed"),jsonArray.getJSONObject(i).getString("bath"),area,areaType,jsonArray.getJSONObject(i).getString("address"),jsonArray.getJSONObject(i).getString("purpose"),withSuffix(price).toString(), imagePath+dot[0],jsonArray.getJSONObject(i).getString("id"));
propertyInfoList.add(propertyInfo);
}
propertyAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getActivity(), e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
CookieBar.Build(getActivity()).setTitle("Oppss. :(").setMessage("Server Error..!").setBackgroundColor(R.color.cookieColor).show();
}
}));
}
public类LatestProperty扩展片段{
视图自定义视图;
private List propertyInfoList=new ArrayList();
私人回收站;
私人财产适应者财产适应者;
视图;
进行对话进行对话;
字符串区域类型;
私有字符串imagePath=“abc”;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.total_属性,容器,false);
recyclerView=(recyclerView)视图.findViewById(R.id.recycler\u视图);
progressDialog=新建progressDialog(getActivity(),R.style.MyAlertDialogStyle);
propertyAdapter=新的propertyAdapter(getActivity(),PropertyFolist);
RecyclerView.LayoutManager mLayoutManager=新的LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
setItemAnimator(新的DefaultItemAnimator());
recyclerView.setAdapter(propertyAdapter);
preparePropertyData();
返回视图;
}
public void preparePropertyData()
{
setMessage(“加载最新属性…”);
progressDialog.setCancelable(假);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
Config=new Config();
Volley.newRequestQueue(getActivity()).add(newStringRequest)(config.getLatestPropertyData
,new Response.Listener(){
公共void onResponse(字符串响应){
progressDialog.disclose();
试一试{
JSONArray JSONArray=新JSONArray(响应);
for(int i=0;i
下面是适配器类
public PropertyAdapter(Context context,List<PropertyInfo> propertyInfoList) {
this.mContext=context;
this.propertyInfoList = propertyInfoList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recent_property_single_item, parent, false);
return new MyViewHolder(itemView);
}
publicpropertyadapter(上下文,列表propertyInfoList){
this.mContext=上下文;
this.propertyInfoList=propertyInfoList;
}
@凌驾
公共MyViewHolder onCreateViewHolder(视图组父级,int-viewType){
View itemView=LayoutInflater.from(parent.getContext())
.充气(R.layout.recent\u property\u single\u item,parent,false);
返回新的MyViewHolder(itemView);
}
未连接适配器;正在跳过布局
因为在将适配器设置到
后,您正在阵列列表中添加数据,而且网络操作需要时间进行响应
您需要首先在属性的文件夹中添加数据,然后将适配器设置到回收视图中
当RecyclerView
项出现异常时,请查看您的MyAdapter
类和相关的ViewHolder
,检查您是否正确地在那里获取了TextView
。但在添加数据后会通知hi。在代码中,适配器在填充数据之前设置,因为网络操作需要时间对于response@Nilesh没有错误,但在recyclerview中没有显示任何内容。我的代码可以与牛轧糖一起使用。但不能与棒棒糖和其他东西一起使用。@Muhammadaad请尝试此适配器。notifyItemInserted(propertyInfoList.size();
并检查您的getItemCount
必须返回propertyInfoList.size()
propertyAdapter.notifyDataSetChanged()之前的notifyItemInserted?和getItemCount已存在propertyInfoList.size()@muhammadaad;
我的代码可以与牛轧糖一起使用。但不能与棒棒糖等一起使用。为了解决这个问题,我首先在build.gradle文件中添加了multidex支持库:请使用此链接了解更多信息。multidex?为什么?任何原因?当你的应用程序和库它引用了超过65536个方法,你可以
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
preparePropertyData();
propertyAdapter=new PropertyAdapter(getActivity(),propertyInfoList);
recyclerView.setAdapter(propertyAdapter);