我在github上找到了一个用于实践的应用程序。在应用程序中,您可以向左滑动以查看类别,而应向右滑动以查看主页。但是我也想通过按一下按钮来获得类别。应用程序菜单中有首选项按钮,我想要;当我点击按钮时,我应该获得类别页面,然后再次点击(如向右滑动)我应该获得主页。我尝试了20-25种不同的组合,但我不成功。我认为类别页面不是片段。你们能看一下这些代码并告诉我是否可能吗?
这是项目github地址https://github.com/MountZoft/BiblePromiseBox-Free
这是主页(RandomVerse.java)
public class RandomVerse extends AppCompatActivity {
final static int CATEGORY_MIN = 0;
final static int CATEGORY_MAX = 11;
final static int VERSE_MIN = 0;
final static int VERSE_MAX = 9;
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private AdView mAdView;
MenuItem shareItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_random_verse);
setTitle(getString(R.string.random_verse_tab_title));
MobileAds.initialize(this, "ca-app-pub-9873336567720966~8715989856");//Admob-AppID-orginal
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
//======================================================================
//peaceVerses
//======================================================================
//======================================================================
//forgivenessVerses
//======================================================================
DataStorage.forgivenessVerses.add(getString(R.string.forgiveness_verses_1));
DataStorage.forgivenessVerses.add(getString(R.string.forgiveness_verses_2));
DataStorage.forgivenessVerses.add(getString(R.string.forgiveness_verses_3));
DataStorage.forgivenessVerses.add(getString(R.string.forgiveness_verses_4));
DataStorage.forgivenessVerses.add(getString(R.string.forgiveness_verses_5));
DataStorage.forgivenessVerses.add(getString(R.string.forgiveness_verses_6));
DataStorage.forgivenessVerses.add(getString(R.string.forgiveness_verses_7));
DataStorage.forgivenessVerses.add(getString(R.string.forgiveness_verses_8));
DataStorage.forgivenessVerses.add(getString(R.string.forgiveness_verses_9));
DataStorage.forgivenessVerses.add(getString(R.string.forgiveness_verses_10));
//======================================================================
//strengthVerses
//======================================================================
DataStorage.strengthVerses.add(getString(R.string.strength_verses_1));
DataStorage.strengthVerses.add(getString(R.string.strength_verses_2));
DataStorage.strengthVerses.add(getString(R.string.strength_verses_3));
DataStorage.strengthVerses.add(getString(R.string.strength_verses_4));
DataStorage.strengthVerses.add(getString(R.string.strength_verses_5));
DataStorage.strengthVerses.add(getString(R.string.strength_verses_6));
DataStorage.strengthVerses.add(getString(R.string.strength_verses_7));
DataStorage.strengthVerses.add(getString(R.string.strength_verses_8));
DataStorage.strengthVerses.add(getString(R.string.strength_verses_9));
DataStorage.strengthVerses.add(getString(R.string.strength_verses_10));
//======================================================================
//loveVerses
//======================================================================
DataStorage.loveVerses.add(getString(R.string.love_verses_1));
DataStorage.loveVerses.add(getString(R.string.love_verses_2));
DataStorage.loveVerses.add(getString(R.string.love_verses_3));
DataStorage.loveVerses.add(getString(R.string.love_verses_4));
DataStorage.loveVerses.add(getString(R.string.love_verses_5));
DataStorage.loveVerses.add(getString(R.string.love_verses_6));
DataStorage.loveVerses.add(getString(R.string.love_verses_7));
DataStorage.loveVerses.add(getString(R.string.love_verses_8));
DataStorage.loveVerses.add(getString(R.string.love_verses_9));
DataStorage.loveVerses.add(getString(R.string.love_verses_10));
//======================================================================
//inspirationalVerses
//======================================================================
DataStorage.inspirationalVerses.add(getString(R.string.inspirational_verses_1));
DataStorage.inspirationalVerses.add(getString(R.string.inspirational_verses_2));
DataStorage.inspirationalVerses.add(getString(R.string.inspirational_verses_3));
DataStorage.inspirationalVerses.add(getString(R.string.inspirational_verses_4));
DataStorage.inspirationalVerses.add(getString(R.string.inspirational_verses_5));
DataStorage.inspirationalVerses.add(getString(R.string.inspirational_verses_6));
DataStorage.inspirationalVerses.add(getString(R.string.inspirational_verses_7));
DataStorage.inspirationalVerses.add(getString(R.string.inspirational_verses_8));
DataStorage.inspirationalVerses.add(getString(R.string.inspirational_verses_9));
DataStorage.inspirationalVerses.add(getString(R.string.inspirational_verses_10));
//======================================================================
//adventVerses
//======================================================================
DataStorage.adventVerses.add(getString(R.string.advent_verses_1));
DataStorage.adventVerses.add(getString(R.string.advent_verses_2));
DataStorage.adventVerses.add(getString(R.string.advent_verses_3));
DataStorage.adventVerses.add(getString(R.string.advent_verses_4));
DataStorage.adventVerses.add(getString(R.string.advent_verses_5));
DataStorage.adventVerses.add(getString(R.string.advent_verses_6));
DataStorage.adventVerses.add(getString(R.string.advent_verses_7));
DataStorage.adventVerses.add(getString(R.string.advent_verses_8));
DataStorage.adventVerses.add(getString(R.string.advent_verses_9));
DataStorage.adventVerses.add(getString(R.string.advent_verses_10));
//======================================================================
//blessingsVerses
//======================================================================
DataStorage.blessingsVerses.add(getString(R.string.blessings_verses_1));
DataStorage.blessingsVerses.add(getString(R.string.blessings_verses_2));
DataStorage.blessingsVerses.add(getString(R.string.blessings_verses_3));
DataStorage.blessingsVerses.add(getString(R.string.blessings_verses_4));
DataStorage.blessingsVerses.add(getString(R.string.blessings_verses_5));
DataStorage.blessingsVerses.add(getString(R.string.blessings_verses_6));
DataStorage.blessingsVerses.add(getString(R.string.blessings_verses_7));
DataStorage.blessingsVerses.add(getString(R.string.blessings_verses_8));
DataStorage.blessingsVerses.add(getString(R.string.blessings_verses_9));
DataStorage.blessingsVerses.add(getString(R.string.blessings_verses_10));
//======================================================================
//**********************************************************************
//======================================================================
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
if(mViewPager.getCurrentItem() == 0){
setTitle(getString(R.string.random_verse_tab_title));
shareItem.setVisible(true);
}else{
setTitle(getString(R.string.share_verse_tab_title));
shareItem.setVisible(false);
}
}
});
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
RandomTab randomTab = new RandomTab();
return randomTab;
case 1:
ShareTab shareTab = new ShareTab();
return shareTab;
default:
return null;
}
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
}
//==================================================================================================
@Override
public boolean onCreateOptionsMenu( Menu menu ) {
getMenuInflater().inflate( R.menu.menu, menu );
shareItem = menu.findItem(R.id.action_share);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_share:
ShareFunction();
return true;
case R.id.action_preference:
Intent intent = new Intent(this,Preference.class);
startActivity(intent);
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
//==================================================================================================
public String RandomVerseGenerator(){
Random random = new Random();
randomCategory = random.nextInt(CATEGORY_MAX - CATEGORY_MIN) + CATEGORY_MIN;
randomVerse = random.nextInt(VERSE_MAX - VERSE_MIN) + VERSE_MIN;
switch (randomCategory){
case 0:verse = DataStorage.peaceVerses.get(DataStorage.randomVerse);
return verse;
case 1:verse = DataStorage.faithVerses.get(DataStorage.randomVerse);
return verse;
case 2:verse = DataStorage.healingVerses.get(DataStorage.randomVerse);
return verse;
case 3:verse = DataStorage.encouragingVerses.get(DataStorage.randomVerse);
return verse;
case 4:verse = DataStorage.hopeVerses.get(DataStorage.randomVerse);
return verse;
case 5:verse = DataStorage.frindshipVerses.get(DataStorage.randomVerse);
return verse;
case 6:verse = DataStorage.forgivenessVerses.get(DataStorage.randomVerse);
return verse;
case 7:verse = DataStorage.strengthVerses.get(DataStorage.randomVerse);
return verse;
case 8:verse = DataStorage.loveVerses.get(DataStorage.randomVerse);
return verse;
case 9:verse = DataStorage.inspirationalVerses.get(DataStorage.randomVerse);
return verse;
case 10:verse = DataStorage.adventVerses.get(DataStorage.randomVerse);
return verse;
case 11:verse = DataStorage.blessingsVerses.get(DataStorage.randomVerse);
return verse;
default:return null;
}
}
public void ShareFunction(){
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
app_name_share = getString(R.string.app_name_share);
app_link = getString(R.string.app_link);
String shareBody = verse+app_name_share+app_link;
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, getString(R.string.share_intent_msg)));
}
//==============================================================================================
}
我在这方面做些事,但是就像我说我做不到。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_share:
ShareFunction();
return true;
case R.id.action_preference:
Intent intent = new Intent(this,Preference.class);
startActivity(intent);
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
这是ShareTab(类别页面)
package com.inc.mountzoft.BiblePromiseBoxApp;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
public class ShareTab extends android.support.v4.app.Fragment {
MyRecyclerViewAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.share_tab_layout, container, false);
// data to populate the RecyclerView with
DataStorage.rowData.add(new CategoryRecyclerviewData(R.drawable.peace,"for example"));
DataStorage.rowData.add(new CategoryRecyclerviewData(R.drawable.faith,getString(R.string.faith_verses_category_title)));
DataStorage.rowData.add(new CategoryRecyclerviewData(R.drawable.healing,getString(R.string.healing_verses_category_title)));
DataStorage.rowData.add(new CategoryRecyclerviewData(R.drawable.encouraging,getString(R.string.encouraging_verses_category_title)));
DataStorage.rowData.add(new CategoryRecyclerviewData(R.drawable.hope,getString(R.string.hope_verses_category_title)));
DataStorage.rowData.add(new CategoryRecyclerviewData(R.drawable.friendship,getString(R.string.frindship_verses_category_title)));
DataStorage.rowData.add(new CategoryRecyclerviewData(R.drawable.forgiveness,getString(R.string.forgiveness_verses_category_title)));
DataStorage.rowData.add(new CategoryRecyclerviewData(R.drawable.strength,getString(R.string.strength_verses_category_title)));
DataStorage.rowData.add(new CategoryRecyclerviewData(R.drawable.love,getString(R.string.love_verses_category_title)));
DataStorage.rowData.add(new CategoryRecyclerviewData(R.drawable.inspirational,getString(R.string.inspirational_verses_category_title)));
DataStorage.rowData.add(new CategoryRecyclerviewData(R.drawable.advent,getString(R.string.advent_verses_category_title)));
DataStorage.rowData.add(new CategoryRecyclerviewData(R.drawable.blessings,getString(R.string.blessings_verses_category_title)));
// set up the RecyclerView
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.category_list);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
adapter = new MyRecyclerViewAdapter(getContext(), DataStorage.rowData);
recyclerView.setAdapter(adapter);
return rootView;
}
}
这是RandomTab(随机词页面)
package com.inc.mountzoft.BiblePromiseBoxApp;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.bakerj.infinitecards.AnimationTransformer;
import com.bakerj.infinitecards.CardItem;
import com.bakerj.infinitecards.InfiniteCardView;
import com.bakerj.infinitecards.ZIndexTransformer;
import com.bakerj.infinitecards.transformer.DefaultCommonTransformer;
import com.bakerj.infinitecards.transformer.DefaultTransformerToBack;
import com.bakerj.infinitecards.transformer.DefaultTransformerToFront;
import com.bakerj.infinitecards.transformer.DefaultZIndexTransformerCommon;
import com.onurkaganaldemir.ktoastlib.KToast;
import java.util.Random;
import static com.inc.mountzoft.BiblePromiseBoxApp.DataStorage.randomCategory;
import static com.inc.mountzoft.BiblePromiseBoxApp.DataStorage.randomVerse;
import static com.inc.mountzoft.BiblePromiseBoxApp.DataStorage.verse;
public class RandomTab extends android.support.v4.app.Fragment {
private InfiniteCardView mCardView;
private BaseAdapter mAdapter1, mAdapter2;
private int[] resId = {R.drawable.pic1, R.drawable.pic3, R.drawable.pic2,
R.drawable.pic4, R.drawable.pic5};
private boolean mIsAdapter1 = true;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.random_tab_layout, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
TextView textView = (TextView) getView().findViewById(R.id.random_verse_tv);
RandomVerse randomVerse = new RandomVerse();
verse = randomVerse.RandomVerseGenerator();
textView.setText(verse);
textView.setMovementMethod(new ScrollingMovementMethod());
mCardView = getView().findViewById(R.id.view);
mAdapter1 = new MyAdapter(resId);
mAdapter2 = new MyAdapter(resId);
mCardView.setAdapter(mAdapter1);
mCardView.setCardAnimationListener(new InfiniteCardView.CardAnimationListener() {
TextView textView = (TextView) getView().findViewById(R.id.random_verse_tv);
@Override
public void onAnimationStart() {
textView.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationEnd() {
RandomVerse randomVerse = new RandomVerse();
verse = randomVerse.RandomVerseGenerator();
textView.setText(verse);
textView.setVisibility(View.VISIBLE);
KToast.customBackgroudToast(getActivity(), getString(R.string.k_toast_share_your_daily_verse), Gravity.BOTTOM, KToast.LENGTH_SHORT,R.drawable.background_custom_toast, null ,R.drawable.ic_share_white_24dp);
}
});
initButton();
}
private void initButton() {
getView().findViewById(R.id.randomize_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mIsAdapter1) {
setStyle2();
} else {
setStyle3();
}
mCardView.bringCardToFront(1);
}
});
}
private void setStyle2() {
mCardView.setClickable(true);
mCardView.setAnimType(InfiniteCardView.ANIM_TYPE_SWITCH);
mCardView.setAnimInterpolator(new OvershootInterpolator(-18));
mCardView.setTransformerToFront(new DefaultTransformerToFront());
mCardView.setTransformerToBack(new AnimationTransformer() {
@Override
public void transformAnimation(View view, float fraction, int cardWidth, int cardHeight, int fromPosition, int toPosition) {
int positionCount = fromPosition - toPosition;
float scale = (0.8f - 0.1f * fromPosition) + (0.1f * fraction * positionCount);
view.setScaleX(scale);
view.setScaleY(scale);
if (fraction < 0.5) {
view.setRotationX(180 * fraction);
} else {
view.setRotationX(180 * (1 - fraction));
}
}
@Override
public void transformInterpolatedAnimation(View view, float fraction, int cardWidth, int cardHeight, int fromPosition, int toPosition) {
int positionCount = fromPosition - toPosition;
float scale = (0.8f - 0.1f * fromPosition) + (0.1f * fraction * positionCount);
view.setTranslationY(-cardHeight * (0.8f - scale) * 0.5f - cardWidth * (0.02f *
fromPosition - 0.02f * fraction * positionCount));
}
});
mCardView.setZIndexTransformerToBack(new ZIndexTransformer() {
@Override
public void transformAnimation(CardItem card, float fraction, int cardWidth, int cardHeight, int fromPosition, int toPosition) {
if (fraction < 0.4f) {
card.zIndex = 1f + 0.01f * fromPosition;
} else {
card.zIndex = 1f + 0.01f * toPosition;
}
}
@Override
public void transformInterpolatedAnimation(CardItem card, float fraction, int cardWidth, int cardHeight, int fromPosition, int toPosition) {
}
});
}
private void setStyle3() {
mCardView.setClickable(false);
mCardView.setAnimType(InfiniteCardView.ANIM_TYPE_FRONT_TO_LAST);
mCardView.setAnimInterpolator(new OvershootInterpolator(-8));
mCardView.setTransformerToFront(new DefaultCommonTransformer());
mCardView.setTransformerToBack(new AnimationTransformer() {
@Override
public void transformAnimation(View view, float fraction, int cardWidth, int cardHeight, int fromPosition, int toPosition) {
int positionCount = fromPosition - toPosition;
float scale = (0.8f - 0.1f * fromPosition) + (0.1f * fraction * positionCount);
view.setScaleX(scale);
view.setScaleY(scale);
if (fraction < 0.5) {
view.setTranslationX(cardWidth * fraction * 1.5f);
view.setRotationY(-45 * fraction);
} else {
view.setTranslationX(cardWidth * 1.5f * (1f - fraction));
view.setRotationY(-45 * (1 - fraction));
}
}
@Override
public void transformInterpolatedAnimation(View view, float fraction, int cardWidth, int cardHeight, int fromPosition, int toPosition) {
int positionCount = fromPosition - toPosition;
float scale = (0.8f - 0.1f * fromPosition) + (0.1f * fraction * positionCount);
view.setTranslationY(-cardHeight * (0.8f - scale) * 0.5f - cardWidth * (0.02f *
fromPosition - 0.02f * fraction * positionCount));
}
});
mCardView.setZIndexTransformerToBack(new ZIndexTransformer() {
@Override
public void transformAnimation(CardItem card, float fraction, int cardWidth, int cardHeight, int fromPosition, int toPosition) {
if (fraction < 0.5f) {
card.zIndex = 1f + 0.01f * fromPosition;
} else {
card.zIndex = 1f + 0.01f * toPosition;
}
}
@Override
public void transformInterpolatedAnimation(CardItem card, float fraction, int cardWidth, int cardHeight, int fromPosition, int toPosition) {
}
});
}
private static class MyAdapter extends BaseAdapter {
private int[] resIds = {};
MyAdapter(int[] resIds) {
this.resIds = resIds;
}
@Override
public int getCount() {
return resIds.length;
}
@Override
public Integer getItem(int position) {
return resIds[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout
.item_card, parent, false);
}
convertView.setBackgroundResource(resIds[position]);
return convertView;
}
}
}