Android通過手勢實現(xiàn)答題器翻頁效果
本文實例為大家分享了Android答題器翻頁功能,主要使用ViewFilpper和GestureDetector來實現(xiàn),供大家參考,具體內(nèi)容如下
1.效果圖

2.實現(xiàn)思路
把Activity的TouchEvent事件交個GestureDetector來處理,然后使用ViewFilpper使用動畫控制多個組件的之間的切換效果。手勢的一個Api就不詳細說了,大家如果不了解可以查一下。
3.實現(xiàn)的步驟
1)、構(gòu)建手勢檢測器
2)、準備數(shù)據(jù)
3)、為ViewFilpper添加子控件。
4)、初始化Animation數(shù)組
5)、把Activity的TouchEvent事件交個GestureDetector來處理
6)、實現(xiàn) onFling方法
4.代碼實現(xiàn)
4.1布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.lidong.demo.view.GestureFilpActivity">
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewFlipper"/>
</LinearLayout>
4.2 動畫文件
left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0"
android:duration="500" />
</set>
left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p"
android:duration="500" />
</set>
right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0"
android:duration="500" />
</set>
right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="100%p"
android:duration="500" />
</set>
4.3GestureFilpActivity的實現(xiàn)
package com.lidong.demo.view;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import com.lidong.demo.AppComponent;
import com.lidong.demo.BaseActivity;
import com.lidong.demo.R;
import com.lidong.demo.view.adapter.ChineseMedicineReportAdapter;
import com.lidong.demo.view.model.Question;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
/**
*@類名 : GestureFilpActivity
*@描述 :
*@時間 : 2016/5/3 16:11
*@作者: 李東
*@郵箱 : lidong@chni.com.cn
*@company: chni
*/
public class GestureFilpActivity extends BaseActivity implements GestureDetector.OnGestureListener{
@Bind(R.id.viewFlipper)
ViewFlipper mViewFlipper;
//1.定義手勢檢測器對象
GestureDetector mGestureDetector;
//2.定義一個動畫數(shù)組,用于為ViewFilpper指定切換動畫效果。
Animation[] animations = new Animation[4];
//3.定義手勢兩點之間的最小距離
final int FLIP_DISTANCE = 50 ;
List<Question> mQuestion = new ArrayList<>();
ChineseMedicineReportAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gesture_filp);
ButterKnife.bind(this);
setActivityTitle("答題器的實現(xiàn)");
//1.構(gòu)建手勢檢測器
mGestureDetector = new GestureDetector(this,this);
//2準備數(shù)據(jù)
List<Question> questions = initData();
mQuestion.addAll(questions);
//3.為ViewFilpper添加子控件。
for (int i = 0;i<mQuestion.size();i++){
Question question = mQuestion.get(i);
mViewFlipper.addView(addQuestionView(question));
}
//4.初始化Animation數(shù)組
animations[0] = AnimationUtils.loadAnimation(this,R.anim.left_in);
animations[1] = AnimationUtils.loadAnimation(this,R.anim.left_out);
animations[2] = AnimationUtils.loadAnimation(this,R.anim.right_in);
animations[3] = AnimationUtils.loadAnimation(this,R.anim.right_out);
}
@Override
protected void setupActivityComponent(AppComponent appComponent) {
}
private View addQuestionView(Question question){
View view = View.inflate(this, R.layout.activity_chnihealthreport, null);
TextView tes = (TextView) view.findViewById(R.id.tv_question);
ListView listview = (ListView) view.findViewById(R.id.lv_question_answer);
adapter = new ChineseMedicineReportAdapter(this,question);
listview.setAdapter(adapter);
tes.setText(question.getQuestion());
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(GestureFilpActivity.this,position+"",Toast.LENGTH_SHORT).show();
Toast.makeText(GestureFilpActivity.this,position+"",Toast.LENGTH_SHORT).show();
if (mViewFlipper.getDisplayedChild() == mQuestion.size() - 1) {
Toast.makeText(GestureFilpActivity.this,"最后一個題",Toast.LENGTH_SHORT).show();
mViewFlipper.stopFlipping();
return;
}else {
mViewFlipper.setInAnimation(animations[0]);
mViewFlipper.setOutAnimation(animations[1]);
mViewFlipper.showNext();
}
}
});
return view;
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
//重點實現(xiàn)在這里切換
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e2.getX() - e1.getX()>FLIP_DISTANCE){
if (mViewFlipper.getDisplayedChild() == 0) {
mViewFlipper.stopFlipping();
Toast.makeText(GestureFilpActivity.this,"第一個題",Toast.LENGTH_SHORT).show();
return false;
} else {
mViewFlipper.setInAnimation(animations[2]);
mViewFlipper.setOutAnimation(animations[3]);
mViewFlipper.showPrevious();
return true;
}
}else if (e1.getX() - e2.getX()>FLIP_DISTANCE){
if (mViewFlipper.getDisplayedChild() == mQuestion.size() - 1) {
Toast.makeText(GestureFilpActivity.this,"最后一個題",Toast.LENGTH_SHORT).show();
mViewFlipper.stopFlipping();
return false;
}else {
mViewFlipper.setInAnimation(animations[0]);
mViewFlipper.setOutAnimation(animations[1]);
mViewFlipper.showNext();
return true;
}
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//將Activity上的觸發(fā)的事件交個GestureDetector處理
return this.mGestureDetector.onTouchEvent(event);
}
private List<Question> initData(){
List<Question> questions = new ArrayList<>();
Question q1 = new Question();
q1.setQuestion("1、\"紅娘\"由來是出自下列哪部古典名劇:");
List<Question.Answer> mA = new ArrayList<>();
Question.Answer a1 = new Question.Answer();
a1.setAnswerMessage("A《琵琶記》");
Question.Answer a2 = new Question.Answer();
a2.setAnswerMessage("B《西廂記》");
Question.Answer a3 = new Question.Answer();
a3.setAnswerMessage("C《長生殿》");
Question.Answer a4 = new Question.Answer();
a4.setAnswerMessage("D《桃花扇》");
mA.add(a1);
mA.add(a2);
mA.add(a3);
mA.add(a4);
q1.setAnswer(mA);
questions.add(q1);
Question q2 = new Question();
q2.setQuestion("2.我國第一部有聲影片是:");
List<Question.Answer> mB = new ArrayList<>();
Question.Answer b1 = new Question.Answer();
b1.setAnswerMessage("A《歌女紅牡丹》");
Question.Answer b2 = new Question.Answer();
b2.setAnswerMessage("B《定軍山》");
Question.Answer b3 = new Question.Answer();
b3.setAnswerMessage("C《林則徐》");
Question.Answer b4 = new Question.Answer();
b4.setAnswerMessage("D《玉人何處》");
mB.add(b1);
mB.add(b2);
mB.add(b3);
mB.add(b4);
q2.setAnswer(mB);
questions.add(q2);
Question q3= new Question();
q3.setQuestion("3.下列哪座山不屬于我國四大佛山之一:( A)");
List<Question.Answer> mC = new ArrayList<>();
Question.Answer c1 = new Question.Answer();
c1.setAnswerMessage("A《歌女紅牡丹》");
Question.Answer c2 = new Question.Answer();
c2.setAnswerMessage("B《定軍山》");
Question.Answer c3 = new Question.Answer();
c3.setAnswerMessage("C《林則徐》");
Question.Answer c4 = new Question.Answer();
c4.setAnswerMessage("D《玉人何處》");
mC.add(c1);
mC.add(c2);
mC.add(c3);
mC.add(c4);
q3.setAnswer(mC);
questions.add(q3);
Question q4 = new Question();
q4.setQuestion("4.下面哪個是對“驚蟄”這個節(jié)氣的正確描述?");
List<Question.Answer> mD = new ArrayList<>();
Question.Answer d1 = new Question.Answer();
d1.setAnswerMessage("A《歌女紅牡丹》");
Question.Answer d2 = new Question.Answer();
d2.setAnswerMessage("B《定軍山》");
Question.Answer d3 = new Question.Answer();
d3.setAnswerMessage("C《林則徐》");
Question.Answer d4 = new Question.Answer();
d4.setAnswerMessage("D《玉人何處》");
mD.add(d1);
mD.add(d2);
mD.add(d3);
mD.add(d4);
q4.setAnswer(mD);
questions.add(q4);
return questions;
}
}
5.總結(jié)
1.構(gòu)建手勢檢測器,2準備數(shù)據(jù),3為ViewFilpper添加子控件。4.初始化Animation數(shù)組。5.把Activity的TouchEvent事件交個GestureDetector來處理,6.實現(xiàn)onFling方法。
以上就是本文的全部內(nèi)容,希望對大家學習Android軟件編程有所幫助。
- Android 中使用RecyclerView實現(xiàn)底部翻頁
- android中圖片翻頁效果簡單的實現(xiàn)方法
- 解析Android中實現(xiàn)滑動翻頁之ViewFlipper的使用詳解
- Android實現(xiàn)閱讀APP平移翻頁效果
- android ViewPager實現(xiàn)滑動翻頁效果實例代碼
- Android自定義左右或上下滑動翻頁效果
- 基于Android實現(xiàn)3D翻頁效果
- Android?ViewPager實現(xiàn)左右滑動翻頁效果
- Android利用懸浮按鈕實現(xiàn)翻頁效果
- Android 仿日歷翻頁、仿htc時鐘翻頁、數(shù)字翻頁切換效果
- RecyclerView+PagerSnapHelper實現(xiàn)抖音首頁翻頁的Viewpager效果
相關文章
Android實現(xiàn)計算器(計算表達式/計算小數(shù)點以及括號)
這篇文章主要為大家詳細介紹了Android實現(xiàn)計算器功能,計算表達式,能計算小數(shù)點以及括號,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-09-09
Android持久化技術之SharedPreferences存儲實例詳解
這篇文章主要介紹了Android持久化技術之SharedPreferences存儲,結(jié)合實例形式較為詳細的分析了SharedPreferences存儲的原理、應用及具體實現(xiàn)方法,需要的朋友可以參考下2016-01-01
Android實現(xiàn)網(wǎng)絡加載時的對話框功能
這篇文章主要介紹了Android實現(xiàn)網(wǎng)絡加載時的對話框功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02
Android onClick方法與setOnClickListener方法對比
這篇文章主要介紹了Android onClick方法與setOnClickListener方法對比的相關資料,這兩個方法都是點擊事件處理函數(shù)的方法,它們之間到底有什么區(qū)別呢,下面就給大家說下,需要的朋友可以參考下2016-12-12

