滚动列表视图后如何设置按钮上的焦点(设置可见性)

时间:2018-10-04 04:36:54

标签: android listview

我是Android新手,我使用带有一个文本视图和button的列表视图。我正在执行一些可见性操作,其中在单击button1后,它将隐藏button1并显示button2。

设置可见性 View.GONE View.VISIBLE 可以正常工作,但是当我神奇地滚动列表视图时出现问题,它正在将所有button1重置为 View可见状态。

请任何人帮助我克服这个问题。

代码:

 @Override
public View getChildView(final int i, final int i1, boolean b, View 
 view, ViewGroup viewGroup) {

    if(view == null) {
        holder =  new ViewHolder();
        LayoutInflater parentInflater = (LayoutInflater) mctx.getSystemService(mctx.LAYOUT_INFLATER_SERVICE);
        view = parentInflater.inflate(R.layout.itemname_child_layout, null);
    } else {
        holder = (ViewHolder) view.getTag();
    }

   holder.button1 = view.findViewById(R.id.button1);
   holder.button1 = view.findViewById(R.id.button2);

   //initial button state
   holder.button1.setVisibility(View.VISIBLE);
   holder.button1.setVisibility(View.GONE);

   holder.recordBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
         holder.button1.setVisibility(View.GONE);
         holder.button2.setVisibility(View.VISIBLE);
      }
   }

   view.setTag(holder);

 return view;
}

2 个答案:

答案 0 :(得分:1)

您需要记录按钮的单击,因为滚动列表视图时,它会刷新到上一个阶段。

因此像这样在适配器类中普遍声明一个变量:

  ArrayList<Integer> count = new ArrayList(); 

现在将列表设置为0,直到适配器大小

    for(int i=0;i<adapter.size;i++){
     count.add(0);
     }

然后在单击button1之后,按如下所示设置按钮的可见性:

              holder.recordBtn.setTag(position);

     if(count.get(position)==0){
         holder.button1.setVisibility(View.GONE);
         holder.button2.setVisibility(View.VISIBLE);
        }
         else{
         holder.button1.setVisibility(View.VISIBLE);
         holder.button2.setVisibility(View.GONE);
           }

       holder.recordBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
         int pos = (int)view.getTag();
         if(count.get(pos)==0){
         count.set(pos,1);
         holder.button1.setVisibility(View.GONE);
         holder.button2.setVisibility(View.VISIBLE);
        }
         else{
         count.set(pos,0);
         holder.button1.setVisibility(View.VISIBLE);
         holder.button2.setVisibility(View.GONE);
           }
      }
   }

答案 1 :(得分:0)

尝试一下...我已经编辑了您的代码

 @Override
 public View getChildView(final int i, final int i1, boolean b, View 
 view, ViewGroup viewGroup) {

    if(view == null) {
        holder =  new ViewHolder();
        LayoutInflater parentInflater = (LayoutInflater) mctx.getSystemService(mctx.LAYOUT_INFLATER_SERVICE);
        view = parentInflater.inflate(R.layout.itemname_child_layout, null);
        holder.button1 = view.findViewById(R.id.button1);
        holder.button2 = view.findViewById(R.id.button2);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }


   //initial button state
   holder.button1.setVisibility(View.VISIBLE);
   holder.button2.setVisibility(View.GONE);

   holder.recordBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {

         holder.button1.setVisibility(View.GONE);
         holder.button2.setVisibility(View.VISIBLE);
      }
   }


 return view;
}