我正在执行单击当前按钮以更改其背景颜色,它正在更改下一页按钮背景 基本上我想要当用户点击问题的4个选项中的任何一个是否正确时将其改为绿色否则为红色但是它正在改变下一页按钮的背景 即使我点击选项1并显示吐司以获取该选项的文本,它也会显示下一页的文本option1 我该怎么做才能帮到我。
package com.app.apti.adapter;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.PagerAdapter;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.app.apti.R;
import com.app.apti.model.QuestionData;
import java.util.ArrayList;
public class QuestionViewPager extends PagerAdapter {
LinearLayout option1color,option2color,option3color,option4color;
TextView option1,option2,option3,option4,ques,questionno;
private Context context;
ArrayList<QuestionData> question;
LayoutInflater layoutInflater;
ImageView imageView;
String category;
public QuestionViewPager(Context context, ArrayList<QuestionData>
question,String category)
{
this.context=context;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.question=question;
this.category=category;
}
@Override
public int getCount() {
if(question != null){
return question.size();
}
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
View itemView = layoutInflater.inflate(R.layout.questionlayout, container, false);
option1color=(LinearLayout)itemView.findViewWithId(R.id.color_option1);
option2color=(LinearLayout)itemView.findViewById(R.id.color_option2);
option3color=(LinearLayout)itemView.findViewById(R.id.color_option3);
option4color=(LinearLayout)itemView.findViewById(R.id.color_option4);
option1=(TextView)itemView.findViewById(R.id.option1);
option2=(TextView)itemView.findViewById(R.id.option2);
option3=(TextView)itemView.findViewById(R.id.option3);
option4=(TextView)itemView.findViewById(R.id.option4);
ques=(TextView)itemView.findViewById(R.id.ques);
questionno=(TextView)itemView.findViewById(R.id.questionno);
questionno.setText(position+1+"/"+getCount());
ques.setText(Html.fromHtml(question.get(position).getQues()));
option1.setText(Html.fromHtml(question.get(position).getOpt1()));
option2.setText(Html.fromHtml(question.get(position).getOpt2()));
option3.setText(Html.fromHtml(question.get(position).getOpt3()));
option4.setText(Html.fromHtml(question.get(position).getOpt4()));
option1color.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
notifyDataSetChanged();
Toast.makeText(context,option1.getText().toString(),Toast.LENGTH_SHORT).show();
if(question.get(position).getAns().equalsIgnoreCase(option1.getText().toString()))
{
option1color.setBackgroundResource(R.drawable.round_corner_green);
}else
{
option1color.setBackgroundResource(R.drawable.round_corner_red);
}
}
});
option2color.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(question.get(position).getAns().equalsIgnoreCase(option2.getText().toString()))
{
option2color.setBackgroundResource(R.drawable.round_corner_green);
}else
{
option2color.setBackgroundResource(R.drawable.round_corner_red);
}
}
});
option3color.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(question.get(position).getAns().equalsIgnoreCase(option3.getText().toString()))
{
option3color.setBackgroundResource(R.drawable.round_corner_green);
}else
{
option3color.setBackgroundResource(R.drawable.round_corner_red);
}
}
});
option4color.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(question.get(position).getAns().equalsIgnoreCase(option4.getText().toString()))
{
option4color.setBackgroundResource(R.drawable.round_corner_green);
}else
{
option4color.setBackgroundResource(R.drawable.round_corner_red);
}
}
});
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
答案 0 :(得分:0)
是的,您可以使用界面。每当用户为任何问题给出答案时,您可以使用特定寻呼机活动或片段上的接口实现来创建回调表单适配器类。基于该回调,您可以触摸视图并根据问题的答案更改背景是对还是错。
快乐的编码!!如有任何疑问,请告诉我。
答案 1 :(得分:0)
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
updatePage(i); //a dummy method to do
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
private void updateFab(int selectedPage) {
switch (selectedPage) {
case MainPagerAdapter.POS1:
showToast();
break;
case MainPagerAdapter.POS2:
hideToast();
break;
case MainPagerAdapter.POS3:
showToast();
default:
break;
}
}
其中,MainPagerAdapter扩展了FragPagerADapter和
@Override
public Fragment getItem(int position) {
switch (position) {
case POS1:
return FragOne.newInstance();
case POS2:
return FragTwo.newInstance();
case POS2:
return FragThree.newInstance();
default:
return null;
}
}
答案 2 :(得分:0)
发生这种情况是因为
LinearLayout option1color,option2color,option3color,option4color;
TextView option1,option2,option3,option4,ques,questionno;
是字段,它们的值将设置为最后一次调用instantiateItem
中设置的最后一个值。因此,当调用on click回调时,它们只获取最后指定的值。只需在instantiateItem
方法体内创建局部变量,你应该没问题。
完整代码:
package com.app.apti.adapter;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.PagerAdapter;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.app.apti.R;
import com.app.apti.model.QuestionData;
import java.util.ArrayList;
public class QuestionViewPager extends PagerAdapter {
private Context context;
ArrayList<QuestionData> question;
LayoutInflater layoutInflater;
ImageView imageView;
String category;
public QuestionViewPager(Context context, ArrayList<QuestionData>
question,String category)
{
this.context=context;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.question=question;
this.category=category;
}
@Override
public int getCount() {
if(question != null){
return question.size();
}
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
/* ALL LOCAL VARIABLES */
LinearLayout option1color,option2color,option3color,option4color;
TextView option1,option2,option3,option4,ques,questionno;
View itemView = layoutInflater.inflate(R.layout.questionlayout, container, false);
option1color=(LinearLayout)itemView.findViewWithTag(R.id.color_option1);
option2color=(LinearLayout)itemView.findViewById(R.id.color_option2);
option3color=(LinearLayout)itemView.findViewById(R.id.color_option3);
option4color=(LinearLayout)itemView.findViewById(R.id.color_option4);
option1=(TextView)itemView.findViewById(R.id.option1);
option2=(TextView)itemView.findViewById(R.id.option2);
option3=(TextView)itemView.findViewById(R.id.option3);
option4=(TextView)itemView.findViewById(R.id.option4);
ques=(TextView)itemView.findViewById(R.id.ques);
questionno=(TextView)itemView.findViewById(R.id.questionno);
questionno.setText(position+1+"/"+getCount());
ques.setText(Html.fromHtml(question.get(position).getQues()));
option1.setText(Html.fromHtml(question.get(position).getOpt1()));
option2.setText(Html.fromHtml(question.get(position).getOpt2()));
option3.setText(Html.fromHtml(question.get(position).getOpt3()));
option4.setText(Html.fromHtml(question.get(position).getOpt4()));
option1color.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,option1.getText().toString(),Toast.LENGTH_SHORT).show();
if(question.get(position).getAns().equalsIgnoreCase(option1.getText().toString()))
{
option1color.setBackgroundResource(R.drawable.round_corner_green);
}else
{
option1color.setBackgroundResource(R.drawable.round_corner_red);
}
}
});
option2color.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(question.get(position).getAns().equalsIgnoreCase(option2.getText().toString()))
{
option2color.setBackgroundResource(R.drawable.round_corner_green);
}else
{
option2color.setBackgroundResource(R.drawable.round_corner_red);
}
}
});
option3color.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(question.get(position).getAns().equalsIgnoreCase(option3.getText().toString()))
{
option3color.setBackgroundResource(R.drawable.round_corner_green);
}else
{
option3color.setBackgroundResource(R.drawable.round_corner_red);
}
}
});
option4color.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(question.get(position).getAns().equalsIgnoreCase(option4.getText().toString()))
{
option4color.setBackgroundResource(R.drawable.round_corner_green);
}else
{
option4color.setBackgroundResource(R.drawable.round_corner_red);
}
}
});
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}