我正在创建一个滚动窗格,它显示从 sqlite 数据库获取的数据集,这些数据的显示方式是它们排列在重复的 ui 集中,就像 android 中的 recyclerview 中一样。 有什么方法可以实现它,因为使用 javafx 定位元素并不容易
javafx 中有一些使用类似概念的类:
适配器被
cellFactory
取代,位置实际上没有等效项,但单元格确实包含 index
属性。 updateItem
类中有一个Cell
方法负责更新单元格视觉效果。
请使用一些教程来熟悉此类控件,因为这超出了答案的范围。
ListCell
的工作方式与 TableView
类似,但基本上只有一个列,并且 cellFactory
是 ListView
类本身的属性,并且没有 cellValueFactory
等效项。
TreeView
和 TreeTableView
的工作方式分别与 ListView
和 TableView
非常相似,主要区别在于支持分层数据结构。
我会实现自己的ListCell,然后在ListView 中使用它。 它会是这样的:
public class RecyclerLikeCell extends ListCell<MyCustomBean> {
@Override
protected void updateItem(MyCustomBean item, boolean empty){
super.updateItem(item, empty);
//Create whatever you need to render. And then add:
//I'm using a VBox as an example, you may use whatever you need.
var myComponent = new VBox(component1, component2, component3);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setGraphic(myComponent);
}
/**
* Utility method to use as ListCellFactory.
*
* @return a Callback to use in ListView.
*/
public static Callback<ListView<MyCustomBean>, ListCell<MyCustomBean>>
forListView(){
return i -> new RecyclerLikeCell();
}
}
然后,在控制器初始化中您可以使用:
listRecyclerView.setCellFactory(RecyclerLikeCell.forListView());
应该没问题。我用它创建了一个用户菜单,就像 SAP Business One GUI 上的菜单一样。
I made use of the list do do my scrollable recycler view
//RecyclerView
public class RecyclerActivity extends AppCompatActivity {
private List<Person> people;
private PersonAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler);
// Initialise data collection
people = Person.getPeople();
// Get reference to Recycler View
RecyclerView lstPeople = findViewById(R.id.lstPeople);
// Create an adapter to map single data element -> single item view
adapter = new PersonAdapter(people);
// How will the individual items be laid out in the collection view?
RecyclerView.LayoutManager layoutManager;
layoutManager = new LinearLayoutManager(getApplicationContext());
// Assign adapter to "list" viewer
lstPeople.setLayoutManager(layoutManager);
lstPeople.setAdapter(adapter);
// Set extra parameters if needed.
// This decorator adds extra spacing around all items in the recycle view.
lstPeople.addItemDecoration(new EqualSpaceItemDecoration(18));
// Attach item selected event handler
adapter.setOnClickListener( view -> {
// Get view holder of the view.
PersonAdapter.PersonViewHolder viewHolder =
(PersonAdapter.PersonViewHolder) lstPeople.findContainingViewHolder(view);
// Get the person from the view holder.
Person person = viewHolder.person;
// Do something with the person.
Toast.makeText(this, person.toString(),
Toast.LENGTH_LONG).show();
});
}
public void onAddClicked(View view) {
// Add a new person via the adapter.
adapter.add(new Person("Someone", 18, -1));
}
public void onDeleteClicked(View view) {
// Delete the last person (if there is one).
if(adapter.getItemCount() > 0)
adapter.remove(adapter.getItemCount() - 1);
}
public void onLinearManagerClicked(View view) {
// How will the individual items be laid out in the collection view?
RecyclerView.LayoutManager layoutManager;
layoutManager = new LinearLayoutManager(getApplicationContext());
// Assign new layout manager to "list" viewer
RecyclerView lstPeople = findViewById(R.id.lstPeople);
lstPeople.setLayoutManager(layoutManager);
}
public void onGridManagerClicked(View view) {
// How will the individual items be laid out in the collection view?
RecyclerView.LayoutManager layoutManager;
layoutManager = new GridLayoutManager(getApplicationContext(),2);
// Assign new layout manager to "list" viewer
RecyclerView lstPeople = findViewById(R.id.lstPeople);
lstPeople.setLayoutManager(layoutManager);
}
public void onStaggeredManagerClicked(View view) {
// How will the individual items be laid out in the collection view?
RecyclerView.LayoutManager layoutManager;
layoutManager = new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL);
// Assign new layout manager to "list" viewer
RecyclerView lstPeople = findViewById(R.id.lstPeople);
lstPeople.setLayoutManager(layoutManager);
}
}
// ListView
public class ListViewActivity extends AppCompatActivity {
private ArrayAdapter<Person> adapter;
private int selectedIndex = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
setUpListView();
setUpSpinnerView();
}
private void setUpListView() {
// Get data to be displayed.
List<Person> people = Person.getPeople();
// Get an adapter to display a person in the ListView.
// Going to use one with a predefined layout that uses the toString method
// of the data item.
adapter = new ArrayAdapter<Person>(this,
android.R.layout.simple_list_item_2, // a pre-define layout with text views
android.R.id.text2, // the id of the text view in which to display the toString value
people); // the person collection to be displayed
// Set the list view to use the adapter.
ListView lstPeople = findViewById(R.id.lstPeople);
lstPeople.setAdapter(adapter);
// Attach event handler to respond to clicking on an item.
lstPeople.setOnItemClickListener((adapterView, view, index, id) -> {
if(index >= 0){
// Get person clicked on.
Person person = adapter.getItem(index);
selectedIndex = index;
// Do something with this info.
Toast.makeText(this,
"Clicked on Person[" + index + "] = " + person.name,
Toast.LENGTH_LONG).show();
} else
Toast.makeText(this, "Clicked, but no-one home.", Toast.LENGTH_LONG).show();
});
}
private void setUpSpinnerView() {
// Set adapter for spinner to use. In this case, the same one as the list view,
// i.e. if the adapter is updated, BOTH views will be updated as well.
Spinner spnPeople = findViewById(R.id.spPeople);
spnPeople.setAdapter(adapter);
}
public void onAddClicked(View view) {
// Create a new person to add to the list.
Person person = new Person("Someone", 18, 0);
// Add to the list, BUT do it via the adapter so that it notifies the
// ListView, which can then refresh itself.
adapter.add(person);
Toast.makeText(this, "Added someone.", Toast.LENGTH_LONG).show();
}
public void onDeleteClicked(View view) {
// Delete the last person clicked on.
ListView lstPeople = findViewById(R.id.lstPeople);
if(selectedIndex >= 0) {
adapter.remove(adapter.getItem(selectedIndex));
Toast.makeText(this, "Deleted someone.", Toast.LENGTH_LONG).show();
} else
Toast.makeText(this, "No-one selected.", Toast.LENGTH_LONG).show();
selectedIndex = -1;
}
}