Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取onCreateViewHolder中的视图位置_Java_Android_Android View_Android Recyclerview_Android Viewholder - Fatal编程技术网

Java 获取onCreateViewHolder中的视图位置

Java 获取onCreateViewHolder中的视图位置,java,android,android-view,android-recyclerview,android-viewholder,Java,Android,Android View,Android Recyclerview,Android Viewholder,我使用的是一个带有单行布局的RecyclerView,它有一个ImageView和一个TextView 我想为视图而不是单独的ViewHolder对象实现OnClickListener。如何获取适配器中视图的位置 现在我正在删除单击时的评论,但我无法选择单击的视图。我在适当的行中添加了TODO public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.ViewHolder> { /** L

我使用的是一个带有单行布局的RecyclerView,它有一个ImageView和一个TextView

我想为视图而不是单独的ViewHolder对象实现OnClickListener。如何获取适配器中视图的位置

现在我正在删除单击时的评论,但我无法选择单击的视图。我在适当的行中添加了TODO

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

    /** List of Comment objects */
    private List<Comment> mCommentList;

    /** Database with Comment objects */
    private CommentsDataSource mDataSource;

    /**
     * Construcutor for CommentAdapter
     *
     * @param commentList   List of Comment objects
     * @param dataSource    Database with Comment objects
     */
    public CommentAdapter(List<Comment> commentList, CommentsDataSource dataSource) {
        this.mCommentList = commentList;
        this.mDataSource = dataSource;
    }

    /**
     * Add Comment objects to RecyclerView
     *
     * @param position  The position where the Comment object is added
     * @param comment   Comment Object
     */
    public void add(int position, Comment comment) {
        mCommentList.add(position, comment);
        notifyItemInserted(position);
    }

    /**
     * Remove Comment objects from RecyclerView
     *
     * @param comment Comment Object
     */
    public void remove(Comment comment) {
        int position = mCommentList.indexOf(comment);
        // Avoid double tap remove
        if (position != -1) {
            mCommentList.remove(position);
            mDataSource.deleteComment(comment);
            notifyItemRemoved(position);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.single_line_row, parent, false);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO get position
                remove(mCommentList.get(getItemCount() - 1));
            }
        });
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Comment comment = mCommentList.get(position);
        holder.comment.setText(comment.getComment());
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {

        /** ImageView icon */
        public ImageView icon;

        /** TextView comment */
        public TextView comment;

        /**
         * Constructor for ViewHolder
         *
         * @param itemView Layout for each row of RecyclerView
         */
        public ViewHolder(final View itemView) {
            super(itemView);
            icon = (ImageView) itemView.findViewById(R.id.icon);
            comment = (TextView) itemView.findViewById(R.id.comment);
        }
    }
}
公共类CommentAdapter扩展了RecyclerView.Adapter{
/**注释对象列表*/
私有列表mCommentList;
/**带有注释对象的数据库*/
私有评论数据源mDataSource;
/**
*注释适配器的构造器
*
*@param commentList注释对象列表
*@param dataSource数据库和注释对象
*/
公共CommentAdapter(列表commentList,CommentsDataSource数据源){
this.mCommentList=commentList;
this.mDataSource=数据源;
}
/**
*将注释对象添加到RecyclerView
*
*@param position添加注释对象的位置
*@param注释对象
*/
公共无效添加(整型位置,注释){
mCommentList.add(位置、注释);
(位置);
}
/**
*从RecyclerView中删除注释对象
*
*@param注释对象
*/
公共无效删除(注释){
int position=mCommentList.indexOf(注释);
//避免双重抽头拆卸
如果(位置!=-1){
mCommentList.移除(位置);
mDataSource.deleteComment(comment);
已移除(位置)的项目;
}
}
@凌驾
public ViewHolder onCreateViewHolder(最终视图组父级,int-viewType){
最终视图=LayoutInflater.from(parent.getContext())
.充气(右布局、单行、父级、假);
view.setOnClickListener(新的view.OnClickListener(){
@凌驾
公共void onClick(视图v){
//待办事项
删除(mCommentList.get(getItemCount()-1));
}
});
返回新的ViewHolder(视图);
}
@凌驾
公共无效onBindViewHolder(ViewHolder,int位置){
最终评论=mCommentList.get(位置);
holder.comment.setText(comment.getComment());
}
@凌驾
public int getItemCount(){
返回mCommentList.size();
}
公共静态类ViewHolder扩展了RecyclerView.ViewHolder{
/**图像视图图标*/
公共图像视图图标;
/**文本视图注释*/
公共文本视图评论;
/**
*ViewHolder的构造函数
*
*@param itemView每行的布局
*/
公共视图持有者(最终视图项视图){
超级(项目视图);
icon=(ImageView)itemView.findViewById(R.id.icon);
comment=(TextView)itemView.findViewById(R.id.comment);
}
}
}

