标签: android-viewholder

ViewHolder不是内部类

可以RecyclerView.ViewHolder不用作内部类吗?这样做有什么问题吗?

我已经四处搜索但没有找到任何文件!

android android-viewholder recycler-adapter android-recyclerview

4
推荐指数
1
解决办法
1315
查看次数

如何在列表视图中使用Android DataBinding并仍然使用ViewHolder模式?

我有一个Android活动,它从适配器类中的可观察列表中提取数据.

getView()在我的适配器类的方法是:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (inflater == null) {
        inflater = (LayoutInflater) parent.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    // Perform the binding
    ActivityTeamMessageListRowBinding binding = DataBindingUtil.inflate(inflater, R.layout.my_activity_list_row, parent, false);
    binding.setInfo(list.get(position));
    binding.executePendingBindings();

    // Return the bound view
    return binding.getRoot();
}
Run Code Online (Sandbox Code Playgroud)

这很有效.但是,我看到了Android的警告从视图适配器无条件布局通胀ActivityTeamMessageListRowBinding binding ...线.

我一直在研究ViewHolders来解决这个问题,但我似乎无法弄清楚如何完成这个并仍然使用我的数据绑定.并且可能是,列表越长,我在不使用ViewHolder的情况下看到的性能就越差.

有谁知道如何扩展我的getView(...)代码以合并视图绑定器?我的my_activity_list_row.xml中有3 TextView秒和1 .ImageView

java data-binding android listview android-viewholder

4
推荐指数
1
解决办法
6228
查看次数

如何将OnClickListener设置为RecyclerView中的两个按钮?

在我的Android应用程序中,我有一个活动来显示RecyclerView,其中每行由TextView和2个按钮组成.就像那样:

ListAdapter布局

好吧,经过互联网的许多解释,我已经制作了这个适配器:

