- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在处理具有不同项目膨胀的 Recyceview
。当我不将数据绑定(bind)到RecycleView
的onBindViewHolder
方法时,它不会崩溃..
但是当我在 onBindViewHolder
中绑定(bind)数据时,我得到了 Exception
,请检查我的代码,让我知道我哪里做错了。
package com.tv.practise.adapter;
/**
Created by Ravindra Kushwaha on 10/10/16.
*/
public class RecycleDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private ArrayList<RecycleBen> data;
public static class SimpleText extends RecyclerView.ViewHolder {
TextView first_data_tv;
public SimpleText(View v) {
super(v);
this.first_data_tv = (TextView) v.findViewById(R.id.first_data_tv);
}
}
public class SimpleImage extends RecyclerView.ViewHolder {
ImageView second_data_iv;
public SimpleImage(View v) {
super(v);
this.second_data_iv = (ImageView) v.findViewById(R.id.second_data_iv);
}
}
public class SimpleImageWithText extends RecyclerView.ViewHolder {
TextView third_data_tv;
ImageView third_iv;
public SimpleImageWithText(View v) {
super(v);
this.third_data_tv = (TextView) v.findViewById(R.id.third_data_tv);
this.third_iv = (ImageView) v.findViewById(R.id.third_iv);
}
}
public RecycleDataAdapter(Context mContext, ArrayList<RecycleBen> data) {
this.mContext = mContext;
this.data = data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
int listViewItemType = getItemViewType(viewType);
View itemView;
if(listViewItemType==1)
{
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycle_first_item, parent, false);
return new SimpleText(itemView
);
}
else if(listViewItemType==2)
{
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycle_fsecond_item, parent, false);
return new SimpleImage(itemView);
}
else
{
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycle_third_item, parent, false);
return new SimpleImageWithText(itemView);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
RecycleBen bean = data.get(position);
if(holder.getItemViewType()==1)
{
/////HERE I AM GETTING THE EXCEPTION WHILE BINDIND DATA
((SimpleText)holder).first_data_tv.setText(bean.getName());
}
else if(holder.getItemViewType()==2)
{
}
else {
/////HERE I AM GETTING THE EXCEPTION WHILE BINDIND DATA((SimpleImageWithText)holder).third_data_tv.setText(bean.getName());
}
}
@Override
public int getItemViewType(int position) {
return Integer.parseInt(data.get(position).getType_row());
}
@Override
public int getItemCount() {
return data.size();
}
}
异常 当我绑定(bind)数据时如下:-
3758-3758/com.tv.practise E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.tv.practise, PID: 3758
java.lang.ClassCastException: com.tv.practise.adapter.RecycleDataAdapter$SimpleImageWithText cannot be cast to com.tv.practise.adapter.RecycleDataAdapter$SimpleText
at com.tv.practise.adapter.RecycleDataAdapter.onBindViewHolder(RecycleDataAdapter.java:101)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2625)
at android.view.View.measure(View.java:18804)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18804)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18804)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18804)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18804)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2112)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1228)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1464)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6060)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
最佳答案
@Joshua 解决了我的问题..
对于其他用户,我在此处发布的是用于在 RecycleView
中膨胀不同行 (3 行) 的代码
它工作正常,请检查以下代码行:- 我在这里发布我的完整代码,请检查:-
这里 RecyleClass.java
是我的主课
package com.tv.practise.recycleview;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.tv.practise.R;
import java.util.ArrayList;
/**
* Created by Ravindra Kushwaha on 10/10/16.
*/
public class RecyleClass extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycle_main);
RecyclerView recycler_vw = (RecyclerView)findViewById(R.id.recycler_vw);
ArrayList<RecycleBen> arrayList = new ArrayList<>();
for (int i = 0;i<=25;i++)
{
RecycleBen bean = new RecycleBen();
if(i%2==0)
{
bean.setType_row("1");
bean.setName("First element");
bean.setImage_url("http://www.androhub.com/wp-content/uploads/2015/09/staggeredrecyclerview_banner.jpg");
}
else if(i%3==0)
{
bean.setType_row("2");
bean.setName("Second element");
bean.setImage_url("http://i.stack.imgur.com/snB84.png");
}
else
{
bean.setType_row("3");
bean.setName("Third element");
bean.setImage_url("http://inducesmile.com/wp-content/uploads/2015/05/gridbanner.jpg");
}
arrayList.add(bean);
}
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
recycler_vw.setLayoutManager(mLayoutManager);
recycler_vw.setItemAnimator(new DefaultItemAnimator());
recycler_vw.setAdapter(new RecycleDataAdapter(this, arrayList));
}
}
这是我的 layout
对于 RecyleClass.java
即 recycle_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_vw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
/>
</LinearLayout>
Recycleview
的
Gradles 条目与 CardView
和 Glide
// CardView
compile 'com.android.support:cardview-v7:23.4.0'
// RecyclerView
compile 'com.android.support:recyclerview-v7:23.4.0'
// For the glide libraray
compile 'com.github.bumptech.glide:glide:3.7.0'
下面是我的 getter
和 setter
类是 RecycleBen.java
package com.tv.practise.recycleview;
/**
* Created by Ravindra Kushwaha on 10/10/16.
*/
public class RecycleBen {
private String type_row;
private String name;
private String image_url;
public String getType_row() {
return type_row;
}
public void setType_row(String type_row) {
this.type_row = type_row;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage_url() {
return image_url;
}
public void setImage_url(String image_url) {
this.image_url = image_url;
}
}
最后我的适配器类是 RecycleDataAdapter.java
package com.tv.practise.adapter;
/**
* Created by Ravindra Kushwaha on 10/10/16.
*/
public class RecycleDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private ArrayList<RecycleBen> data;
public class SimpleText extends RecyclerView.ViewHolder {
TextView first_data_tv;
public SimpleText(View v) {
super(v);
this.first_data_tv = (TextView) v.findViewById(R.id.first_data_tv);
}
}
public class SimpleImage extends RecyclerView.ViewHolder {
ImageView second_data_iv;
ProgressBar second_pb;
public SimpleImage(View v) {
super(v);
this.second_data_iv = (ImageView) v.findViewById(R.id.second_data_iv);
this.second_pb = (ProgressBar)v.findViewById(R.id.second_pb);
}
}
public class SimpleImageWithText extends RecyclerView.ViewHolder {
TextView third_data_tv;
ImageView third_iv;
ProgressBar third_pb;
public SimpleImageWithText(View v) {
super(v);
this.third_data_tv = (TextView) v.findViewById(R.id.third_data_tv);
this.third_iv = (ImageView) v.findViewById(R.id.third_iv);
this.third_pb = (ProgressBar)v.findViewById(R.id.third_pb);
}
}
public RecycleDataAdapter(Context mContext, ArrayList<RecycleBen> data) {
this.mContext = mContext;
this.data = data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
if(viewType==1)
{
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycle_first_item, parent, false);
return new SimpleText(itemView
);
}
else if(viewType==2)
{
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycle_fsecond_item, parent, false);
return new SimpleImage(itemView);
}
else
{
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycle_third_item, parent, false);
return new SimpleImageWithText(itemView);
}
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
RecycleBen bean = data.get(position);
if(holder.getItemViewType()==1)
{
((SimpleText)holder).first_data_tv.setText(bean.getName());
}
else if(holder.getItemViewType()==2)
{
final SimpleImage simple_holder = (SimpleImage)holder;
simple_holder.second_pb.setVisibility(View.VISIBLE);
Glide.with(mContext)
.load(bean.getImage_url())
.fitCenter()
.crossFade()
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
if (e instanceof UnknownHostException)
simple_holder.second_pb.setVisibility(View.VISIBLE);
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
simple_holder.second_pb.setVisibility(View.GONE);
simple_holder.second_data_iv.setVisibility(View.VISIBLE);
return false;
}
}).into(simple_holder.second_data_iv);;
}
else {
final SimpleImageWithText third_holder = (SimpleImageWithText)holder;
third_holder.third_data_tv.setText(bean.getName());
third_holder.third_pb.setVisibility(View.VISIBLE);
Glide.with(mContext)
.load(bean.getImage_url())
.fitCenter()
.crossFade()
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
if (e instanceof UnknownHostException)
third_holder.third_pb.setVisibility(View.VISIBLE);
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
third_holder.third_pb.setVisibility(View.GONE);
third_holder.third_iv.setVisibility(View.VISIBLE);
return false;
}
}).into(third_holder.third_iv);;
}
}
@Override
public int getItemViewType(int position) {
return Integer.parseInt(data.get(position).getType_row());
}
@Override
public int getItemCount() {
return data.size();
}
}
当我们膨胀 Recycleview
中的不同行时, 所以我们在这里使用了 3 布局 ,即 recycle_first_item.xml
, recycle_fsecond_item.xml
和最后一个 recycle_third_item.xml
我正在一一展示所有布局 xml
, 如下:-
recycle_first_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/first_data_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="25sp"
android:text="Hello Card" />
</LinearLayout>
recycle_fsecond_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ProgressBar
android:id="@+id/second_pb"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
/>
<ImageView
android:id="@+id/second_data_iv"
android:layout_width="match_parent"
android:layout_height="120dp"
android:textColor="@android:color/black"
android:layout_centerInParent="true"
android:visibility="invisible"
android:src="@drawable/bubble1" />
</RelativeLayout>
</android.support.v7.widget.CardView>
recycle_third_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/third_data_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="25sp"
android:layout_centerInParent="true"
android:text="Hello Card" />
<ProgressBar
android:id="@+id/third_pb"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:layout_below="@+id/third_data_tv"
/>
<ImageView
android:id="@+id/third_iv"
android:layout_width="match_parent"
android:layout_below="@+id/third_data_tv"
android:layout_height="150dp"
android:src="@drawable/bubble2"
android:layout_centerInParent="true"
android:visibility="invisible"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
关于android - 回收 View 膨胀不同的行 :- Getting exception while binding the data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39971350/
我知道使用 GET 和 SET 函数的公共(public)变量的缺点/私有(private)变量的优点,但目前我正在使用 Ogre3D 开发自己的第一个“真实”游戏(C++)..同时,我有时需要 6-
我正在开发一个 GSM/GPRS 应用程序,它将每 10 秒报告一些值。我必须使用的 SIM 卡每月只有 15MB 可用数据。我使用的是 SIM900 GSM 芯片供您引用。 我到达服务器的方式是通过
这三者有什么区别:gets - 它获取带有 '\n' 的行gets.chomp - 它得到一行,但删除 '\n' 这样对吗? gets.chomp! 怎么样? 最佳答案 gets - 它得到一个末尾带
问题和我现在遇到的问题 脚本 顺便说一句,评论是挪威语的,如果它们看起来很奇怪哈哈 Connect-AzureAD #variabel $Users = Get-AzureADUser -All:$t
我现在面临的问题是获取一个 URL,如下所示: www.example.com/example.php?url=www.google.com 现在的问题是,如果我的网址中有一个 get,如下所示: w
我有一个 queryString 传递给 servlet 的 doGet() 方法,如下所示: count=9&preId0=-99&objId0=-99&preId1=-99&objId1=-99&
这是我在 Django 模板中的代码: {% for tag in tags %} {{ tag }} {% endfor %} 在view.py中: def tag_find(
我正在尝试在express.js中为我的网络应用程序创建一个路由系统,我需要知道是否需要使用app.get/post/put/delete.apply以编程方式设置多个功能对于一条路线。 也是如此 a
我正在通过示例查看 A.Mele Django,第 1 章 def post_list(request, category=None): object_list = Post.publishe
如果我想找到与IIS站点或应用程序关联的目录,我该怎么做? 我似乎无法从Get-Website和Get-WebApplication的对象的任何属性中找到任何允许我这样做的东西。 最佳答案 只需查看一
不知道发生了什么。当我执行以下代码时......它运行良好......但它产生了错误。如果我将以下内容粘贴到我的浏览器地址栏中并点击它,我会得到一个 URL。如果我通过 KRL http:get 输入
Curl 提供了一系列不同的带有 X 前缀的 http 方法调用,但也提供了不带 X 的相同方法。我两种都试过了,但我似乎无法弄清楚其中的区别。有人可以快速向我解释这两种操作有何不同吗? 最佳答案 默
request.GET.get 是什么意思?我在 Django 中看到类似的东西 page = request.GET.get('page', 1) 我认为它与类似的东西有关 « 它们是如
我正在从我的 Angular2 站点查询一些 Elasticsearch 服务器。为了帮助提高安全性,我们希望锁定对 GET 请求的访问权限。 Elasticsearch 支持带主体的 GET,但我在
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 4年前关闭。 Improve t
调用 HTable.get(List) 返回的 Result 数组的顺序是什么? ? 我的意思是,假设与输入列表的顺序相同是否正确? 最佳答案 结果数组中的顺序将与输入列表的顺序相同。与批处理方法一样
所以我有一个看起来像这样的 JSON 数组: var myData = { foo : { biz : 'baz', fig : 'tree' } }
我正在学习 Ajax、javascript 和 html,并且有一个应用程序可以触发“get”请求,然后再触发另一个“get”请求。这些请求是用户按下按钮的结果。在我的 servlet 中,我使用 T
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 6 年前。 Improv
运行以下 cmdlet 适用于组成员(Amer 域中的组)中的所有用户,无论列出的用户位于哪个域: Get-ADGroupMember -Server amer 但是,当尝试通过管道传输到 Get-
我是一名优秀的程序员,十分优秀!