我试图在每个RecyclerView中使多个RecyclerView除以TextView,此代码对我有用,但我想使代码更简单。有人可以帮我吗?
这是我的Activity.java:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
((MainActivity) getActivity()).setActionBarTitle("Tuntunan Toharoh");
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
//=============================================================
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
RecyclerView.Adapter tadapter = new RecyclerViewAdapter(getActivity(), mRecyclerViewItems);
mRecyclerView.setAdapter(tadapter);
addMenuItemsFromJson();
mRecyclerView.setNestedScrollingEnabled(false);
//=========================================================
DzRecyclerView2 = (RecyclerView) view.findViewById(R.id.recycler_submenu);
DzRecyclerView2.setHasFixedSize(true);
RecyclerView.LayoutManager zlayoutManager = new LinearLayoutManager(getActivity());
DzRecyclerView2.setLayoutManager(zlayoutManager);
RecyclerView.Adapter zadapter = new RecyclerViewAdapter(getActivity(), mRecyclerViewItems2);
DzRecyclerView2.setAdapter(zadapter);
addMenuItemsFromJson2();
DzRecyclerView2.setNestedScrollingEnabled(false);
//=========================================================
DzaRecyclerView3 = (RecyclerView) view.findViewById(R.id.recycler_mandi);
DzaRecyclerView3.setHasFixedSize(true);
RecyclerView.LayoutManager zzlayoutManager = new LinearLayoutManager(getActivity());
DzaRecyclerView3.setLayoutManager(zzlayoutManager);
RecyclerView.Adapter ziadapter = new RecyclerViewAdapter(getActivity(), mRecyclerViewItems3);
DzaRecyclerView3.setAdapter(ziadapter);
addMenuItemsFromJson3();
DzaRecyclerView3.setNestedScrollingEnabled(false);
//=========================================================
return view;
private void addMenuItemsFromJson() {
try {
String jsonDataString = readJsonDataFromFile();
JSONArray menuItemsJsonArray = new JSONArray(jsonDataString);
for (int i = 0; i < menuItemsJsonArray.length(); ++i) {
JSONObject menuItemObject = menuItemsJsonArray.getJSONObject(i);
String menuItemtitle = menuItemObject.getString("title");
String menuItemContent = menuItemObject.getString("content");
DafAdapter dafa = new DafAdapter(menuItemtitle, menuItemContent);
mRecyclerViewItems.add(dafa);
}
} catch (IOException | JSONException exception) {
Log.e(MainActivity.class.getName(), "Unable to parse JSON file.", exception);
}
}
private String readJsonDataFromFile() throws IOException {
InputStream inputStream = null;
StringBuilder builder = new StringBuilder();
try {
String jsonDataString = null;
inputStream = getResources().openRawResource(R.raw.tayamum);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream, "UTF-8"));
while ((jsonDataString = bufferedReader.readLine()) != null) {
builder.append(jsonDataString);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
}
return new String(builder);
}
private void addMenuItemsFromJson2() {
try {
String jsonDataString = readJsonDataFromFile2();
JSONArray menuItemsJsonArray = new JSONArray(jsonDataString);
for (int i = 0; i < menuItemsJsonArray.length(); ++i) {
JSONObject menuItemObject = menuItemsJsonArray.getJSONObject(i);
String menuItemtitle = menuItemObject.getString("title");
String menuItemContent = menuItemObject.getString("content");
DafAdapter dafa = new DafAdapter(menuItemtitle, menuItemContent);
mRecyclerViewItems2.add(dafa);
}
} catch (IOException | JSONException exception) {
Log.e(MainActivity.class.getName(), "Unable to parse JSON file.", exception);
}
}
private String readJsonDataFromFile2() throws IOException {
InputStream inputStream = null;
StringBuilder builder = new StringBuilder();
try {
String jsonDataString = null;
inputStream = getResources().openRawResource(R.raw.wudhu);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream, "UTF-8"));
while ((jsonDataString = bufferedReader.readLine()) != null) {
builder.append(jsonDataString);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
}
return new String(builder);
}
private void addMenuItemsFromJson3() {
try {
String jsonDataString = readJsonDataFromFile3();
JSONArray menuItemsJsonArray = new JSONArray(jsonDataString);
for (int i = 0; i < menuItemsJsonArray.length(); ++i) {
JSONObject menuItemObject = menuItemsJsonArray.getJSONObject(i);
String menuItemtitle = menuItemObject.getString("title");
String menuItemContent = menuItemObject.getString("content");
DafAdapter dafa = new DafAdapter(menuItemtitle, menuItemContent);
mRecyclerViewItems3.add(dafa);
}
} catch (IOException | JSONException exception) {
Log.e(MainActivity.class.getName(), "Unable to parse JSON file.", exception);
}
}
private String readJsonDataFromFile3() throws IOException {
InputStream inputStream = null;
StringBuilder builder = new StringBuilder();
try {
String jsonDataString = null;
inputStream = getResources().openRawResource(R.raw.mandi);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream, "UTF-8"));
while ((jsonDataString = bufferedReader.readLine()) != null) {
builder.append(jsonDataString);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
}
return new String(builder);
}
如果我想添加一个或多个RecyclerView,它只是做出一个类似的表达式,只是将表达式1更改为其他表达式。我认为它比该代码更简单。
我尝试了一个开关和一个if块,但是我没有任何运气。我该怎么办?
答案 0 :(得分:-1)
为什么不将RecyclerViews和TextViews列表放在垂直的Recyclerview中
因此您可以动态绑定它们
编辑
为了创建垂直的回收站视图,您应该首先查看是否有可能将所有行数据包装在单个List
中?
因此,如果可能的话,剩下的就是创建一个垂直的LayoutManager
来显示您的行。类似于:new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
并将其分配到新的RecyclerView
,仅剩下的事情就是创建ViewHolders
并将其与数据绑定。这很简单。
您可以查看sample project以便更好地了解