我想构建三个视图的圆圈,在每个视图上按两个按钮来显示和下一个查看由它们连接的所有三个视图。 我为每个View设置了三个布局,并且三个CustomView随布局相应地膨胀。 最重要的是一个包含ViewFlipper的Activity。
我的问题是:
1.我可以用viewFlipper在它们之间切换吗? 使用myFlipper.addView(myView)或者我应该在viewFlipper布局中使用标签吗?
2.如何使用相关布局来扩展我的视图
我已经尝试LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.t1, null);
我的活动看起来像这样
public class Activity1 extends Activity {
private ViewFlipper flipper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_fliper_layout);
flipper = (ViewFlipper) findViewById(R.id.flipper);
myView1 temp1= new myView1 (this);
flipper.addView(myView1 , 1);
flipper.setDisplayedChild(1);
}
}
答案 0 :(得分:4)
ViewFlipper flipper = (ViewFlipper) findViewById(R.id.flipper);
//Inflate the Views
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v1 = inflater.inflate(R.layout.t1, null);
View v2 = inflater.inflate(R.layout.t2, null);
View v3 = inflater.inflate(R.layout.t3, null);
//Add the views to the flipper
viewFlipper.addView(v1);
viewFlipper.addView(v2);
viewFlipper.addView(v3);
//Move between them
flipper.showNext();
flipper.showPrevious();
http://developer.android.com/reference/android/widget/ViewAnimator.html#showNext()
答案 1 :(得分:2)
动态地在viewflipper中添加child。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.walk_around_the_store_question, container, false);
this.initViewControls(view);
// Set Questions
this.setViewFlipper(inflater);
return view;
}
private void initViewControls(View view)
{
// getting name of current student.
String studentName = ApplicationSharedPreference.getInstance(
WalkAroundTheStoreQuestioneryFragment.this.getActivity()).getStringValue(
ApplicationConstants.WALK_AROUND_THE_STORE_CURRENT_STUDENT_NAME);
this.btnSubmitData = (Button) view.findViewById(R.id.btnSubmitData);
this.btnSubmitData.setOnClickListener(this.btnClickListener);
this.txtTitleName = (TextView) view.findViewById(R.id.txtTitleName);
this.txtTitleName.setText(studentName);
this.viewFlipper = (ViewFlipper) view.findViewById(R.id.viewflipper);
this.imgPrevious = (ImageView) view.findViewById(R.id.imgPrevious);
this.imgNext = (ImageView) view.findViewById(R.id.imgNext);
this.imgPrevious.setOnClickListener(this.btnClickListener);
this.imgNext.setOnClickListener(this.btnClickListener);
if (0 == WalkAroundTheStoreQuestioneryFragment.this.viewFlipper.getDisplayedChild())
{
WalkAroundTheStoreQuestioneryFragment.this.imgPrevious.setVisibility(View.INVISIBLE);
}
}
/**
* Button Click event.
*/
private OnClickListener btnClickListener = new View.OnClickListener()
{
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.imgPrevious:
WalkAroundTheStoreQuestioneryFragment.this.imgNext.setVisibility(View.VISIBLE);
// if viewflipper first child then hide previous button
if (1 == WalkAroundTheStoreQuestioneryFragment.this.viewFlipper.getDisplayedChild())
{
WalkAroundTheStoreQuestioneryFragment.this.imgPrevious.setVisibility(View.INVISIBLE);
}
// set animations for previous question move
WalkAroundTheStoreQuestioneryFragment.this.viewFlipper.setInAnimation(SliderAnimation
.inFromLeftAnimation());
WalkAroundTheStoreQuestioneryFragment.this.viewFlipper.setOutAnimation(SliderAnimation
.outToRightAnimation());
WalkAroundTheStoreQuestioneryFragment.this.viewFlipper.showPrevious();
break;
case R.id.imgNext:
WalkAroundTheStoreQuestioneryFragment.this.imgPrevious.setVisibility(View.VISIBLE);
// if viewflipper last child then hide next button.
if (WalkAroundTheStoreQuestioneryFragment.this.viewFlipper.getChildCount() == WalkAroundTheStoreQuestioneryFragment.this.viewFlipper
.getDisplayedChild())
{
WalkAroundTheStoreQuestioneryFragment.this.imgNext.setVisibility(View.INVISIBLE);
}
// set animations for next question move
WalkAroundTheStoreQuestioneryFragment.this.viewFlipper.setInAnimation(SliderAnimation
.inFromRightAnimation());
WalkAroundTheStoreQuestioneryFragment.this.viewFlipper.setOutAnimation(SliderAnimation
.outToLeftAnimation());
WalkAroundTheStoreQuestioneryFragment.this.viewFlipper.showNext();
break;
case R.id.btnSubmitData:
Toast.makeText(WalkAroundTheStoreQuestioneryFragment.this.getActivity(), "btnSubmitData",
Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
};
/**
* @param inflater
*
*/
private void setViewFlipper(LayoutInflater inflater)
{
for (int i = 1; i < 10; i++)
{
View view1 = inflater.inflate(R.layout.walk_around_store_quetions_inflate, null);
this.viewFlipper.addView(view1);
this.txtQuestions = (TextView) view1.findViewById(R.id.txtQuestions);
this.txtQuestions.setText("First Question :" + i);
this.txtQuestionsSub1 = (TextView) view1.findViewById(R.id.txtQuestionsSub1);
this.txtQuestionsSub1.setText("SubPoint :" + i + ".1");
this.spnQuestionsSub1 = (Spinner) view1.findViewById(R.id.spnQuestionsSub1);
this.txtQuestionsSub2 = (TextView) view1.findViewById(R.id.txtQuestionsSub2);
this.txtQuestionsSub2.setText("SubPoint :" + i + ".2");
this.spnQuestionsSub2 = (Spinner) view1.findViewById(R.id.spnQuestionsSub2);
this.txtQuestionsSub3 = (TextView) view1.findViewById(R.id.txtQuestionsSub3);
this.txtQuestionsSub3.setText("SubPoint :" + i + ".3");
this.spnQuestionsSub3 = (Spinner) view1.findViewById(R.id.spnQuestionsSub3);
this.txtQuestionsSub4 = (TextView) view1.findViewById(R.id.txtQuestionsSub4);
this.txtQuestionsSub4.setText("SubPoint :" + i + ".4");
this.spnQuestionsSub4 = (Spinner) view1.findViewById(R.id.spnQuestionsSub4);
}
}
答案 2 :(得分:0)
这可能会对你有帮助。
ViewFlipper viewFlipper= (ViewFlipper) findViewById(R.id.flipper);
View v1 = LayoutInflater.from(mContext).inflate(R.layout.layout_1, null, false);
View v2 = LayoutInflater.from(mContext).inflate(R.layout.layout_2, null, false);
View v3 = LayoutInflater.from(mContext).inflate(R.layout.layout_3, null, false);
View v4 = LayoutInflater.from(mContext).inflate(R.layout.layout_4, null, false);
viewFlipper.addView(v1);
viewFlipper.addView(v2);
viewFlipper.addView(v3);
viewFlipper.addView(v4);
viewFlipper.showNext();
viewFlipper.showPrevious();