好的。所以我正在制作一个小的电子书商店应用程序。我在 strings.xml 中有一个包含书籍列表的字符串数组。每个数组项包含四段数据,包括其封面图像的路径、标题、作者和流派。在下面的代码中,我为数组中的每本书创建了一个 View 对象,并将它们打印到屏幕上。我目前使用的是 Tabs+Swipe 布局,共有三个选项卡:Title、Author 和 Genre。我想要做的是按标题、作者或流派对视图数组进行排序,具体取决于选择的选项卡。任何人都知道我可以做到这一点的任何方式吗?
public static class SectionFragment extends Fragment {
private static ScrollView outerLayout;
private static LinearLayout innerLayout;
private static LayoutParams params;
private static ArrayList <String []> bookData = new ArrayList <String []> ();
private static String [] bookList;
public SectionFragment () { }
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
params = new LayoutParams (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
outerLayout = new ScrollView (context);
outerLayout.setLayoutParams(params);
innerLayout = new LinearLayout (context);
innerLayout.setLayoutParams(params);
innerLayout.setOrientation(LinearLayout.VERTICAL);
bookList = res.getStringArray(R.array.bookList);
for (int i = 0; i < bookList.length; i++) {
View bookView = new View (context);
bookView = inflater.inflate(R.layout.grid_cell, null);
ImageView bookCover = (ImageView) bookView.findViewById(R.id.bookCover);
TextView bookTitle = (TextView) bookView.findViewById(R.id.bookTitle);
TextView bookAuthor = (TextView) bookView.findViewById(R.id.bookAuthor);
TextView bookGenre = (TextView) bookView.findViewById(R.id.bookGenre);
bookData.add(bookList[i].split("\\|"));
try {
bookCover.setImageDrawable(Drawable.createFromStream(
context.getAssets().open(bookData.get(i)[0]), null));
} catch (IOException e) {
e.printStackTrace();
}
bookTitle.setText(bookData.get(i)[1]);
bookAuthor.setText("by " + bookData.get(i)[2]);
bookGenre.setText(bookData.get(i)[3]);
innerLayout.addView(bookView);
}
outerLayout.addView(innerLayout);
return outerLayout;
}
}