0

如何在使用分页单击项目时在回收站视图中获取项目的位置?

示例:如果我点击电影 10,Toast 消息应该显示
“点击项目 10”

4

4 回答 4

1

将接口变成适配器类,用于回收器视图项上的单击事件。

    onItemClickListner onItemClickListner;

public void setOnItemClickListner(CommentsAdapter.onItemClickListner onItemClickListner) {
    this.onItemClickListner = onItemClickListner;
}

public interface onItemClickListner {
    void onClick(int position);//pass your object types.
}

@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
    // below code handle click event on recycler view item.
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onItemClickListner.onClick(position); //pass any data
        }
    });
}

在该适配器类绑定到 recyclerview 并确保适配器不为空之后,然后调用下面的代码..

        adapter.setOnItemClickListner(new CommentsAdapter.onItemClickListner()    {
        @Override
        public void onClick(int position) {
       // pass data get here   
          Toast.makeText(getApplicationContext(),""+position,Toast.LENGTH_LONG).show();
        }
    });
于 2018-11-23T09:05:01.337 回答
1
  // Used to cache the views within the item layout for fast access
 public class ViewHolder extends RecyclerView.ViewHolder implements 
                     View.OnClickListener {
    public TextView tvName;
    public TextView tvHometown;
    private Context context;

    public ViewHolder(Context context, View itemView) {
        super(itemView);
        this.tvName = (TextView) itemView.findViewById(R.id.tvName);
        this.tvHometown = (TextView) itemView.findViewById(R.id.tvHometown);
        // Store the context
        this.context = context;
        // Attach a click listener to the entire row view
        itemView.setOnClickListener(this);
    }

    // Handles the row being being clicked
    @Override
    public void onClick(View view) {
        int position = getAdapterPosition(); // gets item position
        if (position != RecyclerView.NO_POSITION) { // Check if an item was deleted, but the user clicked it before the UI removed it
            User user = users.get(position);
            // We can access the data within the views
            Toast.makeText(context, tvName.getText(), Toast.LENGTH_SHORT).show();
        }
    }
}

了解更多信息链接

于 2018-11-23T10:30:40.247 回答
0

在您的适配器中试试这个

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

    holder.itemView.setOnClickListener {

        // Do something here.

    }
于 2018-11-23T09:05:50.007 回答
0

制作一个用于加载下一页项目的界面

public interface LoadMoreItems {
  void LoadItems();
}

然后添加 addOnScrollListener 以在视图结束时加载更多项目

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView,
                                   int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                totalItemCount = linearLayoutManager.getItemCount();
                lastVisibleItem = linearLayoutManager
                        .findLastVisibleItemPosition();
                if (!loading
                        && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                    //End of the items
                    if (onLoadMoreListener != null) {
                        onLoadMoreListener.LoadItems();
                    }
                    loading = true;

                }
            }
        });

添加 onCreateViewHolder() 是我放置 ProgressBar 的位置。

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                  int viewType) {
    RecyclerView.ViewHolder vh;
    if (viewType == VIEW_ITEM) {
        View v = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.list_row, parent, false);

        vh = new StudentViewHolder(v);
    } else {
        View v = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.progressbar_item, parent, false);

        vh = new ProgressViewHolder(v);
    }
    return vh;
}

我将 LoadItems() 添加到其他项目的 MainActivity 是

mAdapter.setOnLoadMoreListener(new LoadMoreItems() {
        @Override
        public void LoadItems() {
            DataItemsList.add(null);
            mAdapter.notifyItemInserted(DataItemsList.size() - 1);

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //   remove progress item
                    DataItemsList.remove(DataItemsList.size() - 1);
                    mAdapter.notifyItemRemoved(DataItemsList.size());
                    //add items one by one
                    //When you've added the items call the setLoaded()
                    mAdapter.setLoaded();
                    //if you put all of the items at once call
                    // mAdapter.notifyDataSetChanged();
                }
            }, 2000); //time 2 seconds

        }
    });

刚刚关注了这个Github存储库(注意:这是使用 AsyncTask 可能对我的回答很有用

从此答案中赋予属性

于 2018-11-23T10:27:41.647 回答