标题翻译
Gridview item set based on matrix position [row][column]
问题
I need to design the bus seat view in an Android application, and I have seat positions in a matrix format. For example:
a1.row=4;
a1.column=5;
I only need to set those seats in a GridView based on their matrix position. Other spaces will be blank or empty in the matrix. After showing these seat views, if I choose a specific seat, then the seat-related object will be added to a list of objects. If I deselect it, it will be removed from the list of objects. Can anyone help me find a solution for this? Is it possible to implement this using a GridView, where I will set the item in the GridView using its matrix position?
英文翻译
I need to design the bus seat view in android application and I have seat positions in matrix format. For example
a1.row=4;
a1.column=5;
I only need to set those seats in gridview based on their matrix position. Other spaces will be blank or empty in matrix. After showing these seat view if I choose specific seat then that seat related object will be added in a list of object and if I deselect it will be removed from the list of object. Can anyone help me to find out a solution for this ? Is it possible to implement this using gridview but I will set the item in grid view using its matrix position
答案1
得分: 0
尝试以下代码:
活动类
public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.rv_list);
        showSeatList();
    }
    private void showSeatList() {
        List<SeatModel> seatModels = new ArrayList<>();
        seatModels.add(new SeatModel("1", "A", false));
        seatModels.add(new SeatModel("2", "B", false));
        seatModels.add(new SeatModel("3", "C", false));
        seatModels.add(new SeatModel("4", "D", false));
        seatModels.add(new SeatModel("5", "E", false));
        seatModels.add(new SeatModel("6", "F", false));
        seatModels.add(new SeatModel("7", "G", false));
        seatModels.add(new SeatModel("8", "H", false));
        seatModels.add(new SeatModel("9", "I", false));
        seatModels.add(new SeatModel("10", "J", false));
        SeatSelectionAdapter adapter = new SeatSelectionAdapter(this, seatModels);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 5));
        recyclerView.setAdapter(adapter);
    }
}
适配器类
public class SeatSelectionAdapter extends RecyclerView.Adapter<SeatSelectionAdapter.ViewHolder> {
    private List<SeatModel> seatModelList;
    private LayoutInflater inflater;
    private Context context;
    public SeatSelectionAdapter(Context context, List<SeatModel> models) {
        this.seatModelList = models;
        inflater = LayoutInflater.from(context);
        this.context = context;
    }
    @NonNull
    @Override
    public SeatSelectionAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.model_seat, parent, false);
        return new SeatSelectionAdapter.ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull SeatSelectionAdapter.ViewHolder holder, int position) {
        SeatModel model = seatModelList.get(position);
        holder.setData(model);
    }
    @Override
    public int getItemCount() {
        return seatModelList.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder {
        private CardView cardView;
        private LinearLayout lnLayout;
        private TextView tvSeat;
        private ViewHolder(View view) {
            super(view);
            tvSeat = view.findViewById(R.id.tv_seat);
            cardView = view.findViewById(R.id.cv_card);
            lnLayout = view.findViewById(R.id.ln_layout);
        }
        @SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
        private void setData(final SeatModel model) {
            tvSeat.setText(model.getSeat());
            if (model.isSelect())
                lnLayout.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
            else lnLayout.setBackgroundColor(context.getResources().getColor(R.color.white));
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (model.isSelect()) {
                        model.setSelect(false);
                        Toast.makeText(context, "Seat not selected", Toast.LENGTH_LONG).show();
                    } else {
                        model.setSelect(true);
                        Toast.makeText(context, "Seat selected", Toast.LENGTH_LONG).show();
                    }
                    notifyDataSetChanged();
                }
            });
        }
    }
}
模型类
public class SeatModel {
    private String id, seat;
    private boolean isSelect;
    public SeatModel(String id, String seat, boolean isSelect) {
        this.id = id;
        this.seat = seat;
        this.isSelect = isSelect;
    }
    public boolean isSelect() {
        return isSelect;
    }
    public void setSelect(boolean select) {
        isSelect = select;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getSeat() {
        return seat;
    }
    public void setSeat(String seat) {
        this.seat = seat;
    }
}
英文翻译
Try this code
Activityclass
public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.rv_list);
        showSeatList();
    }
    private void showSeatList()
    {
        List<SeatModel> seatModels = new ArrayList<>();
        seatModels.add(new SeatModel("1","A",false));
        seatModels.add(new SeatModel("2","B",false));
        seatModels.add(new SeatModel("3","C",false));
        seatModels.add(new SeatModel("4","D",false));
        seatModels.add(new SeatModel("5","E",false));
        seatModels.add(new SeatModel("6","F",false));
        seatModels.add(new SeatModel("7","G",false));
        seatModels.add(new SeatModel("8","H",false));
        seatModels.add(new SeatModel("9","I",false));
        seatModels.add(new SeatModel("10","J",false));
        SeatSelectionAdapter adapter = new SeatSelectionAdapter(this,seatModels);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 5));
        recyclerView.setAdapter(adapter);
    }
}
Adapter Class
public class SeatSelectionAdapter  extends RecyclerView.Adapter<SeatSelectionAdapter.ViewHolder>
{
    private List<SeatModel> seatModelList;
    private LayoutInflater inflater;
    private Context context;
    public SeatSelectionAdapter(Context context, List<SeatModel>models)
    {
        this.seatModelList = models;
        inflater = LayoutInflater.from(context);
        this.context = context;
    }
    @NonNull
    @Override
    public SeatSelectionAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.model_seat,parent,false);
        return new SeatSelectionAdapter.ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull SeatSelectionAdapter.ViewHolder holder, int position) {
        SeatModel model = seatModelList.get(position);
        holder.setData(model);
    }
    @Override
    public int getItemCount() {
        return seatModelList.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder
    {
        private CardView cardView;
        private LinearLayout lnLayout;
        private TextView tvSeat;
        private ViewHolder(View view) {
            super(view);
            tvSeat = view.findViewById(R.id.tv_seat);
            cardView = view.findViewById(R.id.cv_card);
            lnLayout = view.findViewById(R.id.ln_layout);
        }
        @SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
        private void setData(final SeatModel model)
        {
            tvSeat.setText(model.getSeat());
            if(model.isSelect())
                lnLayout.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
            else lnLayout.setBackgroundColor(context.getResources().getColor(R.color.white));
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(model.isSelect())
                    {
                        model.setSelect(false);
                        Toast.makeText(context,"Seat not selected",Toast.LENGTH_LONG).show();
                    }
                    else{
                        model.setSelect(true);
                        Toast.makeText(context,"Seat selected",Toast.LENGTH_LONG).show();
                    }
                    notifyDataSetChanged();
                }
            });
        }
    }
}
Model Class
public class SeatModel
{
    private String id,seat;
    private boolean isSelect;
    public SeatModel(String id, String seat, boolean isSelect) {
        this.id = id;
        this.seat = seat;
        this.isSelect = isSelect;
    }
    public boolean isSelect() {
        return isSelect;
    }
    public void setSelect(boolean select) {
        isSelect = select;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getSeat() {
        return seat;
    }
    public void setSeat(String seat) {
        this.seat = seat;
    }
}
专注分享java语言的经验与见解,让所有开发者获益!

评论