简体   繁体   English

onCreateViewHolder没有在fragmnet recyclerview中调用

[英]onCreateViewHolder not calling in fragmnet recyclerview

I am using recyclerview in fragmnet. 我在fragmnet中使用recyclerview。
Fragmnet is working but my recycler view data not dispalyed. Fragmnet正在运行,但我的回收站查看数据没有显示出来。

My xml code: 我的xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="5dp">
    <LinearLayout android:id="@+id/projectcommends" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:orientation="vertical" android:padding="@dimen/common_padding">
        <TextView style="@style/TextHeading" android:text="Project comments" />
        <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp">
            <TextView android:layout_width="180dp" android:layout_height="wrap_content" android:text="Date" android:textSize="17dp" android:textColor="@color/black" android:gravity="center_horizontal" android:textStyle="bold" />
            <TextView android:layout_width="180dp" android:layout_height="wrap_content" android:text="Comments" android:textSize="17dp" android:textColor="@color/black" android:gravity="center_horizontal" android:textStyle="bold" />
        </LinearLayout>
        <android.support.v7.widget.RecyclerView android:id="@+id/commendsList" android:layout_width="match_parent" android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.v7.widget.CardView>

Row xml: 行xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:layout_marginTop="5dp">
<TextView android:id="@+id/commentdate" android:layout_width="180dp" android:textSize="15dp" android:layout_height="wrap_content" android:text="20-06-2016" android:gravity="center" />
<TextView android:id="@+id/commentdetail" android:layout_width="180dp" android:layout_height="wrap_content" android:textSize="15dp" android:text="project commends testing list" android:gravity="center" />

My Fragmnet code: 我的Fragmnet代码:

public class ProjectComments extends Fragment {
   private List < CommentsData > arraylist;
   private RecyclerView recyclerView;
   private RecyclerView.Adapter adapter;
   private RecyclerView.LayoutManager layoutManager;
   private String ordered;

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.project_comments, container, false);
      initialize(view);
      Bundle b = getArguments();
      orderid = b.getString("orderid");
      recyclerView.setHasFixedSize(true);
      recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
      arraylist = new ArrayList < > ();
      adapter = new CommentsAdapter(arraylist);
      recyclerView.setAdapter(adapter);
      getCommentsData();
      return view;
   }

   private void initialize(View view) {
      recyclerView = (RecyclerView) view.findViewById(R.id.commendsList);
   }

   private void getCommentsData() {
      String mobilecustomertoken = SharedPreferencesManager.readPreferenceString("MobileCustomerToken", "D/N");
      JSONObject commentData = new JSONObject();
      try {
         commentData.put("mobilecustomertoken", mobilecustomertoken);
         commentData.put("orderid", orderid);
         JsonObjectRequest commentsObject = new JsonObjectRequest(1, Common.CommentsList, commentData, new Response.Listener < JSONObject > () {
            @Override
            public void onResponse(JSONObject commentsResponse) {
               Log.d("Responsedetail", commentsResponse.toString());
               try {
                  int status = commentsResponse.getInt("status");
                  if (status == 1) {
                     commentsProgress(commentsResponse);
                  }
               } catch (JSONException e) {
                  e.printStackTrace();
               } catch (InterruptedException e) {
                  e.printStackTrace();
               }
            }
         }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
               error.printStackTrace();
               Log.d("Response", "PENDING ERROR");
            }
         });
         commentsObject.setShouldCache(false);
         ServiceBellApp.getInstance().addToRequestQueue(commentsObject);
      } catch (JSONException localJSONException) {
         localJSONException.printStackTrace();
         return;
      }
   }

   private void commentsProgress(JSONObject commentsResponse) throws JSONException, InterruptedException {
      JSONArray result = commentsResponse.getJSONArray("response");
      CommentsData orderListModule;
      for (int i = 0; i < result.length(); i++) {
         JSONObject jsonObject = result.getJSONObject(i);
         orderListModule = new CommentsData();
         orderListModule.setCommentsData(jsonObject.getString("commentdate"));
         orderListModule.setCommentsDetail(jsonObject.getString("comendDetail"));
         arraylist.add(orderListModule);
      }
      adapter.notifyDataSetChanged();
      int count = adapter.getItemCount();
      if (count != 0) {
         Log.d("Response", "Test");
      }
   }
}

Final my adapter code: 最后我的适配器代码:

public class CommentsAdapter extends RecyclerView.Adapter < CommentsAdapter.RecyclerViewHolder > {
   List < CommentsData > arrayList;
   public CommentsAdapter(List < CommentsData > list) {
      this.arrayList = list;
   }

   @Override
   public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comments_list, parent, false);
      RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
      return recyclerViewHolder;
   }

   @Override
   public void onBindViewHolder(RecyclerViewHolder holder, int position) {
      CommentsData commentsData = arrayList.get(position);
      Log.d("ResponseAdapter", commentsData.getCommentsData());
      holder.commentdate.setText(commentsData.getCommentsData());
      holder.commentDetail.setText(commentsData.getCommentsDetail());
   }

   @Override
   public int getItemCount() {
      String dd = Integer.toString(arrayList.size());Log.d("Responsecount", dd);return (null != arrayList ? arrayList.size() : 0);
   }

   public class RecyclerViewHolder extends RecyclerView.ViewHolder {
      public TextView commentdate, commentDetail;
      public RecyclerViewHolder(View itemView) {
         super(itemView);
         Log.d("ResponseAda", "Success");
         commentdate = (TextView) itemView.findViewById(R.id.commentdate);
         commentDetail = (TextView) itemView.findViewById(R.id.commentdetail);
      }
   }
}

Edited: 编辑:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.project_comments, container, false);
    initialize(view);
    Bundle b = getArguments();
    orderid = b.getString("orderid");
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new
            LinearLayoutManager(getActivity()));
    arraylist = new ArrayList < > ();
    getCommentsData();
    adapter = new CommentsAdapter(arraylist);
    recyclerView.setAdapter(adapter);
    return view;
}

Any one please help me why my onCreateViewHolder function not calling? 任何人请帮助我为什么我的onCreateViewHolder函数没有调用?

Can you Place Replace the above code snip and recheck , i guess you're not passing the data to while setting it , clog the data set you trying to set to adapter 你可以放置替换上面的代码片段并重新检查,我猜你在设置它时没有传递数据,阻塞你试图设置为适配器的数据集

  @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,     Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.project_comments, container, false);
  initialize(view);
  Bundle b = getArguments();
  orderid = b.getString("orderid");
  recyclerView.setHasFixedSize(true);
  recyclerView.setLayoutManager(new  
  LinearLayoutManager(getActivity()));
  arraylist = new ArrayList < > ();
  getCommentsData();
  adapter = new CommentsAdapter(arraylist);
  recyclerView.setAdapter(adapter);
  return view;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM