2017-01-13 58 views
0

我正在使用ScrollView來顯示列表。有一個XML我正在使用項目佈局。此佈局包含一個按鈕,我需要顯示或隱藏特定位置。現在我的問題是,當我滾動查看所有按鈕隱藏。我如何防止隱藏特定位置的按鈕。ScrollView中所有項目的滾動隱藏按鈕

我的適配器類是如波紋管

package in.nic.bhopal.prosecutordailydiary.adapter; 

import android.content.Context; 
import android.content.Intent; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.TextView; 

import java.util.List; 

import in.nic.bhopal.prosecutordailydiary.R; 
import in.nic.bhopal.prosecutordailydiary.activity.DairyOneActivity; 
import in.nic.bhopal.prosecutordailydiary.activity.EditFormOneActivity; 
import in.nic.bhopal.prosecutordailydiary.helper.RecyclerViewClickListener; 
import in.nic.bhopal.prosecutordailydiary.models.Courts; 

/** 
* @author Satish Rajput 
* on 10-01-2017. 
*/ 

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

    private Context mContext; 
    private List<Courts> courtList; 
    private RecyclerViewClickListener itemListener; 

    public class MyViewHolder extends RecyclerView.ViewHolder 
      implements View.OnClickListener { 
     public TextView tvName, tvNoOfCases, tvPW, tvDW; 
     public Button btnAdd, btnEdit; 
     public MyViewHolder(View view) { 
      super(view); 
      view.setOnClickListener(this); 
      tvName = (TextView) view.findViewById(R.id.tvCourtName); 
      tvNoOfCases = (TextView) view.findViewById(R.id.tvNoOfCases); 
      tvPW = (TextView) view.findViewById(R.id.tvPW); 
      tvDW = (TextView) view.findViewById(R.id.tvDW); 

      btnAdd = (Button)view.findViewById(R.id.btnAdd); 
      btnAdd.setOnClickListener(this); 
      btnEdit = (Button)view.findViewById(R.id.btnEdit); 
      btnEdit.setOnClickListener(this); 
     } 

     @Override 
     public void onClick(View v) { 
//   ((InstitutesActivity)mContext).showAlert(mContext,"Alert","Clicked item"); 
//   mContext.startActivity(new Intent(mContext, InspectionActivity.class)); 
      itemListener.recyclerViewListClicked(v, this.getPosition()); 

      if(v.getId()==R.id.btnAdd){ 
       ((DairyOneActivity)mContext).flipper.setDisplayedChild(1); 
       ((DairyOneActivity)mContext).courts = courtList.get((int)v.getTag()); 

      }else if(v.getId()==R.id.btnEdit){ 
       Intent i = new Intent(mContext, EditFormOneActivity.class); 
       String ci = (courtList.get((int)v.getTag())).courtTypeId; 
       i.putExtra("CourtId", (courtList.get((int)v.getTag())).courtTypeId); 
       mContext.startActivity(i); 
      } 
     } 
    } 

    public CourtAdapter(Context mContext, List<Courts> courtList) { 
     this.mContext = mContext; 
     this.courtList = courtList; 
     this.itemListener = (RecyclerViewClickListener)mContext; 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.list_item_courts, parent, false); 
     return new MyViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(final MyViewHolder holder, int position) { 
     Courts courts = courtList.get(position); 
     holder.tvName.setText("Court Name - "+courts.courtName); 
     holder.tvNoOfCases.setText("No. Of Cases - "+courts.noOfCases); 
     holder.tvPW.setText("Total Prosecution witness - "+courts.pw); 
     holder.tvDW.setText("Total Defence witness - "+courts.dw); 

     holder.btnAdd.setTag(position); 
     holder.btnEdit.setTag(position); 
     if(Integer.parseInt(courts.noOfCases)<=0) 
      holder.btnEdit.setVisibility(View.GONE); 
    } 

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

} 

在onBindView方法條件隱藏所有按鈕。請幫忙。

回答

3

更新onBindViewHolder方法本 -

if(Integer.parseInt(courts.noOfCases)<=0){ 
      holder.btnEdit.setVisibility(View.GONE); 
     } 
     else { 
      holder.btnEdit.setVisibility(View.VISIBLE); 
     } 
1

在你onBindViewHolder,您可以訪問使用viewHolder.getAdapterPosition位置()。對於你來說,它看起來像 -

@Override 
public void onBindViewHolder(final MyViewHolder holder, int position) { 
    Courts courts = courtList.get(position); 
    holder.tvName.setText("Court Name - " + courts.courtName); 
    holder.tvNoOfCases.setText("No. Of Cases - " + courts.noOfCases); 
    holder.tvPW.setText("Total Prosecution witness - " + courts.pw); 
    holder.tvDW.setText("Total Defence witness - " + courts.dw); 

    holder.btnAdd.setTag(position); 
    holder.btnEdit.setTag(position); 
    if (Integer.parseInt(courts.noOfCases) <= 0 && holder.getAdapterPostion == <position_you_want_to_hide_btn_at>) 
     holder.btnEdit.setVisibility(View.VISIBLE); 
    else 
     holder.btnEdit.setVisibility(View.GONE); 
} 
+0

感謝您的回覆,我從你的代碼行得到暗示,並找到解決辦法如下如果(的Integer.parseInt(courts.noOfCases)> 0 && holder.getAdapterPosition( )==位置) holder.btnEdit.setVisibility(View.VISIBLE); else holder.btnEdit.setVisibility(View.GONE); –

+0

樂意提供幫助。如果它有幫助,請將答案標記爲已接受,並提供支持。我將編輯答案以反映正確的解決方案。 :) – Nairiya