gpt4 book ai didi

java - ActionBarSherlock 和 FragmentTabsPager

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:10:07 25 4
gpt4 key购买 nike

到目前为止,正如很多人所做的那样,我正在将 FragmentTabsPager 实现到我的应用程序中,该应用程序使用的是 ActionBarSherlock 4.0。但是,我迷路了。

fragment ,以及谷歌围绕它的所有小想法、计划和方法,都让我感到困惑。如果有人能看一看我的代码并引导我完成这个过程,并提供帮助使其正常工作,我会感谢他们一千次:D。

我有另一个项目以 ViewPager 开头,但 Tab 组合得更好,尤其是当它们位于横向和平板电脑上的 ActionBar 中时。

我的代码已全部压缩并准备好通过这里: http://dl.dropbox.com/u/21807195/Antonius%20College%202.zip

提前致谢!

最佳答案

我将向您展示我的代码,其中包含一个 ViewPager、TabListener 和链接到每个选项卡的 fragment 系统。它实现了 ABS,但到目前为止,仍然在 Gingerbread 和更低版本上崩溃(不过在 ICS 上运行得很好):

import java.util.ArrayList;
import library.DatabaseHandler;
import org.json.JSONObject;
import com.actionbarsherlock.R;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;

public class Polling extends SherlockFragmentActivity {
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
private final static String TAG = "21st Polling:";

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayShowHomeEnabled(false);

mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText(R.string.login),
LoginFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.economics),
EconFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.elections),
ElectionsFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.politics),
PoliticsFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.science),
ScienceFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.finance),
FinanceFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.religion),
ReligionFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.military),
MilitaryFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.international),
InternationalFragment.class, null);
Log.v(TAG, (String)bar.getTabAt(0).getText());

}
public static class TabsAdapter extends FragmentPagerAdapter
implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

static final class TabInfo {
private final Class<?> clss;
private final Bundle args;

TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}

public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.getSupportActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}

public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}


public int getCount() {
return mTabs.size();
}

public SherlockFragment getItem(int position) {
TabInfo info = mTabs.get(position);
return (SherlockFragment)Fragment.instantiate(mContext, info.clss.getName(), info.args);
}


public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}


public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}


public void onPageScrollStateChanged(int state) {
}


public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
//Log.v(TAG, "clicked");
Object tag = tab.getTag();
for (int i=0; i<mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
}
}
}

public void onTabUnselected(Tab tab, FragmentTransaction ft) {}

public void onTabReselected(Tab tab, FragmentTransaction ft) {}

public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {}

public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {}
}

这是一个 fragment 的样子:

package com.davekelley.polling;

import com.actionbarsherlock.R;
import com.actionbarsherlock.app.SherlockFragment;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MilitaryFragment extends SherlockFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

return inflater.inflate(R.layout.militaryfragment, container, false);
}
}

我最后要指出的是,我的代码还有一个问题:在用户按下返回键后,各个 fragment 并不总是重新加载它们的界面(这会导致从屏幕上删除整个应用程序,无论它们位于哪个选项卡/fragment 上,因为我没有 backStack)。这就是我现在正在努力的。我想一旦我对它进行了排序,我就会尝试弄清楚为什么我仍然没有让 Gingerbread 执行正常运行。无论哪种方式,我希望浏览此代码可以帮助您。

这是一个带有一些 onClickListeners 的 fragment :

import com.actionbarsherlock.R;
import com.actionbarsherlock.app.SherlockFragment;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class LoginFragment extends SherlockFragment {

Button loginButton;
Button registerButton;
Polling activity;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.loginfragment, container, false);

return v;
}

public void onResume() {
super.onResume();
Log.d("Econ", "onresume");

loginButton = (Button) getActivity().findViewById(R.id.loginButton);
loginButton.setOnClickListener(loginListener);
registerButton = (Button) getActivity().findViewById(R.id.registerButton);
registerButton.setOnClickListener(registerListener);

}

public OnClickListener loginListener = new OnClickListener() {
@Override
public void onClick(View v) {

if(loginButton.getText().toString() == "Log Out") {

activity.loginReport(2);
loginButton.setText(R.string.login);
//Remove user from dB sqllite when I know how
}
else {
Log.v("LoginF", "onclick");
ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Logging in...");
LoginTask loginTask = new LoginTask((Polling) getActivity(), progressDialog);
loginTask.execute();
}
}
};


public OnClickListener registerListener = new OnClickListener() {
@Override
public void onClick(View v) {


Log.v("Register", "onclick");
ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Registering new user...");
RegisterTask registerTask = new RegisterTask((Polling) getActivity(), progressDialog);
registerTask.execute();
}

};

public void onAttach(Activity activity) {
super.onAttach(activity);
this.activity = (Polling) activity;
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


}

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}

public void onStart() {
super.onStart();
Log.d("Econ", "onstart");
}


public void onPause() {
super.onPause();Log.d("Econ", "onpause");
}

public void onStop() {
super.onStop();
Log.d("Econ", "onstop");
}

public void onDestroyView() {
super.onDestroyView();
Log.d("Econ", "ondestroyview");
}

public void onDestroy() {
super.onDestroy();
Log.d("Econ", "ondestroy");

}

public void onDetach() {
super.onDetach();
Log.d("Econ", "ondetach");
}

}

您看到的 loginTask 对象实际上是扩展 ASyncTask 的类 - 它们处理连接到我的服务器并注册/登录。

我认为再添加一点代码会很有帮助。这是我的另一个 fragment ,如 LoginFragment,但它使 UI 膨胀有点不同。最终,您在下面的 while 循环中看到的内容将进入 ASyncTask 以从服务器获取每个问题,而不是您看到的虚拟字符串数组:

public class EconFragment extends SherlockFragment {

private TableLayout questionContainer;
int pos = 0;
private String[] titles = {"The first title ", "hallo1","hallo2", "hallo3",
"hallo4", "hallo5","hallo6", "hallo7","hallo8", "hallo9"};

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.v("Econ", "onCreateView");
View v = inflater.inflate(R.layout.econfragment, container, false);
questionContainer = (TableLayout) v.findViewById(R.id.questionContainer);
//bs
int leftMargin=5;
int topMargin=5;
int rightMargin=5;
int bottomMargin=5;
while (pos < 10) {
View question = inflater.inflate(R.layout.question, null);
question.setId(pos);
TextView title = (TextView) question.findViewById(R.id.questionTextView);
title.setText(titles[pos]);
Button charts = (Button) question.findViewById(R.id.chartsButton);
charts.setId(pos);
charts.setOnClickListener(chartsListener);
TableRow tr = (TableRow) question;
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(trParams);
Log.v("econ", "while loop");
questionContainer.addView(tr);
pos++;
}
pos = 0;
return v;
}

关于java - ActionBarSherlock 和 FragmentTabsPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9790279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com