我想知道我们是否可以使用JSON开发标签。我希望尽可能多的标签取决于JSON中的类别,如果有可能,每个标签必须包含列表视图请告诉我如何操作
MainActivity.class
private void catmethod(){
String url="http://www.souqalkhaleejia.com/webapis/menus.php";
JsonObjectRequest catreq=new JsonObjectRequest(Request.Method.GET, url, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray menuarray=response.getJSONArray("menu");
for (int i=0;i<menuarray.length();i++){
JSONObject menuobj=menuarray.getJSONObject(i);
tabs.addTab(tabs.newTab().setText(menuobj.getString("title")));
Toast.makeText(MainActivity.this, menuobj.getString("title"), Toast.LENGTH_SHORT).show();
}
TabsAdapter tabsAdapter=new TabsAdapter(getSupportFragmentManager(),menuarray.length());
viewpager.setAdapter(tabsAdapter);
tabs.setupWithViewPager(viewpager);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("CatMenu", String.valueOf(error));
}
});
AppController.getInstance().addToRequestQueue(catreq);
}
答案 0 :(得分:0)
// JSON =&gt; [{&#34; key&#34;:&#34; TAB1&#34;,&#34; list&#34;:[your list]},{&#34; key&#34;:&#34; TAB2& #34;,&#34;列表&#34;:[您的列表]},{&#34;键&#34;:&#34; TAB3&#34;,&#34;列表&#34;:[您的列表]}]
使用JSON,您可以动态添加标签,如...
tabLayout = (TabLayout) findViewById(R.id.tabs);
// get your json list
JSONArray jsonArray = your_json_array;
for(int i = 0; i < yourJsonArray.length(); i++){
JSONObject json = yourJsonArray.get(i);
tabLayout.addTab(tabLayout.newTab().setText(json.getString("key")));
}
现在使用ViewPager,您可以在标签位置上将列表显示为Fragment。
此链接有助于设置TabLayout android-material-design-working-with-tabs
您还可以使用google Gson库来解析JSON对象。
答案 1 :(得分:0)
你的片段
public class OneFragment extends Fragment{
public OneFragment() {
// Required empty public constructor
}
public static Fragment getInstance(int position){
OneFragment fragment = new OneFragment();
Bundle bundle = new Bundle();
bundle.putInt("position", position)
fragment.setArgument(bundle);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_one, container, false);
int position = getArguments().getInt("position", 0);
// set your list base on position
if(position == 0){
//list 1
} else if(position == 1) {
// list 2
} else {
// list 3
}
return view;
}
}
您的ViewPagerAdapter
class ViewPagerAdapter extends FragmentPagerAdapter {
private JSONArray jsonArray;
public ViewPagerAdapter(FragmentManager manager, JSONArray jsonArray) {
super(manager);
this.jsonArray= jsonArray;
}
@Override
public Fragment getItem(int position) {
return OneFragment.getInstance(position);
}
@Override
public int getCount() {
return jsonArray.length();
}
@Override
public CharSequence getPageTitle(int position) {
return jsonArray.getJSONObject(position).getString("key");
}
}
Tablayout设置
tabLayout = (TabLayout) findViewById(R.id.tabs);
// get your json list
JSONArray jsonArray = your_json_array;
for(int i = 0; i < yourJsonArray.length(); i++){
JSONObject json = yourJsonArray.get(i);
tabLayout.addTab(tabLayout.newTab().setText(json.getString("key")));
}
viewPager = (ViewPager) findViewById(R.id.viewpager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), yourJsonArray);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);