- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Android 的 Android 版本 23 项目中使用 RecyclerView
、CardView
、Fragment
和 AsyncTask
工作室。我正在尝试调试三星 Galaxy S7 上的应用程序。无论我似乎在代码方面尝试什么,我的 RecyclerView.Adapter
实现类的 onCreateViewHolder
和 onBindViewHolder
方法都不会被调用。
Activity :
public class RecyclerViewFragmentActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.todays_visit_list_container);
if (savedInstanceState == null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
RecyclerViewFragment fragment = new RecyclerViewFragment();
transaction.replace(R.id.sample_content_fragment, fragment);
transaction.commit();
}
}
}
fragment :
public class RecyclerViewFragment extends Fragment {
protected RecyclerView mRecyclerView;
protected RecyclerView.LayoutManager mLayoutManager;
protected RecyclerViewAdapter mAdapter;
protected ListRefresher aListRefresher;
protected class ListRefresher extends AsyncTask<Uri, Void, Void> {
private ArrayList<Probationer> probationers = null;
RecyclerViewFragment fragment = null;
public ListRefresher(RecyclerViewFragment fragment) {
this.fragment = fragment;
}
@Override
protected Void doInBackground(Uri... params) {
ArrayList<Probationer> probationers = new ArrayList<Probationer>();
// getData makes a JSON call to retrieve data for RecyclerView Adapter
listobjects = getData();
return null;
}
@Override
protected void onPostExecute(Void unused) {
fragment.onTaskCompleted();
}
public ArrayList getItems() {
return probationers;
}
}
protected void onTaskCompleted() {
PPOTodaysVisitListAdapter adapter = new PPOTodaysVisitListAdapter(getActivity(), aListRefresher.getItems());
mRecyclerView.setAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.todays_visit_list, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
llm.scrollToPosition(0);
mRecyclerView.setLayoutManager(llm);
mRecyclerView.setHasFixedSize(true);
aListRefresher = new ListRefresher(this);
aListRefresher.execute();
return rootView;
}
}
适配器:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private ArrayList<Probationer> probationers;
Context context;
public RecyclerViewAdapter(Context context, ArrayList<Probationer> probationers) {
this.probationers = probationers;
this.context = context;
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
public CardView cv;
public TextView supervisionLevel;
public TextView popCodes;
public TextView unavailable;
public TextView offenderId;
public TextView name;
public TextView birthDate;
public TextView addressLine1;
public TextView addressLine2;
public TextView lastDrugScreen;
public TextView lastDrugScreenResultNeg;
public TextView lastDrugScreenResultPos;
public TextView mainCrime;
public TextView lastDate1Lbl;
public TextView lastDate2Lbl;
public TextView lastDate3Lbl;
public TextView lastDate4Lbl;
public TextView lastDate1;
public TextView nextDate1;
public TextView lastDate2;
public TextView nextDate2;
public TextView lastDate3;
public TextView nextDate3;
public TextView lastDate4;
public TextView nextDate4;
MyViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cardView);
supervisionLevel = (TextView) itemView.findViewById(R.id.supervisionLevel);
unavailable = (TextView) itemView.findViewById(R.id.unavailable);
popCodes = (TextView) itemView.findViewById(R.id.popCodes);
.
.
.
}
}
@Override
public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Inflate the layout, initialize the View Holder
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.todays_visit_list_item, parent, false);
RecyclerViewAdapter.MyViewHolder holder = new RecyclerViewAdapter.MyViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(RecyclerViewAdapter.MyViewHolder viewHolder, final int position) {
if (probationers != null && probationers.size() > 0) {
Probationer aProbationer = probationers.get(position);
if (aProbationer != null) {
viewHolder.offenderId.setText(aProbationer.getProbationerId());
viewHolder.name.setText(aProbationer.getName());
viewHolder.birthDate.setText(aProbationer.getDateOfBirth());
viewHolder.supervisionLevel.setText(aProbationer.getSupervisionLevel());
viewHolder.unavailable.setText(aProbationer.getUnavailable());
.
.
.
}
}
@Override
public int getItemCount() {
return probationers.size();
}
}
RecyclerView 布局(todays_visit_list.xml):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
CardView 布局(todays_visit_list_item.xml):
<android.support.v7.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
app:cardCornerRadius="@dimen/activity_vertical_margin"
app:cardElevation="@dimen/activity_vertical_margin"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/photolayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="72dip"
android:layout_height="100dip"
android:layout_marginTop="6dip">
<ImageView
android:id="@+id/loadingphoto"
android:scaleType="fitEnd"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/nopicture"/>
<ImageView
android:id="@+id/photo"
android:scaleType="fitEnd"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/supmsglayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="72dip"
android:layout_height="20dip"
android:layout_marginTop="6dip"
android:layout_below="@id/photolayout"
android:layout_alignLeft="@id/photolayout">
<TextView android:id="@+id/supervisionLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dip"
android:layout_marginLeft="2dip"
android:textSize="11sp"
android:textStyle="bold"/>
<TextView android:id="@+id/unavailable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11sp"
android:textStyle="bold"
android:layout_toRightOf="@id/supervisionLevel"
android:layout_alignTop="@id/supervisionLevel"/>
<TextView android:id="@+id/popCodes"
android:layout_width="76dip"
android:layout_height="wrap_content"
android:layout_marginTop="2dip"
android:textSize="11sp"
android:textColor="#E79A00"
android:textStyle="bold"
android:layout_below="@id/supervisionLevel"
android:layout_alignLeft="@id/supervisionLevel"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/contentlayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dip"
android:layout_height="100dip"
android:layout_marginTop="8dip"
android:layout_marginLeft="4dip"
android:layout_toRightOf="@id/photolayout"
android:layout_alignTop="@id/photolayout">
<TextView android:id="@+id/offenderIdLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Offender ID: "
android:textStyle="bold"
android:textSize="11sp"/>
<TextView android:id="@+id/offenderId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_toRightOf="@id/offenderIdLbl"
android:layout_alignTop="@id/offenderIdLbl"/>
<TextView android:id="@+id/nameLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: "
android:textStyle="bold"
android:textSize="11sp"
android:layout_below="@id/offenderIdLbl"
android:layout_alignLeft="@id/offenderIdLbl"/>
<TextView android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_toRightOf="@id/nameLbl"
android:layout_alignTop="@id/nameLbl"/>
<TextView android:id="@+id/birthDateLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Birth Date: "
android:textStyle="bold"
android:textSize="11sp"
android:layout_below="@id/nameLbl"
android:layout_alignLeft="@id/nameLbl"/>
<TextView android:id="@+id/birthDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_toRightOf="@id/birthDateLbl"
android:layout_alignTop="@id/birthDateLbl"/>
<TextView android:id="@+id/addrLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address: "
android:textStyle="bold"
android:textSize="11sp"
android:layout_below="@id/birthDateLbl"
android:layout_alignLeft="@id/birthDateLbl"/>
<TextView
android:id="@+id/addressLine1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_toRightOf="@id/addrLbl"
android:layout_alignTop="@id/addrLbl"/>
<TextView
android:id="@+id/addressLine2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_below="@id/addressLine1"
android:layout_alignLeft="@id/addressLine1"/>
<TextView android:id="@+id/lastDrugScreenLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drug Scr: "
android:textStyle="bold"
android:textSize="11sp"
android:layout_below="@id/addressLine2"
android:layout_alignLeft="@id/addrLbl"/>
<TextView android:id="@+id/lastDrugScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_toRightOf="@id/lastDrugScreenLbl"
android:layout_alignTop="@id/lastDrugScreenLbl"/>
<TextView android:id="@+id/lastDrugScreenResultNeg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_toRightOf="@id/lastDrugScreen"
android:layout_alignTop="@id/lastDrugScreen"/>
<TextView android:id="@+id/lastDrugScreenResultPos"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11sp"
android:textColor="#d7144b"
android:textStyle="bold"
android:layout_toRightOf="@id/lastDrugScreen"
android:layout_alignTop="@id/lastDrugScreen"/>
<TextView android:id="@+id/mainCrimeLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Crime: "
android:textStyle="bold"
android:textSize="11sp"
android:layout_below="@id/lastDrugScreenLbl"
android:layout_alignLeft="@id/lastDrugScreenLbl"/>
<TextView android:id="@+id/mainCrime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_toRightOf="@id/mainCrimeLbl"
android:layout_alignTop="@id/mainCrimeLbl"/>
<TextView android:id="@+id/lastDate1Lbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="33dip"
android:textStyle="bold"
android:textSize="11sp"
android:layout_below="@id/mainCrimeLbl"
android:layout_alignLeft="@id/mainCrimeLbl"/>
<TextView android:id="@+id/lastDate2Lbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="34dip"
android:textStyle="bold"
android:textSize="11sp"
android:layout_below="@id/mainCrimeLbl"
android:layout_toRightOf="@id/lastDate1Lbl"/>
<TextView android:id="@+id/lastDate3Lbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="86dip"
android:textStyle="bold"
android:textSize="11sp"
android:layout_below="@id/mainCrimeLbl"
android:layout_toRightOf="@id/lastDate1Lbl"/>
<TextView android:id="@+id/lastDate4Lbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="140dip"
android:textStyle="bold"
android:textSize="11sp"
android:layout_below="@id/mainCrimeLbl"
android:layout_toRightOf="@id/lastDate1Lbl"/>
<TextView android:id="@+id/lastLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last: "
android:textStyle="bold"
android:textSize="11sp"
android:layout_below="@id/lastDate1Lbl"
android:layout_alignLeft="@id/mainCrimeLbl"/>
<TextView android:id="@+id/nextLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next: "
android:textStyle="bold"
android:textSize="11sp"
android:layout_below="@id/lastLbl"
android:layout_alignLeft="@id/lastLbl"/>
<TextView android:id="@+id/lastDate1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_below="@id/lastDate1Lbl"
android:layout_alignLeft="@id/lastDate1Lbl"/>
<TextView android:id="@+id/nextDate1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_below="@id/lastDate1"
android:layout_alignLeft="@id/lastDate1"/>
<TextView android:id="@+id/lastDate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_below="@id/lastDate2Lbl"
android:layout_alignLeft="@id/lastDate2Lbl"/>
<TextView android:id="@+id/nextDate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_below="@id/lastDate2"
android:layout_alignLeft="@id/lastDate2"/>
<TextView android:id="@+id/lastDate3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_below="@id/lastDate3Lbl"
android:layout_alignLeft="@id/lastDate3Lbl"/>
<TextView android:id="@+id/nextDate3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_below="@id/lastDate3"
android:layout_alignLeft="@id/lastDate3"/>
<TextView android:id="@+id/lastDate4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_below="@id/lastDate4Lbl"
android:layout_alignLeft="@id/lastDate4Lbl"/>
<TextView android:id="@+id/nextDate4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_below="@id/lastDate4"
android:layout_alignLeft="@id/lastDate4"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/undolayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="40dip"
android:layout_height="100dip"
android:layout_marginTop="8dip"
android:layout_marginLeft="4dip"
android:layout_toRightOf="@id/contentlayout"
android:layout_alignTop="@id/contentlayout">
<Button
android:id="@+id/undo_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_undo"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:layout_gravity="end|center_vertical"
style="@style/Base.Widget.AppCompat.Button.Borderless"
/>
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
我的 JSON 调用正在返回数据,AsyncTask
正在回调到 Fragment
并执行方法 onTaskCompleted()
。当我创建 RecyclerViewAdapter
的实例时,ArrayList
包含项目。唯一的问题是 RecyclerView
从不显示任何内容。 RecyclerViewAdapter
的 onCreateViewHolder
和 onBindViewHolder
方法永远不会被调用。
我试过这两种布局的所有元素的 layout_width
和 layout_height
,我认为这是导致问题的原因。我还特意在 RecyclerViewAdapter
实例化之后和在 RecyclerView
中设置它之前调用了 notifyDataSetChanged()
。那里也没有快乐。
有没有人在这里看到任何可能导致 RecyclerView
不呈现的明显问题?
我也很好奇究竟是什么触发了适配器上这两个方法的调用...如果我能弄清楚,也许我可以确定为什么没有触发事件。
最佳答案
尝试在生命周期的早期将适配器设置为 RecyclerView(onCreate 是一个不错的选择),并且当您的 AsyncTask 完成时仅通过 notifyDataSetChanged()
此外,请确保设置 LayoutManager,如下所示:
recycler.setLayoutManager(new LinearLayoutManager(this));
由于您的 RecyclerView 存在于 fragment 中,因此在您的情况下将是:
recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
关于你的问题
I'm also curious as to what actually triggers the calling of these two methods on the adapter... if I can figure that out, maybe I can determine why the event isn't being triggered.
当渲染列表的时间到来时,它主要与您的数据集大小有关。在您的情况下(在我的情况下),缺少设置 LayoutManager 会以某种方式导致不呈现。
您已经在适配器中引用了上下文,您可以尝试这样做吗:
@Override
public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.todays_visit_list_item, parent, false); //
RecyclerViewAdapter.MyViewHolder holder = new RecyclerViewAdapter.MyViewHolder(v);
return holder;
}
关于android - RecyclerView 适配器 onCreateViewHolder 与 onBindViewHolder 未调用 : view empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38983373/
我有 3 个 AutoCompleteTextView,我想在它们上面注册 2 个 String[] 适配器。目前,我正在这样做: atw_from.setAdapter(new ArrayAdapt
我需要实现一个 recyclerView 来显示我对 Parse 的查询,所以我已经做到了: private class Pagination extends RecyclerView.OnScro
我对 BizTalk 相当陌生,目前我只是探索它的功能并了解不同部分(架构、编排、端口等)如何协同工作。我对其适配器有疑问: 不同的适配器是否已经随 BizTalk 服务器安装一起预装并准备好配置,或
我在 BizTalk 中测试 MQSC 适配器以与 Z/OS 主机上的队列通信时遇到问题。 测试场景:通过 Biztalk 发送消息时,我(强制)停止并启动主机 channel ,以模拟主机 IPL。
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我想用我的音频单元在iPhone上录制一条音频信号,该信号来自一条普通的3.5毫米音频电缆(例如,另一部iPhone作为声源)。 由于iPhone具有4端口耳机插孔,因此无法直接插入。 我尝试了不同种
[请参阅下面的更新] 我很难定义模式。我的同事说这是适配器模式。我不知道。我们陷入困境主要是因为我们想要正确命名我们的组件。 问题:是适配器模式吗?如果不是的话是什么?如果是其他事情,这是实现这个想法
我有点不熟悉Java KeyAdapter有效,并且使用 KeyAdapter 使用以下代码得到了意想不到的结果。当按下一个键而另一个键已按下时,就会出现此问题,无论 isKeyPressed() 是
我想知道如何通过 ORM 适配器使用 Node.js 在 MySQL 中创建多个表。我通过模型创建了一个表,即“us.js” module.exports = { identity: 'us'
我有一个 JavaFx 客户端。我正在使用一个具有 ObservableSet 作为字段的 bean 作为模型。我想将这些数据显示到 ListView 中,但我无法将我的字段类型更改为 Observa
我正在尝试在 native iOS 应用程序中实现基于表单的身份验证,但我需要在没有收到质询的情况下登录,我想打开一个表单并登录。我实现了一个包含 isCustomResponse 函数的 Chall
我正在尝试为我的迭代器和 const_iterator 类实现反向迭代器适配器,但遇到了一些麻烦。如果有人可以指导我解决这个问题,将不胜感激! 我的想法是我应该能够从我的 rbegin() 和 ren
使用 spring-integration-sftp,创建任意数量的入站 channel 适配器对象的推荐方法是什么?我的应用程序需要监视多个远程目录(1 到 n),直到运行时才知道。 最佳答案 当前
我正在尝试为我们自己的框架创建适配器。我们的框架使用自己的断言机制,因此我需要编写适配器。 适配器类非常简单,如下所示: public class AllureReportListener {
有没有什么方法可以使用命令行而不是使用 Worklight 控制台来部署 Worklight 适配器? (因为我的 worklight 服务器安装在 WAS 上,wsadmin 命令或类似的命令...
我想构建自己的自定义 log4j(网络)适配器来解决我的问题 that I posted here. 我查看了 log4j 的文档,但看不到开发人员在哪里/是否讨论如何执行此操作。 有人能给我指出正确
我使用消息驱动 channel 适配器从 weblogic JMS 队列接收作为字符串的 xml 消息,然后将此消息传递到 spring 集成 channel 以存储到数据库中,转换为不同的 xml,
有没有什么方法可以使用命令行而不是使用 Worklight 控制台来部署 Worklight 适配器? (因为我的 worklight 服务器安装在 WAS 上,wsadmin 命令或类似的命令...
我试图为 Android 制作一个聊天应用程序,所以我使用了 RecyclerView 。我的适配器有问题,我的聊天室收到的 JSON 响应显示为空白。我的代码是否遗漏了某些内容? 这是我的适配器类
如果这是重复的,我提前道歉。我对 Android 开发还是新手,并尝试寻找解决方案,但找不到有效的解决方案。 我正在创建一个待办事项应用程序并在我的适配器中收到此错误。 java.lang.NullP
我是一名优秀的程序员,十分优秀!