唯美
一、簡述
在項目開發中,有時候頁面會比較長,這時候我們自然而然就想到采用ScrollView進行滑動,但這里我遇到一個小坑,因為ScrollView本身就是上下滑動效果,而像EditView有時候可能需要多行輸入,那么這時候就會發現問題來了,EditView多行數據滑動不起效果,如下所示:
二、解決
明顯可以看到多行情況下EditView無法滑動,導致這個原因是ScrollView的滑動事件與EditView滑動事件沖突,解決方案是在EditView的觸摸事件中限制ScrollView不攔截
/**
* 設置觸摸事件,由于EditView與TextView都處于ScollView中,
* 所以需要在OnTouch事件中通知父控件不攔截子控件事件
*/
private OnTouchListener touchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN
|| event.getAction() == MotionEvent.ACTION_MOVE){
//按下或滑動時請求父節點不攔截子節點
v.getParent().requestDisallowInterceptTouchEvent(true);
}
if(event.getAction() == MotionEvent.ACTION_UP){
//抬起時請求父節點攔截子節點
v.getParent().requestDisallowInterceptTouchEvent(false);
}
return false;
}
};
然后再設置EditView對象調用該事件即可
etContent.setOnTouchListener(touchListener);
三、滑動TextView設置
布局中可設置textView滑動的ScrollBar,具體如下:
另外在代碼中還需要設置滑動調用的方法:
//textView滑動需要設置滑動方法
tvDefault.setMovementMethod(ScrollingMovementMethod.getInstance());
四、詳細代碼
布局文件:activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.anand.textviewdemo.activity.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/et_title" />
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/edit_text"
android:gravity="start|top"
android:hint="@string/et_hint"
android:inputType="textMultiLine"
android:maxLines="8"
android:minLines="8"
android:scrollbars="vertical"
android:scrollbarStyle="outsideOverlay" />
<Button
android:id="@+id/btn_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_ok"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/default_style" />
<TextView
android:id="@+id/tv_default"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/tvbar_bg"
android:maxLines="10"
android:minLines="10"
android:scrollbarFadeDuration="1000"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical"
android:singleLine="false" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/descriptionStr"
android:layout_marginTop="15dp"
android:adjustViewBounds="true"
android:src="@drawable/demo"/>
</LinearLayout>
</ScrollView>
Activity文件:MainActivity.java
package com.anand.textviewdemo.activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.anand.textviewdemo.R;
/**
* ScrollView嵌套EditView或滑動的TextView沖突
* @ClassName: MainActivity
* @Description: ScrollView嵌套EditView或滑動的TextView沖突
* @author Anand
* @date 2017年6月28日 下午2:10:46
*/
public class MainActivity extends ActionBarActivity {
/** ButterKnife注入 **/
@Bind(R.id.et_content)
EditText etContent;
@Bind(R.id.btn_ok)
Button btnOk;
@Bind(R.id.tv_default)
TextView tvDefault;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ButterKnife綁定Activity
ButterKnife.bind(this);
initView(); //初始化視圖
initData(); //初始化數據
initEvent(); //初始化事件
}
/**
* 初始化視圖
*/
private void initView() {
//設置觸摸事件
etContent.setOnTouchListener(touchListener);
//textView滑動需要設置滑動方法
tvDefault.setMovementMethod(ScrollingMovementMethod.getInstance());
//設置觸摸事件
tvDefault.setOnTouchListener(touchListener);
}
/**
* 設置觸摸事件,由于EditView與TextView都處于ScollView中,
* 所以需要在OnTouch事件中通知父控件不攔截子控件事件
*/
private OnTouchListener touchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN
|| event.getAction() == MotionEvent.ACTION_MOVE){
//按下或滑動時請求父節點不攔截子節點
v.getParent().requestDisallowInterceptTouchEvent(true);
}
if(event.getAction() == MotionEvent.ACTION_UP){
//抬起時請求父節點攔截子節點
v.getParent().requestDisallowInterceptTouchEvent(false);
}
return false;
}
};
/**
* 初始化數據
*/
private void initData() {
}
/**
* 初始化事件
*/
private void initEvent() {
}
/**
* ButterKnife的OnClick注解
* 多個控件具有相同的事件
* @param btn
*/
@OnClick(R.id.btn_ok)
public void btnOnclick(Button btn){
String content = etContent.getText().toString().trim();
if(TextUtils.isEmpty(content)){
Toast.makeText(this,"編輯內容不能為空",Toast.LENGTH_SHORT).show();
return;
}else{
tvDefault.setText(content);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//ButterKnife解綁
ButterKnife.unbind(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
最后來一張修復后整體效果: