- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我的RecyclerView没有调用onCreateViewHolder,所以onBindViewHolder在recyclerview中不会出现什么。我放了调试日志,没有显示日志。可以是什么?
我的适配器:
public class CommentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private static final int EMPTY_VIEW = 10 ;
private ArrayList<comment> mItems;
Boolean firstTime = true;
private Typeface mTf = null;
Context mContext;
public CommentListAdapter(Context context,ArrayList<comment> items){
Log.e("Adapter", "constructor Called");
this.mItems = items;
mContext = context;
}
public class EmptyViewHolder extends RecyclerView.ViewHolder {
public EmptyViewHolder(View itemView) {
super(itemView);
}
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView mAuthorName;
TextView mMessage;
NetworkImageView mThumbnail;
public ViewHolder(View itemView) {
super(itemView);
mAuthorName = (TextView)itemView.findViewById(R.id.author_name);
mMessage = (TextView)itemView.findViewById(R.id.message);
mThumbnail = (NetworkImageView)itemView.findViewById(R.id.author_avatar);
}
}
public void add(comment item, int position) {
mItems.add(position, item);
notifyItemInserted(position);
}
public void remove(comment item) {
int position = mItems.indexOf(item);
mItems.remove(position);
notifyItemRemoved(position);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
Log.e("Adapter", "onCreateViewHolder Called");
View v;
if(firstTime){
mTf = BBcTypeFace.getTypeFace(parent.getContext().getApplicationContext(),"font/bbc.ttf");
firstTime = false;
}
if( viewType == EMPTY_VIEW){
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.comment_empty_row,parent,false);
EmptyViewHolder evh = new EmptyViewHolder(v);
return evh;
}else {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.comment_row, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
Log.e("Adapter", "onBindViewHolder Called");
if(viewHolder instanceof ViewHolder) {
ViewHolder holder = (ViewHolder)viewHolder;
comment c = mItems.get(position);
Log.e("Adapter", "Comment is\n: " + c.toString());
final ViewHolder finalHolder = holder;
ImageRequest request = new ImageRequest(c.author_img_link, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
if (bitmap != null) {
finalHolder.mThumbnail.setImageBitmap(bitmap);
}
}
}, 0, 0, null,
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
VolleyLog.e("ImageLoader", volleyError.getMessage());
VolleyLog.e("ImageLoader", volleyError.getStackTrace());
}
});
GetVideoInfo.getInstance(mContext.getApplicationContext()).addToRequestQueue(request);
holder.mAuthorName.setText(c.author_name);
holder.mMessage.setText(c.Message);
holder.mMessage.setTypeface(mTf);
holder.mAuthorName.setTypeface(mTf);
}
}
@Override
public int getItemCount() {
Log.e("Adapter", "getItemCount() Called");
return (mItems.size() > 0 ? mItems.size() : 1);
}
@Override
public int getItemViewType(int position) {
Log.e("Adapter", "getItemViewType() Called");
if (mItems.size() == 0) {
return EMPTY_VIEW;
}
return super.getItemViewType(position);
}}
我将使用 public void add(comment item, int position){...}
在 RecyclerView 中添加项目。
在 fragment 中:
private RecyclerView mRecyclerView;
private CommentListAdapter mAdapter;
private LayoutManager mLayoutManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);
....
mLayoutManager = new LinearLayoutManager(getActivity());
mAdapter = new CommentListAdapter(getActivity(),new ArrayList<comment>());
mRecyclerView = (RecyclerView)rootView.findViewById(R.id.comment_list);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
makeJsonObjectRequest(mItem.url);
return rootView;
}
XML 文件:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView"
android:background="@android:color/white"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true">
....
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
.....
<android.support.v7.widget.RecyclerView
android:id="@+id/comment_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
....
</RelativeLayout>
....
</ScrollView>
comment_row xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:gravity="right"
android:background="@android:color/darker_gray">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/author_name"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/author_avatar"
android:layout_toStartOf="@+id/author_avatar"
android:gravity="right" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/message"
android:layout_below="@+id/author_name"
android:layout_toLeftOf="@+id/author_avatar"
android:layout_toStartOf="@+id/author_avatar"
android:gravity="right" />
</LinearLayout>
<com.android.volley.toolbox.NetworkImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/author_avatar"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:minHeight="150dp"
android:minWidth="150dp" /></LinearLayout>
我将使用此代码将项目添加到 RecyclerView :
JsonObjectRequest jsonObjReqComment = new
JsonObjectRequest(Request.Method.GET,urlJsonObj+"#comment", null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray res = response.getJSONArray("response");
//Log.e("Comment","Count:"+response.toString());
//Log.e("Comment","Count:"+res.length());
for (int i = 0; i < res.length(); i++) {
JSONObject thread = res.getJSONObject(i);
JSONObject author_json = thread.getJSONObject("author");
int dislikes = thread.getInt("dislikes");
int likes = thread.getInt("likes");
String Message = thread.getString("message");
//get Author info
String author_img_link = author_json.getJSONObject("avatar").getString("permalink");
String author_name = author_json.getString("name");
comment c = new comment(dislikes,likes,Message,author_img_link,author_name);
//Log.e("Comment",c.toString());
//commentsList.add(c);
mAdapter.add(c,0);
}
} catch (JSONException e) {
Log.e("OnResponse","Error JSON");
e.printStackTrace();
} catch (Exception e){
Log.e("OnResponse","Error Exception");
e.printStackTrace();
}
}
}
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("vOLLEY", "Error: " + error.getMessage());
// hide the progress dialog
}
}
){
@Override
public Map<String, String> getHeaders(){
Map<String, String> headers = new HashMap<String, String>();
headers.put("User-agent", "Comment");
return headers;
}
};
最佳答案
除了@SanatiSharif 和@sohrab 的回答,您必须遵循以下强制性步骤。
确保调用 setLayoutManager
,如下所示。
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
在把adapter设置成recyclerView之前,否则是不行的。如果需要,您可以自定义它。 this link会让您了解 LayoutManager 的工作原理。
关于java - Recyclerview没有调用任何Adapter方法:onCreateViewHolder, onBindViewHolder,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28115553/
这个问题在这里已经有了答案: RecyclerView onCreateViewHolder Return Type Incompatibility With Multiple Custom Vie
我的 fragment public class PostFragment extends BaseFragment implements PostView { @Inject PostPresent
我正在关注 this tutorial ,据此,当我启动我的应用程序时,我应该能够看到已添加到 Firestore 的项目(文档)。但这正在发生 onCreateViewHolder仅当我点击 tex
我有一个 RecylerView 适配器,我在其中处理 onCreateViewHolder 方法中的 onClicks,但是我无法找到获取当前位置的方法。我当前的方法如下所示: @Overr
我在 NestedScrollView 中有一个 RecyclerView,它显示一些异步下载的数据。问题是在初始化项目时存在明显的滞后。经过一些测试后,我发现问题是每个项目都会调用 onCreate
我正在使用支持库 v7 将我的 ListView 转换为 RecyclerView。 我有 2 个不同的布局要加载到 RecyclerView 中。在 ListView 中,我在 getView()
我的RecyclerView没有调用onCreateViewHolder,所以onBindViewHolder在recyclerview中不会出现什么。我放了调试日志,没有显示日志。可以是什么? 我的
我有一个根 recyclerview,每个项目都填充了这个布局: 如您所见,每个项目中都有另一个recyclerview。我想用headers实现分段recycler
Recyclerview 不调用任何适配器方法:onBindViewHolder、onCreateViewHolder 因此适配器为空 这是我的代码: public class PostsFragme
我有 PagedListAdapater: class TrackedActivityAdapter constructor(diffUtilCallback: DiffUtil.ItemCallba
我尝试调试我的程序,发现执行没有在 FirebaseRecyclerAdapter recyclerAdapter=new FirebaseRecyclerAdapter( 选项)。我已经尝试了 st
我关注了this Google Developer 的 YouTube channel 的教程,用于实现 AdMob 原生快捷广告。 我收到以下错误: required: packagename.ad
我有一个 RecyclerView 可以将项目显示为列表、小网格或大网格,这可以在运行时更改。根据用户选择的样式,我在 onCreateViewHolder 中展开不同的布局。 我还使用 layout
我正在使用 RecyclerView.Adapter,但我对它的方法 onCreateViewHolder 的工作有点困惑。 @Override public RecyclerView.ViewH
到目前为止,我认为当我使用 adapter = new RecyclerViewAdapter(this, list) 调用适配器的构造函数时,调用了 RecyclerView.Adapter 中的
我的自定义适配器中的 onCreateViewHolder 函数出现了一个奇怪的异常。可能是因为 getItemViewType 函数按预期返回不同的值。 这是 getItemViewType 函数的
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 6 年前。 这应该是一个一如既往的简单工作。
短篇小说:我有一个(预缓存)自定义 LinearLayoutManager、RecyclerView、自定义 RecyclerView.Adaptor、自定义 RecyclerView.ViewHol
这是我的代码,我不知道为什么它现在不起作用 songs 是一个包含 getter setter 的简单 java 类 在从 JSON 获取数据之前,我尝试在 recycleView 中静态添加数据,并
我正在尝试制作 RecyclerView,但遇到了这个奇怪的错误。 消息如下: android.view.InflateException: Binary XML file line #0: Atte
我是一名优秀的程序员,十分优秀!