我正在尝试在我的应用中进行导航,但我无法理解如何在我的应用中实现导航标签。 我一直关注Creating Swipe Views with Tabs
作为初学者,我无法理解如何使用FragmentStatePagerAdapter以及将其放置在MainAcitvity内部或作为新活动。
我在谷歌搜索了更多的指南,但我发现的只是堕落或无关紧要。
我需要您的帮助来简化在应用中实施导航标签的方式。
答案 0 :(得分:0)
您在FragmentActivity中创建适配器,并将其附加到ViewPager
。 ViewPager
是布局的一部分,应该保存在XML代码中。请参阅您关联的网页中的CollectionDemoActivity
。
答案 1 :(得分:0)
这是来自我的项目,接受代码并实现它:
public class KataloziSlike extends AppCompatActivity {
private AdapterCustomPage mAdapterCustomPage;
private ViewPager mViewPager;
private TextView tv, brojStranica;
private ImageButton settings, share, locirajMe, back, next, previous;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_katalozi_slike);
if (Static.katalozi.getSlikeKataloga().size() == 1) {
Message.message(KataloziSlike.this, "Katalog ima samo jednu stranu");
}
brojStranica = (TextView) findViewById(R.id.tvBrojStranicaKataloga);
brojStranica.setText(String.valueOf(1) + "/" + String.valueOf(Static.katalozi.getSlikeKataloga().size() + 1));
mAdapterCustomPage = new AdapterCustomPage(getFragmentManager(), this);
mViewPager = (ViewPager) findViewById(R.id.view_pagerZaSlikeKataloga);
mViewPager.setAdapter(mAdapterCustomPage);
mViewPager.setCurrentItem(0);
next = (ImageButton) findViewById(R.id.imageButtonKataloziNext);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pozicija = mViewPager.getCurrentItem();
if (pozicija < mViewPager.getAdapter().getCount()) {
mViewPager.setCurrentItem(pozicija + 1);
}
}
});
previous = (ImageButton) findViewById(R.id.imageButtonKataloziPrevious);
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pozicija = mViewPager.getCurrentItem();
if (pozicija > 0) {
mViewPager.setCurrentItem(pozicija - 1);
}
}
});
if (mViewPager.getCurrentItem() == 0) {
previous.setVisibility(View.INVISIBLE);
}
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 0) {
previous.setVisibility(View.INVISIBLE);
} else {
previous.setVisibility(View.VISIBLE);
}
if (position == Static.katalozi.getSlikeKataloga().size()) {
next.setVisibility(View.INVISIBLE);
} else {
next.setVisibility(View.VISIBLE);
}
if (Static.katalozi.getSlikeKataloga().size() != 1) {
if (position == 0) {
Message.message(KataloziSlike.this, "Na prvoj stranici kataloga ste");
} else if (position == Static.katalozi.getSlikeKataloga().size() - 1) {
Message.message(KataloziSlike.this, "Na poslednjoj stranici kataloga ste");
}
}
if (position <= Static.katalozi.getSlikeKataloga().size() - 1) {
YourFragmentInterface fragment = (YourFragmentInterface) mAdapterCustomPage.instantiateItem(mViewPager, position);
if (fragment != null) {
fragment.fragmentBecameVisible();
}
}
brojStranica.setText(String.valueOf(position + 1) + "/" + String.valueOf(Static.katalozi.getSlikeKataloga().size() + 1));
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}}
适配器类:
package aik.akcijeikatalozi.adapteri;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.util.Log;
import java.util.ArrayList;
import aik.akcijeikatalozi.fragments.ImageFragmentKatalozi;
import aik.akcijeikatalozi.fragments.ImageFragmentMapKatalozi;
import aik.akcijeikatalozi.objetki.Katalozi;
import aik.akcijeikatalozi.utils.Message;
import aik.akcijeikatalozi.utils.Static;
/**
* Created by Vukasin on 7/11/2015.
*/
public class AdapterCustomPage extends FragmentStatePagerAdapter {
protected Context mContext;
int maxBrojStrana = Static.katalozi.getSlikeKataloga().size();
int pozicija;
private ArrayList<Katalozi> mKatalozi;
public AdapterCustomPage(FragmentManager fm, Context context) {
super(fm);
mContext = context;
}
@Override
public Fragment getItem(int position) {
// Create fragment object
if (position == maxBrojStrana) {
Fragment fragment = new ImageFragmentMapKatalozi();
return fragment;
} else {
Fragment fragment = new ImageFragmentKatalozi();
fragment.onPause();
Bundle args = new Bundle();
Log.i("Poizicija", String.valueOf(position));
String url = Static.katalozi.getSlikeKataloga().get(position);
args.putString("url", url);
fragment.setArguments(args);
String link = Static.katalozi.getUrl();
args.putString("linkKataloga", link);
return fragment;
}
}
@Override
public int getItemPosition(Object object) {
pozicija = super.getItemPosition(object);
return super.getItemPosition(object);
}
@Override
public int getCount() {
return maxBrojStrana + 1;
}
}
片段类:
public class ImageFragmentKatalozi extends Fragment implements KataloziSlike.YourFragmentInterface {
private PhotoViewAttacher mAttacher;
private ImageView imageView;
private Bundle args;
private Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_katalozi_slike, container, false);
args = getArguments();
imageView = (ImageView) rootView.findViewById(R.id.imageViewAdapterZaListanjeKataloga);
Callback imageLoadedCallback = new Callback() {
@Override
public void onSuccess() {
if (mAttacher != null) {
mAttacher.update();
} else {
mAttacher = new PhotoViewAttacher(imageView);
}
}
@Override
public void onError() {
// TODO Auto-generated method stub
}
};
Picasso.with(getActivity()).load(args.getString("url")).placeholder(R.drawable.load).error(R.drawable.error).into(imageView, imageLoadedCallback);
mAttacher = new PhotoViewAttacher(imageView);
button = (Button) rootView.findViewById(R.id.zaProveru);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return rootView;
}
@Override
public void onResume() {
super.onResume();
Toast.makeText(getActivity(), "sadsadasd", Toast.LENGTH_SHORT);
}
@Override
public void fragmentBecameVisible() {
mAttacher.update();
}
}