简体   繁体   English

带有水平项目的 Recyclerview,因为它是一张桌子?

[英]Recyclerview with horizontal items as it was a table?

My app has a database where are stored 12 columns that cointains text added by the user I am able to retrive and put data in the recycle view using an array list but the result is a line-by-line list an element after another I want the 12 element allineated horizontally and then another 13 element as a new line,I tried putting a \\n in the array list but the result is an horizontal space followed by other 13 elements For this I used :我的应用程序有一个数据库,其中存储了 12 列,这些列包含用户添加的文本水平排列的 12 个元素,然后是另一个 13 个元素作为新行,我尝试在数组列表中放置一个 \\n,但结果是一个水平空间,后跟其他 13 个元素为此我使用了:

LinearLayoutManager layoutManager= new LinearLayoutManager(this,RecyclerView.HORIZONTAL,false);
        recyclerView.setLayoutManager(layoutManager);

This is the code that loads the the columns in the array list and puts the in the recycleview:这是加载数组列表中的列并将其放入回收视图的代码:

public class VisualizzaPunto extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener {

    private PuntoStradaDatabase helper;
    private MyRecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_visualizza_punto);
        helper = new PuntoStradaDatabase(this);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycle);
        LinearLayoutManager layoutManager= new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        SQLiteDatabase db = helper.getReadableDatabase();
        ArrayList<String> theList = new ArrayList<>();
        Cursor cursor = db.rawQuery("SELECT * FROM PUNTOSTRADA", null);
        while(cursor.moveToNext()){
            theList.add(cursor.getString(1));
            theList.add(cursor.getString(2));
            theList.add(cursor.getString(3));
            theList.add(cursor.getString(4));
            theList.add(cursor.getString(5));
            theList.add(cursor.getString(6));
            theList.add(cursor.getString(7));
            theList.add(cursor.getString(8));
            theList.add(cursor.getString(9));
            theList.add(cursor.getString(10));
            theList.add(cursor.getString(11));
            theList.add(cursor.getString(12));
            theList.add("\n");
        }
       /* DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
                layoutManager.getOrientation());
        recyclerView.addItemDecoration(dividerItemDecoration);
      */  adapter = new MyRecyclerViewAdapter(this, theList);
        adapter.setClickListener(this);
        recyclerView.setAdapter(adapter);



    }

    public void onItemClick(View view, int position) {
        Toast.makeText(this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();
    }
}

This is the adapter code:这是适配器代码:

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
    private List<String> mData;
    private LayoutInflater mInflater;
    private ItemClickListener mClickListener;

    // data is passed into the constructor
    MyRecyclerViewAdapter(Context context, List<String> data) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
    }

    // inflates the row layout from xml when needed
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.recyclerview_row1, parent, false);
        return new ViewHolder(view);
    }

    // binds the data to the TextView in each row
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String item = mData.get(position);
        holder.myTextView.setText(item);
    }

    // total number of rows
    @Override
    public int getItemCount() {
        return mData.size();
    }


    // stores and recycles views as they are scrolled off screen
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView myTextView;

        ViewHolder(View itemView) {
            super(itemView);
            myTextView = itemView.findViewById(R.id.row1);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
        }
    }

    // convenience method for getting data at click position
    String getItem(int id) {
        return mData.get(id);
    }

    // allows clicks events to be caught
    void setClickListener(ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    // parent activity will implement this method to respond to click events
    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }
}

do you have any suggestions to show data in a human-readable way?您对以人类可读的方式显示数据有什么建议吗? Thanks in advance and sorry for my incompetence but I am still learning预先感谢并为我的无能感到抱歉,但我仍在学习

Use GridLayoutManager https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.html instead of a LinearLayoutManager使用 GridLayoutManager https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.html而不是 LinearLayoutManager

Or if you want an layout Manger that adapts to the size of the screen use a FlexboxLayoutManager或者,如果您想要一个适应屏幕大小的布局管理器,请使用 FlexboxLayoutManager

https://github.com/google/flexbox-layout https://github.com/google/flexbox-layout

If there is too much you want to fit in a row or column to fit on the screen then wrap the recyclerview view with GridLayoutManager in a HorizontalScrollView or ScrollView to scroll the recyclerview in the direction it does not scroll如果您想要在一行或一列中容纳太多内容以适合屏幕,则将 recyclerview 视图与GridLayoutManager包装在HorizontalScrollViewScrollView中,以沿不滚动的方向滚动 recyclerview

https://developer.android.com/reference/android/widget/HorizontalScrollView https://developer.android.com/reference/android/widget/Horizo​​ntalScrollView

and

https://developer.android.com/reference/android/widget/ScrollView.html https://developer.android.com/reference/android/widget/ScrollView.html

So it sounds like you want a RecyclerView in Vertical Orientation with a Grid layout manager with the columns set to 12 all wrapped up in a Horizontal Scroll View so you can scroll to the column off the screen to the right.所以听起来你想要一个垂直方向的 RecyclerView 和一个网格布局管理器,列设置为 12,所有列都包含在一个水平滚动视图中,这样你就可以滚动到屏幕右侧的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM