我尝试开发一个系统来建立标签和片段之间的关系。您将看到我的代码和视图。
片段管理滑动,并在点击时直接导航导航。
我设法在用户滑动时修改Selected Tab,但我无法做反向。单击选项卡时视图会发生变化。
你明白我的问题并请你解决方案吗?
这是我的主要活动:
public class MainActivity extends FragmentActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
TextView tw;
ActionBar myActionBar;
ArrayList<Category> ListOfCategory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myActionBar = getActionBar();
myActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ListOfCategory = new ArrayList<Category>();
try {
List<String> categList = new ExecuteJsonLinkTask().execute("http://www.76actu.fr/json.php?d=categories&a=list&key=8smA5YjLG1132zbz301tM94jZO30B7dW").get();
for (String item : categList) {
JSONArray jArray = new JSONArray(item);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Category categ = new Category();
categ.setCategIdSite(json_data.getInt("term_id"));
categ.setName(json_data.getString("name"));
categ.setMoreArticle(true);
categ.setArticleAvailable(false);
if(json_data.getInt("term_id") == 89871){
categ.setActive(false);
} else categ.setActive(true);
Tab tab = myActionBar.newTab();
tab.setText(json_data.getString("name"));
Object obj = new Object();
obj = String.valueOf(json_data.getInt("term_id"));
tab.setTag(obj);
tab.setTabListener(new MyTabListener(getBaseContext()));
// tab.setTabListener(new MyTabListener(getBaseContext()));
myActionBar.addTab(tab);
ListOfCategory.add(categ);
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
myActionBar.setSelectedNavigationItem(arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Category> ListOfCategory = new ArrayList<Category>();
Fragment fragment;
ActionBar myActionBar;
public SectionsPagerAdapter(FragmentManager fm){
super(fm);
myActionBar = getActionBar();
try {
List<String> categList = new ExecuteJsonLinkTask().execute("[API_LINK]").get();
for (String item : categList) {
JSONArray jArray = new JSONArray(item);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Category categ = new Category();
categ.setCategIdSite(json_data.getInt("term_id"));
categ.setName(json_data.getString("name"));
categ.setMoreArticle(true);
categ.setArticleAvailable(false);
if(json_data.getInt("term_id") == 89871){
categ.setActive(false);
} else categ.setActive(true);
ListOfCategory.add(categ);
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position);
args.putInt("categId", ListOfCategory.get(position).getCategIdSite());
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
// Show total pages.
return ListOfCategory.size();
}
@Override
public CharSequence getPageTitle(int position) {
return ListOfCategory.get(position).getName();
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
TextView dummyTextView;
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
int position = getArguments().getInt(ARG_SECTION_NUMBER);
dummyTextView.setText(Integer.toString(getArguments().getInt("categId")));
return rootView;
}
}
public class MyTabListener implements TabListener{
public Context context;
MyTabListener(Context context) {
this.context = context;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// HERE
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
}
这是我的应用程序的屏幕截图:http://i.stack.imgur.com/HBKUj.png
我认为你不需要它,但我可以给你这个。
非常感谢
答案 0 :(得分:1)
在我的代码中,我有这个解决方案,它可以工作:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
public static String TAG="MAIN_ACTIVITY";
public static Context mContext;
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "FRIENDS", "CHAT", "MAP" };
//ON CREATE METHOD:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
{
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft){}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {}
}
TabsPagerAdapter类:
public class TabsPagerAdapter extends FragmentPagerAdapter
{
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
//switch names of your fragments:
switch (index) {
case 0:
return new Stats_Fragment();
case 1:
return new FightsFragment();
case 2:
return new SearchFragment();
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
答案 1 :(得分:0)
您必须在Activity 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"
tools:context="example.com.MainActivity">
//ADD THIS AND PROBLEM WILL BE SOLVED:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</FrameLayout>