Android 按降序选择选项卡会与ViewPager上的错误片段交互
我是一名编程新手,目前正在开发一款包含20个问题的基本四选项测验应用程序。当您选择正确答案时,它会将背景色设置为绿色,否则为红色。我的问题是,当我以升序选择tablayout(或滑动)上的问题时,一切正常(例如1到7到15到20)。但是,当我按降序选择问题时(首先单击tablayout上的20,然后单击15),它标志着下一个问题。当我选择了一个在offScreenPageLimit之外的问题并开始以递增的顺序滑动时,它会自动更正 您可以从这里观看案例: 我怎样才能解决这个问题 我的viewpager活动:Android 按降序选择选项卡会与ViewPager上的错误片段交互,android,android-fragments,Android,Android Fragments,我是一名编程新手,目前正在开发一款包含20个问题的基本四选项测验应用程序。当您选择正确答案时,它会将背景色设置为绿色,否则为红色。我的问题是,当我以升序选择tablayout(或滑动)上的问题时,一切正常(例如1到7到15到20)。但是,当我按降序选择问题时(首先单击tablayout上的20,然后单击15),它标志着下一个问题。当我选择了一个在offScreenPageLimit之外的问题并开始以递增的顺序滑动时,它会自动更正 您可以从这里观看案例: 我怎样才能解决这个问题 我的viewpa
class ViewPagerActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_view_pager)
viewPager = findViewById<ViewPager2>(R.id.view_pager)
viewPager.setPageTransformer(DepthPageTransformer())
tabLayout = findViewById(R.id.tabs)
viewPager.adapter = createCardAdapter()
TabLayoutMediator(tabLayout, viewPager
) { tab, position ->
tab.text = " " + (position + 1)
}.attach()
})
}
private fun createCardAdapter(): ViewPagerAdapter {
return ViewPagerAdapter(this)
}
override fun onBackPressed() {
Toast.makeText(this,"Pressed Back",Toast.LENGTH_SHORT).show()
} }
我的片段:
class CardFragment : Fragment() {
private var counter: Int? = null
var answerq =""
lateinit var opA : Button
lateinit var opB : Button
lateinit var opC : Button
lateinit var opD : Button
private val COLOR_MAP = intArrayOf(
R.color.red_100, R.color.red_300, R.color.red_500, R.color.red_700, R.color.blue_100,
R.color.blue_300, R.color.blue_500, R.color.blue_700, R.color.green_100, R.color.green_300,
R.color.green_500, R.color.green_700
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (arguments != null) {
counter = requireArguments().getInt(ARG_COUNT)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_four_options, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val colorNumber = counter!! % 10
view.setBackgroundColor(ContextCompat.getColor(requireContext(), COLOR_MAP[colorNumber]))
val tabPos = tabLayout.selectedTabPosition
val textDort = view.findViewById<TextView>(R.id.textFour)
opA=view.findViewById<Button>(R.id.button41)
opB=view.findViewById<Button>(R.id.button42)
opC=view.findViewById<Button>(R.id.button43)
opD=view.findViewById<Button>(R.id.button44)
textDort.text = questionFullDataList[counter!!].text
if (textDort.text != questionFullDataList[tabPos].text){
println("Wrong place")
}
opA.text = questionFullDataList[counter!!].a
opB.text = questionFullDataList[counter!!].b
opC.text = questionFullDataList[counter!!].c
opD.text = questionFullDataList[counter!!].d
answerq = questionFullDataList[counter!!].answer
if (allInfoArray[counter!!].secondClick == 1){
val thisQuestion = allInfoArray[counter!!]
val userOption = thisQuestion.buttonId
val correctOption = thisQuestion.dogruSecenekId
when (userOption){
opA.id -> opA.setBackgroundColor(resources.getColor(R.color.red_100))
opB.id -> opB.setBackgroundColor(resources.getColor(R.color.red_100))
opC.id -> opC.setBackgroundColor(resources.getColor(R.color.red_100))
opD.id -> opD.setBackgroundColor(resources.getColor(R.color.red_100))
}
when (correctOption){
opA.id -> opA.setBackgroundColor(resources.getColor(R.color.green_700))
opB.id -> opB.setBackgroundColor(resources.getColor(R.color.green_700))
opC.id -> opC.setBackgroundColor(resources.getColor(R.color.green_700))
opD.id -> opD.setBackgroundColor(resources.getColor(R.color.green_700))
}
}
opA.setOnClickListener {
buttonClick(it)
}
opB.setOnClickListener {
buttonClick(it)
}
opC.setOnClickListener {
buttonClick(it)
}
opD.setOnClickListener {
buttonClick(it)
}
}
private fun buttonClick(view: View){
val tabPos = tabLayout.selectedTabPosition
if (allInfoArray[tabPos].secondClick==0){
val b = view as Button
val buttonText = b.text.toString()
val buttonId = b.id
var dogruSecenek = 0
if (answerq == buttonText){
view.setBackgroundColor(resources.getColor(R.color.green_700))
} else {
view.setBackgroundColor(resources.getColor(R.color.red_100))
}
when (answerq) {
questionFullDataList[tabPos].a -> {
dogruSecenek = opA.id
opA.setBackgroundColor(resources.getColor(R.color.green_700))
}
questionFullDataList[tabPos].b -> {
dogruSecenek = opB.id
opB.setBackgroundColor(resources.getColor(R.color.green_700))
}
questionFullDataList[tabPos].c -> {
dogruSecenek = opC.id
opC.setBackgroundColor(resources.getColor(R.color.green_700))
}
questionFullDataList[tabPos].d -> {
dogruSecenek = opD.id
opD.setBackgroundColor(resources.getColor(R.color.green_700))
}
}
val newQuestionInfo = QuestionInfo(
tabPos.toString(),
buttonText,
buttonId,
dogruSecenek,
1
)
allInfoArray[tabPos]=newQuestionInfo
}
}
companion object {
private const val ARG_COUNT = "param1"
fun newInstance(counter: Int?): CardFragment {
val fragment = CardFragment()
val args = Bundle()
args.putInt(ARG_COUNT, counter!!)
fragment.arguments = args
return fragment
}
}
但却无法从那里找到解决办法
class CardFragment : Fragment() {
private var counter: Int? = null
var answerq =""
lateinit var opA : Button
lateinit var opB : Button
lateinit var opC : Button
lateinit var opD : Button
private val COLOR_MAP = intArrayOf(
R.color.red_100, R.color.red_300, R.color.red_500, R.color.red_700, R.color.blue_100,
R.color.blue_300, R.color.blue_500, R.color.blue_700, R.color.green_100, R.color.green_300,
R.color.green_500, R.color.green_700
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (arguments != null) {
counter = requireArguments().getInt(ARG_COUNT)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_four_options, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val colorNumber = counter!! % 10
view.setBackgroundColor(ContextCompat.getColor(requireContext(), COLOR_MAP[colorNumber]))
val tabPos = tabLayout.selectedTabPosition
val textDort = view.findViewById<TextView>(R.id.textFour)
opA=view.findViewById<Button>(R.id.button41)
opB=view.findViewById<Button>(R.id.button42)
opC=view.findViewById<Button>(R.id.button43)
opD=view.findViewById<Button>(R.id.button44)
textDort.text = questionFullDataList[counter!!].text
if (textDort.text != questionFullDataList[tabPos].text){
println("Wrong place")
}
opA.text = questionFullDataList[counter!!].a
opB.text = questionFullDataList[counter!!].b
opC.text = questionFullDataList[counter!!].c
opD.text = questionFullDataList[counter!!].d
answerq = questionFullDataList[counter!!].answer
if (allInfoArray[counter!!].secondClick == 1){
val thisQuestion = allInfoArray[counter!!]
val userOption = thisQuestion.buttonId
val correctOption = thisQuestion.dogruSecenekId
when (userOption){
opA.id -> opA.setBackgroundColor(resources.getColor(R.color.red_100))
opB.id -> opB.setBackgroundColor(resources.getColor(R.color.red_100))
opC.id -> opC.setBackgroundColor(resources.getColor(R.color.red_100))
opD.id -> opD.setBackgroundColor(resources.getColor(R.color.red_100))
}
when (correctOption){
opA.id -> opA.setBackgroundColor(resources.getColor(R.color.green_700))
opB.id -> opB.setBackgroundColor(resources.getColor(R.color.green_700))
opC.id -> opC.setBackgroundColor(resources.getColor(R.color.green_700))
opD.id -> opD.setBackgroundColor(resources.getColor(R.color.green_700))
}
}
opA.setOnClickListener {
buttonClick(it)
}
opB.setOnClickListener {
buttonClick(it)
}
opC.setOnClickListener {
buttonClick(it)
}
opD.setOnClickListener {
buttonClick(it)
}
}
private fun buttonClick(view: View){
val tabPos = tabLayout.selectedTabPosition
if (allInfoArray[tabPos].secondClick==0){
val b = view as Button
val buttonText = b.text.toString()
val buttonId = b.id
var dogruSecenek = 0
if (answerq == buttonText){
view.setBackgroundColor(resources.getColor(R.color.green_700))
} else {
view.setBackgroundColor(resources.getColor(R.color.red_100))
}
when (answerq) {
questionFullDataList[tabPos].a -> {
dogruSecenek = opA.id
opA.setBackgroundColor(resources.getColor(R.color.green_700))
}
questionFullDataList[tabPos].b -> {
dogruSecenek = opB.id
opB.setBackgroundColor(resources.getColor(R.color.green_700))
}
questionFullDataList[tabPos].c -> {
dogruSecenek = opC.id
opC.setBackgroundColor(resources.getColor(R.color.green_700))
}
questionFullDataList[tabPos].d -> {
dogruSecenek = opD.id
opD.setBackgroundColor(resources.getColor(R.color.green_700))
}
}
val newQuestionInfo = QuestionInfo(
tabPos.toString(),
buttonText,
buttonId,
dogruSecenek,
1
)
allInfoArray[tabPos]=newQuestionInfo
}
}
companion object {
private const val ARG_COUNT = "param1"
fun newInstance(counter: Int?): CardFragment {
val fragment = CardFragment()
val args = Bundle()
args.putInt(ARG_COUNT, counter!!)
fragment.arguments = args
return fragment
}
}
if (textDort.text != questionFullDataList[tabPos].text){
println("Wrong place")}