不能在回调中使用
onBindViewHolder
position
参数。 如果在上面添加了一个新项目,RecyclerView将不会重新绑定您的项目,因此该位置已过时。 相反,RecyclerView在ViewHolder上提供了一个
getAdapterPosition
方法

@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
    final View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.single_line_row, parent, false);
    final ViewHolder holder = new ViewHolder(view);
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final int position = holder.getAdapterPosition();
            if (position != RecyclerView.NO_POSITION) {
                remove(mCommentList.get(position));
            }
        }
    });
    return holder;
}

我添加了
位置!=RecyclerView.NO_POSITION
检查,因为当项目被删除时,RecyclerView将淡出视图,因此用户仍可以单击它,但其适配器位置将返回适配器中的
NO_POSITION
覆盖getItemViewType方法:

@Override
public int getItemViewType(int position) {
    //...
    return position;
}
现在视图类型是位置

 @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       int position=viewType; //position 
        //your code 

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recursive, parent, false);
        return new ViewHolder(view);
    }
比如说

public class BrandBtnAdapter extends RecyclerView.Adapter<BrandBtnAdapter.MyViewHolder>
{

     //............

    @Override
    public int getItemViewType(int position)
    {
        //...
        return position;
    }

    @Override
    public BrandBtnAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        int position = viewType; //position 

        final View itemView = mInflater.inflate(mResource, parent, false);
        return new BrandBtnAdapter.MyViewHolder(itemView);
    }

}
公共类BrandBtnAdapter扩展了RecyclerView.Adapter
{
//............
@凌驾
public int getItemViewType(int位置)
{
//...
返回位置;
}
@凌驾
public BrandBtnAdapter.MyViewHolder onCreateViewHolder(视图组父级,int-viewType)
{
int position=viewType;//位置
最终视图itemView=mInflater.flate(mResource,parent,false);
返回新的BrandBtnAdapter.MyViewHolder(itemView);
}
}

您可以创建一个方法来更新类中的位置

在我的情况下,我需要附加
watcher
,并获得更新
arraylist
的位置。以下是一个例子:

class DodolWatcher bla bla {
    private var position: Int = 0
    fun updatePosition(pos:Int)
    {
      position = pos
    }

    override fun onTextChanged(charSequence: CharSequence, i: Int, i2: Int, i3: Int) {
    Log.d("test", position.toString())
    }

}
在您的
onCreateViewHolder
中,您可以将watcher附加到
edittext

 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RVPaymentMethodAdapter.ViewHolder {
     ....
     bla bla
     ....
     theWatcher = DodolWatcher() <-- this is the trick
     amount.addTextChangedListener(theWatcher)
 }

您应该在ViewHolder(最终视图项视图)构造函数中设置OnClickListener,在这种情况下,ViewHolder应该实现OnClickListener无论我单击什么,我都会得到位置-1(RecyclerView.NO_位置),会出现什么问题?如果在
视图上设置
OnClickListener
支架
,这有关系吗?我先将它链接到持有者,然后在看到此答案后将其更改为视图,但这似乎没有实际意义-至少对于不可滚动的RecycleView(没有足够的项目)来说不是这样的。不要这样做-它将使适配器为列表中的每个项目创建一个新视图,而不是循环它们,因为它会认为它们都是不同类型的。这样做很糟糕。
override fun onBindViewHolder(viewHolder: RVPaymentMethodAdapter.ViewHolder, position: Int) {
     theWatcher.updatePosition(viewHolder.adapterPosition)  <-- this is the trick
 }