基于矩阵位置设置的GridView项目 [行][列]

huangapple 未分类评论48阅读模式
标题翻译

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?

英文翻译

基于矩阵位置设置的GridView项目 [行][列]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&lt;SeatModel&gt; seatModels = new ArrayList&lt;&gt;();

        seatModels.add(new SeatModel(&quot;1&quot;,&quot;A&quot;,false));
        seatModels.add(new SeatModel(&quot;2&quot;,&quot;B&quot;,false));
        seatModels.add(new SeatModel(&quot;3&quot;,&quot;C&quot;,false));
        seatModels.add(new SeatModel(&quot;4&quot;,&quot;D&quot;,false));
        seatModels.add(new SeatModel(&quot;5&quot;,&quot;E&quot;,false));
        seatModels.add(new SeatModel(&quot;6&quot;,&quot;F&quot;,false));
        seatModels.add(new SeatModel(&quot;7&quot;,&quot;G&quot;,false));
        seatModels.add(new SeatModel(&quot;8&quot;,&quot;H&quot;,false));
        seatModels.add(new SeatModel(&quot;9&quot;,&quot;I&quot;,false));
        seatModels.add(new SeatModel(&quot;10&quot;,&quot;J&quot;,false));

        SeatSelectionAdapter adapter = new SeatSelectionAdapter(this,seatModels);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 5));
        recyclerView.setAdapter(adapter);

    }

}

Adapter Class

public class SeatSelectionAdapter  extends RecyclerView.Adapter&lt;SeatSelectionAdapter.ViewHolder&gt;
{
    private List&lt;SeatModel&gt; seatModelList;
    private LayoutInflater inflater;
    private Context context;

    public SeatSelectionAdapter(Context context, List&lt;SeatModel&gt;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({&quot;SetTextI18n&quot;, &quot;ClickableViewAccessibility&quot;})
        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,&quot;Seat not selected&quot;,Toast.LENGTH_LONG).show();
                    }
                    else{
                        model.setSelect(true);
                        Toast.makeText(context,&quot;Seat selected&quot;,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;
    }
}

huangapple
  • 本文由 发表于 2020年3月16日 13:52:59
  • 转载请务必保留本文链接:https://java.coder-hub.com/60700946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定