我有一个网格视图。单击gridview中的任何项时,我将转到一个ViewPager/FragmentStatePagerAdapter中,该ViewPager/FragmentStatePagerAdapter包含gridview的所有项。我希望视图页导航显示选定的项目。Viewpager总是加载第一个项目。如何解决这个问题?
活动:
我正在从gridview接收BuyActivity Intent Intent=getIntent()中的选定选项;pos=Intent.GetIntExtra(“选定”,0);
公共类BuyActivity扩展AppCompatActivity{
SharedPreferences preferences,sharedPreferences;
final String[] categoryName = new String[]{"Science","Arts","Engineering","Medical"};
ArrayList<Integer> choices = new ArrayList<>();
ArrayList<BuyHorizontalPageModel> buyList = new ArrayList<>();
ViewPager viewPager;
int selected;
BuySwipeAdapter swipeAdapter;
int pos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buy);
preferences = this.getSharedPreferences("com.example.sabmad.basenav", Context.MODE_PRIVATE);
//preferences = this.getSharedPreferences("com.example.sabmad.basenav", Context.MODE_PRIVATE);
int choiceSingle = preferences.getInt("single",0);
int choiceFilter = preferences.getInt("filter",0);
//Log.i("single",Integer.toString(choiceSingle));
//Log.i("filter",Integer.toString(choiceFilter));
//Log.i("previous",Integer.toString(previous));
Intent intent = getIntent();
pos = intent.getIntExtra("selected",0);
Log.i("item from grid",Integer.toString(pos));
viewPager = (ViewPager)findViewById(R.id.buyPageView);
viewPager.setCurrentItem(pos);
//getSupportFragmentManager().get
swipeAdapter = new BuySwipeAdapter(getSupportFragmentManager(),buyList);
viewPager.setAdapter(swipeAdapter);
viewPager.setOffscreenPageLimit(1);
if(choiceSingle>0){
ParseQuery<ParseObject> query =new ParseQuery<ParseObject>("SellBook");
query.whereEqualTo("book_category",categoryName[choiceSingle-1]);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if(e==null){
for(ParseObject object : objects){
ParseFile file = object.getParseFile("book_image");
String user_id = object.getString("user_id");
String price = object.getString("book_price");
String condition = object.getString("book_condition");
BuyHorizontalPageModel buyModel = new BuyHorizontalPageModel(file,price,condition,user_id);
buyList.add(buyModel);
swipeAdapter.notifyDataSetChanged();
}
}
}
});
}if(choiceSingle == 0 && choiceFilter >0){
choices.clear();
ArrayList<ParseQuery<ParseObject>> queries = new ArrayList<ParseQuery<ParseObject>>();
UserFilterChoice filterChoice = new UserFilterChoice();
choices = filterChoice.choices(choiceFilter);
for(int i = 0; i < choices.size(); i++){
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("SellBook");
query.whereEqualTo("book_category",categoryName[choices.get(i)-1]);
queries.add(query);
}
ParseQuery<ParseObject> query = ParseQuery.or(queries);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if(e==null){
for( ParseObject object:objects){
ParseFile file = object.getParseFile("book_image");
String user_id = object.getString("user_id");
String price = object.getString("book_price");
String condition = object.getString("book_condition");
BuyHorizontalPageModel buyModel = new BuyHorizontalPageModel(file,price,condition,user_id);
buyList.add(buyModel);
swipeAdapter.notifyDataSetChanged();
}
}
}
});
}
}
}
适配器:
公共类BuySwipeAdapter扩展FragmentStatePagerAdapter{
private ArrayList<BuyHorizontalPageModel> buySet;
FragmentManager fm;
//SparseArray<Fragment> registeredFragment = new SparseArray<>();
public BuySwipeAdapter(FragmentManager fm,ArrayList<BuyHorizontalPageModel> buySet) {
super(fm);
this.buySet = buySet;
this.fm = fm;
}
@Override
public Fragment getItem(int position) {
BuyHorizontalPageModel pageSet = buySet.get(position);
Fragment pageFragment = new FragmentPage();
final Bundle bundle = new Bundle();
bundle.putString("price",pageSet.getPrice());
bundle.putString("condition",pageSet.getCondition());
bundle.putString("user_id",pageSet.getUser_id());
ParseFile file = pageSet.getFile();
try {
byte[] imageData = file.getData();
bundle.putByteArray("book_image",imageData);
} catch (ParseException e) {
e.printStackTrace();
}
pageFragment.setArguments(bundle);
return pageFragment;
}
@Override
public int getCount() {
return buySet.size();
}
ImageView book_image;
CircleImageView profile_user;
TextView price,condition;
String user;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.buy_items,container,false);
book_image = (ImageView)view.findViewById(R.id.book_image);
profile_user = (CircleImageView)view.findViewById(R.id.profile_image);
price = (TextView)view.findViewById(R.id.price);
condition = (TextView)view.findViewById(R.id.condition);
// retrieve Bundle
Bundle bundle = getArguments();
String priceBook = bundle.getString("price");
String conditionBook = bundle.getString("condition");
String user_id = bundle.getString("user_id");
byte[] data = bundle.getByteArray("book_image");
Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length);
book_image.setImageBitmap(bitmap);
ParseQuery<ParseObject> query = ParseQuery.getQuery("FbUser");
query.whereEqualTo("user_id",user_id);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if(e==null){
for (ParseObject object : objects){
ParseFile file = object.getParseFile("user_image");
file.getDataInBackground(new GetDataCallback() {
@Override
public void done(byte[] data, ParseException e) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length);
profile_user.setImageBitmap(bitmap);
}
});
}
}
}
});
//byte[] data = bundle.getByteArray("book_image");
//Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length);
//book_image.setImageBitmap(bitmap);
price.setText("₹" + priceBook);
price.setTextSize(20);
condition.setText(conditionBook);
condition.setTextSize(15);
return view;
}
}
您只需要在设置viewpager的适配器和设置SetOffScreenPageLimit
之后放置viewpager.SetCurrentItem(pos);
行。
这是因为在设置了当前项之后,您正在设置适配器,这将导致擦除先前设置的当前项
我想在屏幕中的页面预览之前显示viewpager。前一页和下一页在屏幕中显示深度,并用深度动画滑动下一页。 你可以看看这个图像 我怎么做?
你好,我有一个带有slidingtab的活动,一个viewpager,在viewpager中,我以编程方式添加了Listviews 问题是,如果showMenu为true,则上下文菜单不会显示,但如果showMenu为false,则其他函数工作。因此“OnItemClickListener”似乎没有问题,因为它被正确调用了,但问题是“ShowContextMenuForChild”。 我做错了什么
在我的应用程序中,我有一个ViewPager,里面有很多可滑动的标签,里面有碎片。是否有像这样的方法,每次片段出现在屏幕上时都会调用它?等是在创建片段之后调用的,而不是在片段出现在屏幕上时调用的,因此它们对我不起作用。我的问题可以用哪种方法?
我正在编写一个使用嵌套ViewPager的应用程序。每个父ViewPager包含7个子ViewPager。 对于第一个父视图,它可以正常工作,但当它到达下一页时,它在子视图页面的位置显示为空白。 如果使用嵌套视图寻呼机是可行的,这里有什么建议吗?如果是的话,我怎么能避免这个案子呢? 父ViewPager布局与子片段相同: 这是我在父ViewPager片段中实现的子ViewPager适配器:
我有两个<代码> 当用户从第一个选择框中选择一个值时,我希望第二个选择框只显示连接的值。 我的代码: 因此,当用户从第一个选择框M中选择“test1”时,他在第二个选择框上只会看到“test2”、“test3”和“test4”;第一个框中的“test2”将在第二个框中显示“test6”、“test7”和“test8”。 如何使用JavaScript解决此问题?
本文向大家介绍Android中ViewPager获取当前显示的Fragment,包括了Android中ViewPager获取当前显示的Fragment的使用技巧和注意事项,需要的朋友参考一下 前言 在项目中,有时会用到在ViewPager中显示同样类型的Fragment,同时这样的Fragment的个数是动态的,但是PagerAdapter没有给我们提供getCurrentFragment类似的方