我创建了一个选项卡式活动,其中所有列表片段都是在首次打开活动时以编程方式创建的,您可以根据以前保存的数据看到。
public class CcSongBook extends ActionBarActivity implements TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
SharedPreferences vSettings;
SharedPreferences.Editor localEditor;
public SongBookSQLite db = new SongBookSQLite(this, SongBookDatabase.DATABASE, null, SongBookDatabase.VERSION);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sb_book);
vSettings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
localEditor = PreferenceManager.getDefaultSharedPreferences(this).edit();
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(2);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(this.mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
}
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
String[] songbooks = TextUtils.split(vSettings.getString("js_vsb_sbcodes", "NA"), ",");
String[] songbookno = TextUtils.split(vSettings.getString("js_vsb_sbnos", "NA"), ",");
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return songbooks.length-1;
}
@Override
public CharSequence getPageTitle(int position) {
return songbooks[position];
}
public Fragment getItem(int position) {
Bundle data = new Bundle();
data.putInt("songbook", Integer.parseInt(songbookno[position]));
SongBookList sblist = new SongBookList();
sblist.setArguments(data);
return sblist;
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.sb_fragment, container, false);
return rootView;
}
}
}
然后在我的另一个用于列表片段的类中,我基本上是根据从基本活动传递的包数据从我的sqlite数据库中读取一些特定的值:
public class SongBookList extends ListFragment implements LoaderCallbacks<Cursor> {
public SongBookSQLite db;
private String[] My_Text, My_Texti, My_Textii;
List<SongItem> mylist;
ArrayAdapter<String> listAdapter;
SimpleCursorAdapter mCursorAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
db = new SongBookSQLite(getActivity().getBaseContext(), SongBookDatabase.DATABASE, null, SongBookDatabase.VERSION);
Bundle data = this.getArguments();
int curr_songbook = data.getInt("songbook", 1);
mylist = db.getAllSongs(curr_songbook);
List<String> listSongid = new ArrayList<String>();
List<String> listTitle = new ArrayList<String>();
List<String> listContent = new ArrayList<String>();
for (int i = 0; i < mylist.size(); i++) {
listSongid.add(i, Integer.toString(mylist.get(i).getSongid()));
listTitle.add(i, mylist.get(i).getTitle());
listContent.add(i, mylist.get(i).getContent());
}
My_Text = listSongid.toArray(new String[listSongid.size()]);
for (String string : My_Text) { System.out.println(string); }
My_Texti = listTitle.toArray(new String[listTitle.size()]);
for (String stringi : My_Texti) { System.out.println(stringi);}
My_Textii = listContent.toArray(new String[listContent.size()]);
for (String stringii : My_Textii) { System.out.println(stringii); }
setListAdapter(new CustomSongList(getActivity(), My_Text, My_Texti, My_Textii));
return super.onCreateView(inflater, container, savedInstanceState);
}
public void onStart() {
super.onStart();
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myIntent = new Intent(getActivity().getApplicationContext(), SongBookView.class);
Uri data = Uri.withAppendedPath(SongBookProvider.CONTENT_URI, String.valueOf(id+1));
myIntent.setData(data);
startActivity(myIntent);
}
});
}
public Loader<Cursor> onCreateLoader(int arg0, Bundle data) {
Uri uri = SongBookProvider.CONTENT_URI;
return new CursorLoader(getContext(), uri, null, null, new String[]{data.getString("query")}, null);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
this.mCursorAdapter.swapCursor(c);
}
public void onLoaderReset(Loader<Cursor> loader) {
}
现在,如果在任何列表片段上单击列表项,它将在新活动中打开,如下所示:
public class SongBookView extends ActionBarActivity {
TextView mSongCont;
Cursor cursor;
public Uri mUri;
SongBookSQLite db;
SharedPreferences vSettings;
SharedPreferences.Editor localEditor;
String VSB_SETTINGS, FONT_SIZE, CURRENT_SONG;
public int CurrSong, FontSize;
ListView StanzasList;
SongItem currentSong;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.song_view);
db = new SongBookSQLite(this, SongBookDatabase.DATABASE, null, SongBookDatabase.VERSION);
StanzasList =(ListView)findViewById(R.id.stanzalist);
vSettings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
localEditor = PreferenceManager.getDefaultSharedPreferences(this).edit();
changeStatusBarColor();
Long rated_me_not = vSettings.getLong("js_vsb_rated_me_not", 0);
Long time_used = (System.currentTimeMillis() - rated_me_not) / 1000;
if (!PreferenceManager.getDefaultSharedPreferences(this).getBoolean("js_vsb_rate_me", false)) {
if (time_used > 18000 ) {
rateMePlease();
}
}
FontSize = PreferenceManager.getDefaultSharedPreferences(this).getInt("js_vsb_font_size", 15);
mUri = getIntent().getData();
CurrSong = Integer.parseInt(mUri.toString().replaceAll("\\D+", ""));
openCurrentSong();
}
public void openCurrentSong(){
localEditor.putInt("js_vsb_curr_song", CurrSong).commit();
currentSong = db.readSong(CurrSong);
setTitle(currentSong.getTitle());
String[] Stanzas = TextUtils.split(currentSong.getContent(), "`");
CustomSongView adapter = new CustomSongView(this, Stanzas);
StanzasList.setAdapter(adapter);
}
@Override
public void onBackPressed() {
Intent intent = new Intent(this, CcSongBook.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
}
这里的主要问题是如何防止主要活动再次重新加载,因为它正在发生。
答案 0 :(得分:0)
我已经看到按下后退按钮时它就像启动了一个新意图一样。你怎么样删除了这条线:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div>
<label class='form-control' data-toggle='tooltip' data-placement='bottom' title='something'>truncated text</label>
<div>
来自onBackPressed()方法的
答案 1 :(得分:0)
实际上删除下面的行确实解决了我的问题
Intent intent = new Intent(this, CcSongBook.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);