public class ListAdapter extends 
RecyclerView.Adapter<ListAdapter.ViewHolder> {

    private LayoutInflater layoutInflater;
    protected Vector<List> lists;

    public ListAdapter(Context context, Vector lists) {
        layoutInflater = (LayoutInflater) 
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.lists = lists;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.listadapter, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        List list = lists.elementAt(position);
        holder.name.setText(list.getName());
        //holder.delete.set HOW DO I GET BUTTON HERE?
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-viewholder recycler-adapter android-recyclerview

4
推荐指数
1
解决办法
4478
查看次数

RecyclerView ViewHolder 中 ConstraintLayout 的性能

过去 2 天我一直在尝试对为什么我的 RecyclerView 在滚动时如此缓慢地进行分类,并将其缩小到我用于行的 ConstraintLayout。在 android 上使用 GPU 分析器显示绿色/蓝绿色条一直到屏幕顶部,表明严重卡顿。很明显有什么地方不对劲。

这是我的观察者的样子:

class MyViewHolder(

    override val containerView: View) : RecyclerView.ViewHolder(containerView), LayoutContainer {
        fun bindTo(item: Item?, clickListener: (Item?) -> Unit) {
            text_item_name.text = item?.name
            Glide.with(containerView.context).load(item?.url).into(image_item)

            containerView.setOnClickListener { clickListener(item) }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这就是我的布局的样子。尽可能简单:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:paddingBottom="@dimen/padding"
    android:paddingEnd="@dimen/padding_2x"
    android:paddingStart="@dimen/padding_2x"
    android:paddingTop="@dimen/padding">

    <ImageView
        android:id="@+id/image_item"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginEnd="@dimen/margin_2x"
        android:layout_marginStart="@dimen/margin_2x"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text_coin_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/margin_2x"
        android:layout_marginStart="@dimen/margin_2x"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@id/image_item"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:我的布局有什么问题导致了卡顿?我是否使用了不正确的 LayoutManager?约束是否会导致透支?

如果我将布局 XML …

android android-viewholder android-recyclerview android-constraintlayout

4
推荐指数
1
解决办法
4976
查看次数

ClassCastException 自定义 ViewHolder 无法转换为另一个 Viewholder

我的项目应该根据用户输入(用户发送文本、拍照或录制视频)创建多个视图,类似于 WhatsApp 聊天活动,几乎相同的结构。适配器应该能够通过使用 getItemViewType() 来识别该项目是哪种类型的视图。如果尝试使用视频录制功能,它可以工作,但如果我尝试拍照或发送文本,应用程序会崩溃并出现以下错误:

java.lang.ClassCastException:AppPackage.myAdapter$TextViewHolder 无法转换为 AppPackage.myAdapter$ImageViewHolder

从输入中获取的所有数据都会添加到列表中,然后传递给 onBindViewHolder

已经尝试将 onBindViewHolder( CustomViewHolder myViewHolder, intposition) 更改为 onBindViewHolder(RecyclerView.ViewHolder, intposition) 并在开关的每个“case”处添加中断。还是行不通


适配器

public class myAdapter extends   
RecyclerView.Adapter<RecyclerView.ViewHolder> {

List<ModelloDati> lista;
Context context;


public class ImageViewHolder extends RecyclerView.ViewHolder {
    //ImageView mImage;
    private ImageView imageView;
    public ImageViewHolder(View itemView) {
        super (itemView);
        imageView = itemView.findViewById(R.id.immagine);
    }
}

public class TextViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    private TextView titolo;

    public TextViewHolder(View v) {
        super(v);
        titolo = itemView.findViewById(R.id.testo); …
Run Code Online (Sandbox Code Playgroud)

android android-adapter android-viewholder android-recyclerview

4
推荐指数
1
解决办法
1827
查看次数

RecyclerView 执行项目单击

我有一个RecyclerView包含可扩展项目的。单击某个项目可将其展开。问题是它还意外地扩展了一些其他卡。我检查了所有内容,但找不到为什么会发生这种情况,但我确实设法发现单击的项目总是以某种方式与其他展开的项目具有相同的 id。仅当列表足够大时才会发生错误,因此我认为这与 s 功能有关RecyclerView。也使用notifyDataSetChanged()作品,但它消除了动画,我希望布局是动画的......

这个问题似乎讨论了我面临的同样的问题......但我不知道如何解决它。

我不明白为什么会发生这种情况或如何解决这个问题...下面是一些图像和代码,可以帮助您更好地理解,也许看看问题是否出在代码中...

这是RecyclerView

在此输入图像描述

扩展的卡片项目如下所示:

在此输入图像描述

这是我的适配器类:

public class ActiveGoalsAdapter extends RecyclerView.Adapter<ActiveGoalsAdapter.ActiveGoalsViewHolder> {

    private Context context;
    private Cursor cursor;
    private ArrayList<Goal> activeGoals;
    private static boolean[] openedFromParent = new boolean[]{false, true}, editing = new boolean[]{false};

    public ActiveGoalsAdapter(Context context, ArrayList<Goal> activeGoals, Cursor cursor) {
        this.context = context;
        this.activeGoals = activeGoals;
        this.cursor = cursor;
    }

    public class ActiveGoalsViewHolder extends RecyclerView.ViewHolder {

        public LinearLayout shrunkContainer, subGoalsTitleContainer;
        public RelativeLayout expandedContainer, subGoalsRecyclerViewContainer, btnDelete, btnCancel, btnSave; …
Run Code Online (Sandbox Code Playgroud)

java android android-layout android-viewholder android-recyclerview

4
推荐指数
1
解决办法
429
查看次数

有没有办法使用 PagingDataAdapter 查找项目并滚动到该视图?

我正在使用 Android Paging Library 3. 有没有办法使用 PagingDataAdapter 查找项目并滚动到该视图?因为我无法访问项目列表。或者我别无选择,只能使用普通的列表适配器?

android kotlin android-viewholder android-recyclerview android-paging

4
推荐指数
1
解决办法
369
查看次数

Binding.root 表示什么?

以下是我的代码片段。

class DashBoardHolder(val binding: ActivityDashBoardBinding) : RecyclerView.ViewHolder(binding.root) {
    internal var tvName: TextView = itemView.findViewById(R.id.textViewGrandrukName)
Run Code Online (Sandbox Code Playgroud)

android adapter kotlin android-viewholder android-recyclerview

4
推荐指数
1
解决办法
3723
查看次数

RecyclerView.Adapter中的ViewHolder并非特定于位置

以下是我的代码onBindViewHolder(内部MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>)的一部分

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    StatusItem item = mDataset.get(position);
    //......
    //Add content and timing to the textview
    String content = item.getContent();

    holder.mTextViewTime.setText(timing);
    //Set the img
    holder.imgViewIcon.setImageDrawable(item.getProfileDrawable());
    //Set content image (for Instagram)
    holder.mImageViewContentPic.setImageDrawable(item.getContentDrawable());
    //HIDE THE VIEW Start
    if(item.getContentDrawable() == null){
        holder.mImageViewContentPic.setVisibility(View.GONE);
    }
    //HIDE THE VIEW End
}
Run Code Online (Sandbox Code Playgroud)

该部分HIDE THE VIEW未按预期工作.当我向下滚动时,视图正常工作.但是,当我开始向上滚动,即重新访问先前的视图时,应该VISIBLE成为的ImageViews GONE,虽然我检查了我的数据集并验证它没有被修改.尝试在视图上调用其他方法也会产生不稳定的结果(数据集中的位置和项目不匹配).

似乎视图持有者没有绑定到RecyclerView内的特定位置.

如果删除HIDE THE VIEW …

android android-viewholder android-5.0-lollipop android-recyclerview

3
推荐指数
1
解决办法
4226
查看次数

如何在回收站视图中访问项目的视图?

我有一个回收站视图,显示图像列表.我在该回收站视图中添加了一个onScrollListener.

现在,当检测到滚动时,我想在视图中设置imageviews的转换.因此,我需要参考基础视图持有者.

我怎么能得到那些?

在代码中(这不起作用,但你会明白我想要实现的目标):

ImageView imageView = (ImageView) ((RecyclerFragmentAdapter.MyViewHolder) 
    mRecyclerView.getChildAt(i)).itemView.findViewById(R.id.imageView);
// ... Modify imageview, i.e. imageView.setImageMatrix(customImageMatrix);
Run Code Online (Sandbox Code Playgroud)

animation android android-viewholder android-recyclerview

3
推荐指数
1
解决办法
3078
查看次数