如何更新viewpager的当前视图?
How to update current view of viewpager?
我正在执行单击当前按钮以更改其背景颜色并且它正在更改下一页按钮背景
基本上我想当用户点击问题的 4 个选项中的任何一个时,如果它是正确的,则将其更改为绿色,否则为红色,但它正在更改下一页按钮的背景
即使我点击选项 1 并显示祝酒词以获取该选项的文本,它也会显示下一页选项 1 的文本
请问我该怎么办
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);
}
}
是的,您可以使用界面。每当用户回答任何问题时,您都可以使用特定寻呼机 activity 或片段上的接口实现创建回调表单适配器 class。根据该回调,您可以触摸视图并根据问题的答案更改背景。
编码愉快!!如果有任何疑问,请告诉我。
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;
}
}
发生这种情况是因为
LinearLayout option1color,option2color,option3color,option4color;
TextView option1,option2,option3,option4,ques,questionno;
是字段,它们的值将设置为上次调用 instantiateItem
时设置的最后值。因此,当调用点击回调时,他们只会获得最后分配的值。只需将它们设为 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);
}
}
我正在执行单击当前按钮以更改其背景颜色并且它正在更改下一页按钮背景 基本上我想当用户点击问题的 4 个选项中的任何一个时,如果它是正确的,则将其更改为绿色,否则为红色,但它正在更改下一页按钮的背景 即使我点击选项 1 并显示祝酒词以获取该选项的文本,它也会显示下一页选项 1 的文本 请问我该怎么办
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);
}
}
是的,您可以使用界面。每当用户回答任何问题时,您都可以使用特定寻呼机 activity 或片段上的接口实现创建回调表单适配器 class。根据该回调,您可以触摸视图并根据问题的答案更改背景。
编码愉快!!如果有任何疑问,请告诉我。
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;
}
}
发生这种情况是因为
LinearLayout option1color,option2color,option3color,option4color;
TextView option1,option2,option3,option4,ques,questionno;
是字段,它们的值将设置为上次调用 instantiateItem
时设置的最后值。因此,当调用点击回调时,他们只会获得最后分配的值。只需将它们设为 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);
}
}