Can I display ListView horizontally on Android?

Asked 2 years ago, Updated 2 years ago, 119 views

Is it possible to display the list view horizontally? I did it using Gallery view, but when I use it, it automatically comes to the center of the screen when I select each item. How do we solve this problem? I think we can print out the ListView horizontally, is it possible?

android listview gallery horizontal-scrolling

2022-09-22 14:14

1 Answers

With the latest version of Android, RecyclerView has improved the problem of the existing list view. RecyclerView allows you to display horizontally.

Advantages of Recycler View:

This is the most interesting part of RecyclerView. When creating RecyclerView, it must be created, and through this, the layout of the views of all items is managed. In addition to horizontal/vertical arrangement, various layouts in the form of a grid may be arranged.

Layout Manager provided by default

In addition, developers can expand Layout Manager to create various forms. Use as follows.

LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.scrollToPosition(currPos);
recyclerView.setLayoutManager(layoutManager);

ViewHolder is a pattern that has been widely used in existing ListViews and has long been recommended by the Google Android team. However, although there was no compulsory restriction on using it, RecyclerView changed its structure to use the Adapter and Viewholder together. Developers who do not use ViewHolder patterns will need some training.

public final static class ListItemViewHolder extends RecyclerView.ViewHolder {
   TextView label;
   TextView dateTime;

   public ListItemViewHolder(View itemView) {
      super(itemView);
      label = (TextView) itemView.findViewById(R.id.txt_label_item);
      dateTime = (TextView) itemView.findViewById(R.id.txt_date_time);
   }
}

To use it, expand ViewHolder and save the subclass through findViewById(). In this way, the subclass (view) can be quickly accessed again through the class that has been created once.

In ListView, the same structure as Adapter is used to process data and views of the item. Three interfaces should be implemented:

public int getItemCount()

private List items;

RecyclerViewDemoAdapter(List modelData) { if (modelData == null) { throw new IllegalArgumentException( "modelData must not be null"); } this.items = modelData; }

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

@Override public void onBindViewHolder( ListItemViewHolder viewHolder, int position) { DemoModel model = items.get(position); viewHolder.label.setText(model.label); String dateStr = DateUtils.formatDateTime( viewHolder.label.getContext(), model.dateTime.getTime(), DateUtils.FORMAT_ABBREV_ALL); viewHolder.dateTime.setText(dateStr); }

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

public final static class ListItemViewHolder extends RecyclerView.ViewHolder { // // ViewHolder } }

It is the same structure as the existing ListView, so it can be easily implemented, and there is no extended version of the adapter's basic class (CursorAdapter, ArrayAdapter).


2022-09-22 14:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.