我想用viewpager創建動態標籤,但我不知道如何做到這一點。 我會得到一個jsonstring從我的web API是這樣的:如何使用viewpager(android)創建動態選項卡?
{"status":0,"data":[{"category":"fruit","pic":"URL","Content":"apple"},{"category":"fruit","pic":"URL","Content":"banana"},{"category":"fruit","pic":"URL","Content":"papaya"},{"category":"drink","pic":"URL","Content":"milktea"},{"category":"drink","pic":"URL","Content":"blacktea"},{"category":"drink","pic":"URL","Content":"greentea"},{"category":"animal","pic":"URL","Content":"dog"},{"category":"animal","pic":"URL","Content":"cat"},{"category":"animal","pic":"URL","Content":"lion"}]}
我想創建的標籤形式類別(水果,飲料,動物)與viewpager和viewpager設置片段(片段只是有列表視圖,列表視圖有圖片和文字)
現在我的代碼是靜態的
我只設置三個選項卡和三個片段來做到這一點。
屏幕:
和我的代碼現在:
Food.java
public class Food extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(3);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
FoodViewPagerAdapter adapter = new FoodViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Fragment_Food_one(), "FRUIT");
adapter.addFragment(new Fragment_Food_two(), "DRINK");
adapter.addFragment(new Fragment_Food_three(), "ANIMAL");
viewPager.setAdapter(adapter);
}
activity_food.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="edu.shu.foodtest.Food">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Light">
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#EEEEEE"
android:theme="@style/ThemeOverlay.AppCompat.Light"
app:popupTheme="@style/AppTheme.PopupOverlay">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:singleLine="true"
android:text="test"
android:textColor="@color/red"
android:textSize="18sp"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
android:background="#FFFFFF"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEEEEE"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
FoodViewPagerAdapter.java:
public class FoodViewPagerAdapter extends FragmentPagerAdapter {
private final List<android.support.v4.app.Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public FoodViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(android.support.v4.app.Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
Fragment_Food_one.java:
public class Fragment_Food_one extends Fragment {
View view;
ListView mListView;
TextView BrandID;
String brandid;
ArrayList<Foodlistclass> arraylist = new ArrayList<Foodlistclass>();
private String result;
JSONObject response_object;
Context context;
String jsonstring="{\"status\":0,\"data\":[{\"category\":\"fruit\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"apple\"},{\"category\":\"fruit\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"banana\"},{\"category\":\"fruit\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"papaya\"},{\"category\":\"drink\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"milktea\"},{\"category\":\"drink\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"blacktea\"},{\"category\":\"drink\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"greentea\"},{\"category\":\"animal\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"dog\"},{\"category\":\"animal\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"cat\"},{\"category\":\"animal\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"lion\"}]}";
public Fragment_Food_one() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_food_one, container, false);
context = getActivity().getApplicationContext();
mListView = (ListView) view.findViewById(R.id.listviewitem);
try {
setlistview();
} catch (JSONException e) {
e.printStackTrace();
}
return view;
}
public void setlistview() throws JSONException {
jsonstring = jsonstring.replace("\\", "");
response_object = new JSONObject(jsonstring.substring(jsonstring.indexOf("{"), jsonstring.lastIndexOf("}") + 1));
String status = response_object.getString("status");
if (status.equals("0")) {
JSONArray responseData = response_object.optJSONArray("data");
for (int i = 0; i < responseData.length(); i++) {
Map<String, Object> item = new HashMap<String, Object>();
JSONObject jsonChildNode = responseData.getJSONObject(i);
String category = jsonChildNode.optString("category");
System.out.println(category);
String Content = jsonChildNode.optString("Content");
System.out.println(Content);
String pic = jsonChildNode.optString("pic");
System.out.println(pic);
if (category.equals("fruit")) {
arraylist.add(new Foodlistclass(
pic, Content, "a", "aW"
));
}
FoodlistAdapter adapter = new FoodlistAdapter(context, R.layout.food_listitem, arraylist);
mListView.setAdapter(adapter);
}
}
}
fragment_food_one.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical">
<ListView
android:id="@+id/listviewitem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:divider="@android:color/transparent"
android:dividerHeight="10dp"
android:scrollbars="none"></ListView>
</LinearLayout>
我想改變它動態BEC因爲它會在未來添加更多的類別,如果我使用動態標籤,它會自動從jsonstring添加標籤
我是Android開發人員的更新,很多事情我不知道。我研究了很多數據,但我無法實現我想要做的事情。
希望有人能告訴我如何做或給我一個樣本來做到這一點。
謝謝大家,我想對我可憐的英語說抱歉,希望你能理解我上面的意思。
謝謝,謝謝!