滚动回来时,Android RecyclerView没有保存其状态

时间:2015-05-27 06:14:39

标签: android android-recyclerview recycler-adapter

我有一个recyclelerview,其中onLongClick()项目,我正在显示一个按钮。但是when scroll down the recycler view and scrolling back, that button is showing on top of another item or sometimes it is not showing。 这是我的代码

public static class TextViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener{
    public LinearLayout enq_layout;
    public LinearLayout item_layout;
    public TextView enquire;
    public int position;

    public TextViewHolder(View itemView) {
        super(itemView);
        item_layout= (LinearLayout) itemView.findViewById(R.id.item_layout);
        enq_layout= (LinearLayout) itemView.findViewById(R.id.enq_layout);
        enquire=(TextView) itemView.findViewById(R.id.enquire);
        //position=getLayoutPosition();
    }
}

@Override
public int getItemViewType(int position) {
    return product.get(position)!=null? VIEW_ITEM: VIEW_PROG;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.cardlayout_product, parent, false);
        RecyclerView.ViewHolder vh = new TextViewHolder(v);
    return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
   // Toast.makeText(act, "onBindViewHolder" +position, Toast.LENGTH_LONG).show();
        final ProductDetails item = product.get(position);
        final TextViewHolder hold=((TextViewHolder)holder);

        //hold.position=position;
      //  hold.item_layout.setTag(position);

        hold.item_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             // go to next activity
            }
        });
        hold.item_layout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
               // show enquiry button
                hold.enq_layout.setVisibility(View.VISIBLE);
            }
        });
        hold.enquire.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               //do some operation
                int productid = Integer.parseInt(product.get(item.getPosition()).getProduct_id());
            }
        });
}
@Override
public int getItemCount() {
    return product.size();
}

我试过Why doesn't RecyclerView have onItemClickListener()? And how RecyclerView is different from Listview?,但我无法访问onCreateViewHolder onclick方法中的视图。

3 个答案:

答案 0 :(得分:0)

你将不得不创建一个名为boolean shouldShowView的全局变量,如下所示:

public static class TextViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener{
    public LinearLayout enq_layout;
private boolean shouldShowView;

然后在onBindViewHolder中使用可以使用以下代码:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
   // Toast.makeText(act, "onBindViewHolder" +position, Toast.LENGTH_LONG).show();
        final ProductDetails item = product.get(position);
        final TextViewHolder hold=((TextViewHolder)holder);

if (shouldShowView) {
    hold.enq_layout.setVisibility(View.VISIBLE);
    } else {
    hold.enq_layout.setVisibility(View.INVISIBLE);
    }

然后在onLongClick方法中,如果长按一下,只需将shouldShowView变量设置为true即可。如果要隐藏按钮,还应该在其他位置将其设置为false。您可能还必须跟踪在全局变量状态中选择的项目,并检查当前位置是否等于所选位置。

您可能还想在点击侦听器下面移动shouldShowView检查。

如果您有任何问题,请与我们联系。

答案 1 :(得分:0)

你可以试试这个

        public View getViewByPosition(int position, ListView lv1) {
        final int firstListItemPosition = lv1.getFirstVisiblePosition();
        final int lastListItemPosition = firstListItemPosition +     
        lv1.getChildCount() - 1;
        if (position < firstListItemPosition || position >  
       lastListItemPosition ) {
            return lv1.getAdapter().getView(position, null, lv1);
        } else {
            final int childIndex = position - firstListItemPosition;
            return lv1.getChildAt(childIndex);
        }
    }

在你声明它就像这样

             selectedids ="";
                for (int i = 0; i < len; i++){

                    //View childv = lv1.getChildAt(i);
                    View childv=getViewByPosition(i, lv1);

答案 2 :(得分:0)

我遇到了同样的问题并解决了这个问题。您需要使用RecyclerView的方法并设置项目缓存大小:                   mRecyclerView.setItemViewCacheSize(300);