iT邦幫忙

0

【Android 】RecyclerView 如何變更當前item的可見度(JAVA)

我想要透過按鈕控制當前item內容物件的可見度,我總共有五個按鈕
當點選按鈕1時,按鈕1會的layout不可見,2~5的layout變成可見
當點選按鈕2時,按鈕2會不可使用,3變可使用
當點選按鈕3時,按鈕3會不可使用,4變可使用,以此類推
但是實作後我只能操控item最後一項的button,我點其他button時,只有最夠一項item的button事件會觸發
我該如何綁定當前item,比如說我使用第一項item時候,點選第一項item中的button,只有第一項中的button會動作

解釋的有點混亂,請大大們見諒,有附上問題影片

程式碼:

public class RecyclerviewDriverStartAdapter extends RecyclerView.Adapter<RecyclerviewDriverStartAdapter.DSviewHolder>{
    Context context;
    ArrayList<DriverStartData> listData;
    public Button startBtn,driverArrivalBtn,boardBtn,addressArrivalBtn,getOffBtn;
    public LinearLayout startLayout;
    public ConstraintLayout buttonLayout;
    public TextView id,carNumber,people,date,time,goAddress,endAddress,remark,pay,money,formula,mission;
    public RecyclerviewDriverStartAdapter(Context context,ArrayList<DriverStartData> listData){
        this.context=context;
        this.listData=listData;
    }
    //綁定recyclerview的layout模組
    @NonNull
    @Override
    public DSviewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.recyclerview_drive_starting,parent,false);
        return new DSviewHolder(view);
    }
    //設置內容
    @Override
    public void onBindViewHolder(@NonNull DSviewHolder holder, int position) {
        DriverStartData data = listData.get(position);
        setView(data);
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkNowAddress();
                startLayout.setVisibility(View.GONE);
                buttonLayout.setVisibility(View.VISIBLE);
                notifyDataSetChanged();
            }
        });
        driverArrivalBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkNowAddress();
                driverArrivalBtn.setEnabled(false);
                driverArrivalBtn.setTextColor(Color.parseColor("#808080"));
                boardBtn.setEnabled(true);
                boardBtn.setTextColor(Color.parseColor("#008080"));
                addressArrivalBtn.setEnabled(false);
                addressArrivalBtn.setTextColor(Color.parseColor("#808080"));
                getOffBtn.setEnabled(false);
                getOffBtn.setTextColor(Color.parseColor("#808080"));
                notifyDataSetChanged();
            }
        });
        boardBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkNowAddress();
                driverArrivalBtn.setEnabled(false);
                driverArrivalBtn.setTextColor(Color.parseColor("#808080"));
                boardBtn.setEnabled(false);
                boardBtn.setTextColor(Color.parseColor("#808080"));
                addressArrivalBtn.setEnabled(true);
                addressArrivalBtn.setTextColor(Color.parseColor("#FF1493"));
                getOffBtn.setEnabled(false);
                getOffBtn.setTextColor(Color.parseColor("#808080"));
                notifyDataSetChanged();
            }
        });
        addressArrivalBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkNowAddress();
                driverArrivalBtn.setEnabled(false);
                driverArrivalBtn.setTextColor(Color.parseColor("#808080"));
                boardBtn.setEnabled(false);
                boardBtn.setTextColor(Color.parseColor("#808080"));
                addressArrivalBtn.setEnabled(false);
                addressArrivalBtn.setTextColor(Color.parseColor("#808080"));
                getOffBtn.setEnabled(true);
                getOffBtn.setTextColor(Color.parseColor("#0000FF"));
                notifyDataSetChanged();
            }
        });
        getOffBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkNowAddress();
                driverArrivalBtn.setEnabled(true);
                driverArrivalBtn.setTextColor(Color.parseColor("#B8860B"));
                boardBtn.setEnabled(false);
                boardBtn.setTextColor(Color.parseColor("#808080"));
                addressArrivalBtn.setEnabled(false);
                addressArrivalBtn.setTextColor(Color.parseColor("#808080"));
                getOffBtn.setEnabled(false);
                getOffBtn.setTextColor(Color.parseColor("#808080"));
                startLayout.setVisibility(View.VISIBLE);
                buttonLayout.setVisibility(View.GONE);
                startBtn.setVisibility(View.GONE);
                mission.setVisibility(View.VISIBLE);
                notifyDataSetChanged();
            }
        });
    }
    //回傳長度
    @Override
    public int getItemCount() {
        return listData.size();
    }
    //元件放入位置
    public class DSviewHolder extends RecyclerView.ViewHolder{
        public DSviewHolder(@NonNull View itemView) {
            super(itemView);
            startLayout=(LinearLayout)itemView.findViewById(R.id.driver_start_begin_layout);
            buttonLayout=(ConstraintLayout)itemView.findViewById(R.id.driver_btn_layout);
            startBtn=(Button)itemView.findViewById(R.id.driver_start_begin_btn);
            startBtn.setTypeface(FontManager.getTypeface(context, FontManager.FONTAWESOME));
            driverArrivalBtn=(Button)itemView.findViewById(R.id.driver_start_driver_arrive_btn);
            driverArrivalBtn.setTypeface(FontManager.getTypeface(context, FontManager.FONTAWESOME));

            boardBtn=(Button)itemView.findViewById(R.id.driver_start_board_btn);
            boardBtn.setTypeface(FontManager.getTypeface(context, FontManager.FONTAWESOME));
            boardBtn.setEnabled(false);
            boardBtn.setTextColor(Color.parseColor("#808080"));
            addressArrivalBtn=(Button)itemView.findViewById(R.id.driver_start_address_arrive_btn);
            addressArrivalBtn.setTypeface(FontManager.getTypeface(context, FontManager.FONTAWESOME));
            addressArrivalBtn.setEnabled(false);
            addressArrivalBtn.setTextColor(Color.parseColor("#808080"));
            getOffBtn=(Button)itemView.findViewById(R.id.driver_start_get_off_btn);
            getOffBtn.setTypeface(FontManager.getTypeface(context, FontManager.FONTAWESOME));
            getOffBtn.setEnabled(false);
            getOffBtn.setTextColor(Color.parseColor("#808080"));

            id=(TextView) itemView.findViewById(R.id.recycler_driver_start_id);
            carNumber=(TextView) itemView.findViewById(R.id.recycler_driver_start_car_number);
            people=(TextView) itemView.findViewById(R.id.recycler_driver_start_people);
            date=(TextView) itemView.findViewById(R.id.recycler_driver_start_date);
            time=(TextView) itemView.findViewById(R.id.recycler_driver_start_time);
            goAddress=(TextView) itemView.findViewById(R.id.recycler_driver_start_go_address);
            endAddress=(TextView) itemView.findViewById(R.id.recycler_driver_start_end_address);
            remark=(TextView) itemView.findViewById(R.id.recycler_driver_start_remark);
            pay=(TextView) itemView.findViewById(R.id.recycler_driver_start_pay);
            money=(TextView) itemView.findViewById(R.id.recycler_driver_start_money);
            formula=(TextView) itemView.findViewById(R.id.recycler_driver_start_formula);
            mission=(TextView) itemView.findViewById(R.id.recycler_driver_start_mission_completed);
        }
    }
    public void setView(DriverStartData data){
        id.setText(data.getId());
        carNumber.setText(data.getCarNumber());
        people.setText(data.getPeople());
        String day=data.getReserve_date();
        String[] setDay=day.split("T");
        date.setText(setDay[0].substring(5,setDay[0].length()));
        time.setText(setDay[1].substring(0,setDay[1].length()-3));
        goAddress.setText(data.getGoAddress());
        endAddress.setText(data.getEndAddress());
        remark.setText(data.getRemark());
    }
不太清楚你的需求到底是需要做什麼 影片內容看起來也符合你上述所提供的內容
setEnabled(false) -> 不可點擊,不可使用
setVisibility()-> INVISIBLE僅不可見仍佔位 GONE消失
那如果你是希望對item進行操作
在onBindViewHolder內呼叫holder.itemView試試看
不知道有沒有解答到 或是你可以在進行補充說明 看code也不確定你需要什麼

尚未有邦友回答

立即登入回答