【问题标题】:view pager is swiping same image again and again查看寻呼机一次又一次地刷相同的图像
【发布时间】:2017-01-23 17:56:11
【问题描述】:

我一直在开发应用程序(基于问答)。我已经包含视图寻呼机来滑动图像,但它只是滑动相同的图像。

调用类的类代码(视图分页器包含在活动中)

package com.example.joshiyogesh.puzzle;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class ExerciseOneQuestionList extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercise_one_question_list);
    }

    public void questionOne(View view) {

        Intent intent = new Intent(getApplicationContext(),ExerciseOneQuestions.class);
        intent.putExtra("question_index",0);
        startActivity(intent);
    }
}

ExerciseOneQuestion.java

package com.example.joshiyogesh.puzzle;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class ExerciseOneQuestions extends AppCompatActivity {
    ViewPager viewPager;
    public int questionIndexReceive;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercise_one_questions);
        questionIndexReceive = getIntent().getIntExtra("question_index",1);
//        questionIndexReceive =Integer.parseInt(getIntent().getStringExtra("question_index"));
        viewPager = (ViewPager)findViewById(R.id.pager);
        PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
    }
}

寻呼机适配器类

package com.example.joshiyogesh.puzzle;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class PagerAdapter extends FragmentPagerAdapter {
    public PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        position = new ExerciseOneQuestions().questionIndexReceive;
        Fragments fragments = new Fragments();
        Bundle bundle = new Bundle();
        bundle.putInt("question_no",position);
        fragments.setArguments(bundle);
        return fragments;
    }

    @Override
    public int getCount() {
        return 2;
    }
}

片段类

public class Fragments extends Fragment {
    ImageView imageView;
    @Override
    public View onCreateView(LayoutInflater inflater , ViewGroup container ,
                             Bundle savedInstanceState){

        int [] image_index = {R.drawable.eightypageanswer1,R.drawable.eightypageanswer8};
        Bundle bundle = getArguments();
        int message = bundle.getInt("question_no");
        View rootView = inflater.inflate(R.layout.fragments,container,false);
        imageView = (ImageView) rootView.findViewById(R.id.imageQuestions);
        imageView.setImageResource(image_index[message]);
        return rootView;
    }
}

出于测试目的,我只考虑了两个整数图像,但它总是滑动相同的图像,即第一张图像。

而不是在ExerciseOneQuestionList.java 中更改“question_index” 这里...

public void questionOne(View view) {

        Intent intent = new Intent(getApplicationContext(),ExerciseOneQuestions.class);
        intent.putExtra("question_index",0);
        startActivity(intent);
    }

不明白为什么它总是显示相同的图像?

【问题讨论】:

  • 你只得到第一张图片吗?我能知道你每次得到的消息 int 值吗?
  • @Nivedh 是的,我总是得到第一张图片,即索引 0
  • 移除位置=新的ExerciseOneQuestions().questionIndexReceive;这可能会解决您的问题

标签: java android android-fragments android-viewpager


【解决方案1】:

看来问题出在一行

position = new ExerciseOneQuestions().questionIndexReceive;

似乎位置总是设置在同一个数字上。

【讨论】:

  • 无法理解,如果 questionIndexReceive 已由我手动更改,那么如果该行出现问题,为什么它不会根据您更改位置
  • 如果还有其他方法,让我们告诉您。这里实际所做的是图像在片段内部发生变化。而且我使用了上面的行,因为当有很多图像时,如果用户想直接去(假设)第 10 张图像,那么可以直接完成,也可以通过滑动来完成
  • Var position 永远不应更改。如果您想在位置 x 处手动显示图像,您应该使用 pager.setCurrentItem(x) 转到寻呼机页面。我不确定我是否完全理解您想要做什么,但这肯定是错误的方式。
  • app 基于图像和答案。 Activity中写有数字,如果用户点击任意数字,就会直接显示图片,问题图片以整数形式存储,由片段访问,在片段中我们更改图片。当用户直接在特定阶段(问题图像)跳转时,他还可以在该位置滑动图像以查看下一个或上一个图像(如果有)
【解决方案2】:

我真的不知道你在用这个“新的 ExerciseOneQuestions().questionIndexReceive;”做什么线。但是以标准方式,您应该制作两个片段,然后将这些片段添加到您的适配器。其余的事情将由适配器完成,以下粗略的代码可能会对您有所帮助:

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    private final List<Fragment> mFragments = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public void addFragment(Fragment fragment, String title) {
        mFragments.add(fragment);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);    // Which Fragment should be dislpayed by the viewpager for the given position

    }

    @Override
    public int getCount() {
        return mFragments.size();
    }
}

通过调用 addFragment 方法然后 setadapter 使类中的两个片段对象将它们添加到适配器中。

【讨论】:

  • 我想知道实际上从 user 中选择了哪个位置(数字)。也不是只应用两个片段的情况。实际上我在做什么,我正在改变片段中的图像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 2013-05-24
  • 2018-08-20
  • 1970-01-01
相关资源
最近更新 更多