gpt4 book ai didi

java - Recyclerview 具有水平项目,因为它是一张 table ?

转载 作者:行者123 更新时间:2023-12-01 19:05:25 24 4
gpt4 key购买 nike

我的应用程序有一个数据库,其中存储了 12 列,其中包含用户添加的文本我可以使用数组列表检索数据并将其放入回收 View 中,但结果是一个逐行列表,一个元素一个接着一个我希望 12 个元素水平排列,然后将另一个 13 个元素作为新行,我尝试在数组列表中放入\n 但结果是一个水平空间,后跟其他 13 个元素为此,我使用了:

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

这是加载数组列表中的列并将其放入回收 View 的代码:

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();
}
}

这是适配器代码:

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);
}
}

您对以人类可读的方式显示数据有什么建议吗?预先感谢并为我的无能感到抱歉,但我仍在学习

最佳答案

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

或者,如果您想要一个适应屏幕大小的布局管理器,请使用 FlexboxLayoutManager

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

如果您想要在行或列中容纳太多内容以适应屏幕,请使用 GridLayoutManager 将 recyclerview View 包装在 Horizo​​ntalScrollView 中ScrollView 以不滚动的方向滚动 recyclerview

https://developer.android.com/reference/android/widget/HorizontalScrollView

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

所以听起来你想要一个垂直方向的 RecyclerView 和一个网格布局管理器,列设置为 12 全部包含在水平 ScrollView 中,这样你就可以滚动到屏幕右侧的列。

关于java - Recyclerview 具有水平项目,因为它是一张 table ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59566284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com