我在fragmnet中使用recyclelerview
Fragmnet正在运行,但我的回收者查看的数据并未显示出来。
我的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>
行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" />
我的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");
}
}
}
最终我的适配器代码:
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);
}
}
}
编辑:
@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;
}
任何人请帮助我为什么我的onCreateViewHolder函数没有调用?
答案 0 :(得分:0)
你可以放置替换上面的代码片段并重新检查,我猜你在设置时没有传递数据,阻塞你试图设置为适配器的数据集
@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;
}