getAdapterPosition未在我的适配器

时间:2016-05-20 15:52:38

标签: android android-recyclerview recycler-adapter

我正在处理onBindViewHolder中的点击事件,但我在此处看到这不是最佳做法,所以我想切换到ViewHolder

我遇到的问题是每次点击一个项目,即使我点击的项目是第一项,也会选择recyclerview中的最后一项。

我尝试使用getAdapterPosition,但我弄错了,而且它没有返回点击项目的位置。

这是我的代码

public classTreeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{


    private ImageLoader imageLoader;
    private static Context mContext;
    public static int aposition;

    //List oftrees
    private List<TreeItems> mTreeItems;

    private final int VIEW_ITEM = 0;
    private final int VIEW_PROG = 1;

    private int lastPosition = -1;

   publicTreeAdapter(List<TreeItems>treeItems, Context context) {
       super();

       //Getting alltrees
       this.mTreeItems =treeItems;
       this.mContext = context;
   }

    @Override
    public int getItemViewType(int position) {
        if (isPositionItem(position))
            return VIEW_ITEM;
        return VIEW_PROG;
    }

    private boolean isPositionItem(int position) {
       return position != getItemCount()-1;
    }



    @Override
    public RecyclerView.ViewHolder  onCreateViewHolder (ViewGroup parent, int viewType) {
        if (viewType == VIEW_ITEM) {
            View v =  LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.tree_summ, parent, false);
            return new TextViewHolder(v);
        } else if (viewType == VIEW_PROG){
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.recyclerfooter, parent, false);
            return new ProgressViewHolder(v);
        }

        return null;
    }



    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof TextViewHolder) {

            finalTreeItemstreeList = mTreeItems.get(position);
            //imageLoader = VolleyRequest.getInstance(mContext).getImageLoader();
            //imageLoader.get(treeList.getTree_image(), ImageLoader.getImageListener(((TextViewHolder) holder).treeImage, R.drawable.default_thumb, R.drawable.error_thumb));

            //((TextViewHolder) holder).treeImage.setImageUrl(treeList.getTree_image(), imageLoader);
            ((TextViewHolder) holder).treeTitle.setText(Html.fromHtml(treeList.getTree_title()));
            ((TextViewHolder) holder).treeBody.setText(Html.fromHtml(treeList.getTree_body()));
            ((TextViewHolder) holder).treeAuthor.setText(treeList.getTree_author());
            ((TextViewHolder) holder).treeDate.setText(treeList.getTree_date());
//
//            ((TextViewHolder) holder).treeBody.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Intent intent = new Intent(mContext,Tree.class);
//                    intent.putExtra("TreeId",treeList.getTreeId());
//                    mContext.startActivity(intent);
//                }
//            });
//
//            ((TextViewHolder) holder).treeTitle.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Intent intent = new Intent(mContext,Tree.class);
//                    intent.putExtra("TreeId",treeList.getTreeId());
//                    mContext.startActivity(intent);
//                }
//            });
//



        } else {
            ((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
            ((ProgressViewHolder) holder).loadButton.setText(R.string.reload);
        }


    }




    @Override
    public int getItemCount(){
        //Return the number of items in the data set
        return mTreeItems.size();
    }



    public static class TextViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public NetworkImageViewtreeImage;
        public TextViewtreeTitle,treeAuthor,treeDate,treeBody;
        public ImageButton imageButton;

        public TextViewHolder (final ViewtreeView) {
            super(treeView);
           treeImage = (NetworkImageView)treeView.findViewById(R.id.tree_image);
           treeTitle = (TextView)treeView.findViewById(R.id.tree_title);
           treeBody = (TextView)treeView.findViewById(R.id.tree_body);
           treeAuthor = (TextView)treeView.findViewById(R.id.tree_author);
           treeDate = (TextView)treeView.findViewById(R.id.tree_date);
            imageButton = (ImageButton)treeView.findViewById(R.id.arrow_button);
            imageButton.setOnClickListener(this);
           treeTitle.setOnClickListener(this);
           treeBody.setOnClickListener(this);
        }



        @Override
        public void onClick(View v) {

        aposition = getAdapterPosition();

            if (v.getId() == imageButton.getId()) {
                Intent intent = new Intent(mContext,Tree.class);
                intent.putExtra("TreeId",TreeItems.getTreeId());
                mContext.startActivity(intent);

            } else if (v.getId() ==treeTitle.getId()) {

                Intent intent = new Intent(mContext,Tree.class);
                intent.putExtra("TreeId",TreeItems.getTreeId());
                mContext.startActivity(intent);

            } else if (v.getId() ==treeBody.getId()) {
                Intent intent = new Intent(mContext,Tree.class);
                intent.putExtra("TreeId",TreeItems.getTreeId());
                mContext.startActivity(intent);
            }
        }
    }

    public static class ProgressViewHolder extends RecyclerView.ViewHolder implements  View.OnClickListener{
        Button loadButton;
        ProgressBar progressBar;
        public ProgressViewHolder(View footerView){
            super(footerView);
            loadButton = (Button) footerView.findViewById(R.id.reload_button);
            progressBar = (ProgressBar) footerView.findViewById(R.id.progress_load);

            loadButton.setOnClickListener(this);

            if(NetworkCheck.isAvailableAndConnected(footerView.getContext())) {
                progressBar.setVisibility(View.VISIBLE);


            } else if (!NetworkCheck.isAvailableAndConnected(footerView.getContext())) {
                loadButton.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onClick(View v) {
            if (v.getId() == loadButton.getId()) {
                //
            }
        }
    }


}

请问,我该如何解决这个问题?

0 个答案:

没有答案